diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index b7ccacf..648e7a4 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -150,7 +150,7 @@ def _cache_write(key, object, *arguments) # Clear the cache. def cache_clear(options = nil) # Memcache doesn't support delete_matched, which can also be computationally expensive - if (Object.const_defined?(:Dalli) && cached_resource.cache.class.instance_of?("ActiveSupport::Cache::MemCacheStore")) || options.try(:fetch, :all) + if (Object.const_defined?(:Dalli) && cached_resource.cache.instance_of?(ActiveSupport::Cache::MemCacheStore)) || options.try(:fetch, :all) cached_resource.cache.clear.tap do |result| cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} CLEAR ALL") end @@ -162,8 +162,8 @@ def cache_clear(options = nil) end def cache_key_delete_pattern - case cached_resource.cache.class.to_s - when "ActiveSupport::Cache::MemoryStore", "ActiveSupport::Cache::FileStore" + case cached_resource.cache + when ActiveSupport::Cache::MemoryStore, ActiveSupport::Cache::FileStore /^#{name_key}\// else "#{name_key}/*" diff --git a/spec/cached_resource/caching_spec.rb b/spec/cached_resource/caching_spec.rb index 54d74c2..b0d58bf 100644 --- a/spec/cached_resource/caching_spec.rb +++ b/spec/cached_resource/caching_spec.rb @@ -321,19 +321,26 @@ def read_from_cache(key, model = Thing) end describe "#cache_key_delete_pattern" do - let(:cache_class_name) { "Redis" } + let(:cache_class) { "Redis" } + before do - allow(Thing.cached_resource).to receive(:cache).and_return(double(class: cache_class_name)) + allow(Thing.cached_resource).to receive(:cache).and_return(cache_class) end - ["ActiveSupport::Cache::MemoryStore", "ActiveSupport::Cache::FileStore"].each do |cache_name| - context "with cache #{cache_name}" do - let(:cache_class_name) { cache_name } - it do - expect(Thing.send(:cache_key_delete_pattern)).to eq(/^thing\//) - end + context "with cache ActiveSupport::Cache::MemoryStore" do + let(:cache_class) { ActiveSupport::Cache::MemoryStore.new } + it do + expect(Thing.send(:cache_key_delete_pattern)).to eq(/^thing\//) + end + end + + context "with cache ActiveSupport::Cache::FileStore" do + let(:cache_class) { ActiveSupport::Cache::FileStore.new('tmp/') } + it do + expect(Thing.send(:cache_key_delete_pattern)).to eq(/^thing\//) end end + context "default" do it do expect(Thing.send(:cache_key_delete_pattern)).to eq("thing/*")