Skip to content

Commit

Permalink
Fix spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jlurena committed Jul 9, 2024
1 parent 17f6072 commit ee18844
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}/*"
Expand Down
23 changes: 15 additions & 8 deletions spec/cached_resource/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/*")
Expand Down

0 comments on commit ee18844

Please sign in to comment.