From e292bf5b88843a6d03ca178d3b0c4f70ecff70a6 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Thu, 30 Nov 2017 22:23:07 +0000 Subject: [PATCH] allow caching options to be specified for counting services --- app/services/base_count_service.rb | 8 +++++++- spec/services/base_count_service_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/services/base_count_service.rb b/app/services/base_count_service.rb index 99cc9a196e6..19873fe09a5 100644 --- a/app/services/base_count_service.rb +++ b/app/services/base_count_service.rb @@ -9,7 +9,7 @@ class BaseCountService end def count - Rails.cache.fetch(cache_key, raw: raw?) { uncached_count }.to_i + Rails.cache.fetch(cache_key, cache_options) { uncached_count }.to_i end def refresh_cache @@ -31,4 +31,10 @@ class BaseCountService def cache_key raise NotImplementedError, 'cache_key must be implemented and return a String' end + + # subclasses can override to add any specific options, such as + # super.merge({ expires_in: 5.minutes }) + def cache_options + { raw: raw? } + end end diff --git a/spec/services/base_count_service_spec.rb b/spec/services/base_count_service_spec.rb index 5ec8ed0976d..090b2dcdd43 100644 --- a/spec/services/base_count_service_spec.rb +++ b/spec/services/base_count_service_spec.rb @@ -77,4 +77,10 @@ describe BaseCountService, :use_clean_rails_memory_store_caching do expect { service.cache_key }.to raise_error(NotImplementedError) end end + + describe '#cache_options' do + it 'returns the default in options' do + expect(service.cache_options).to eq({ raw: false }) + end + end end