Skip to content

Commit

Permalink
Merge pull request #31 from CodiTramuntana/clean_cache_for_dalli_store
Browse files Browse the repository at this point in the history
[FIX] Added NoMethodError to rescue in clear_cache
  • Loading branch information
ahukkanen authored Mar 3, 2020
2 parents 5d7dca1 + 65040e7 commit 7c1bee2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/decidim/term_customizer/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def translations_hash
# the resolver.
def clear_cache
Rails.cache.delete_matched("#{cache_key_base}/*")
rescue NotImplementedError
# Some cache store, such as `ActiveSupport::Cache::MemCacheStore` do not
# support `delete_matched`. Therefore, clear all the possibly existing
rescue NotImplementedError, NoMethodError
# Some cache store, such as `ActiveSupport::Cache::MemCacheStore` or
# `ActiveSupport::Cache::DalliStore` do not support `delete_matched`.
# Therefore, clear all the possibly existing
# cache keys manually for each space and component.

# Clear all the "organization" translation keys.
Expand Down
67 changes: 67 additions & 0 deletions spec/lib/decidim/term_customizer/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,73 @@
end
end
end

# The DalliStore does not implement `delete_matched` which allows us to
# test the `clear_cache` functionality when the cache implementation raises
# a `NoMethodError`.
context "when using dalli_store" do
before do
allow(Rails).to receive(:cache).and_return(
ActiveSupport::Cache.lookup_store(:dalli_store)
)
end

context "without organization" do
let(:organization) { nil }

it "clears cache with correct key" do
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/system"
)

subject.clear_cache
end
end

context "with organization" do
it "clears cache with correct key" do
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}"
)

subject.clear_cache
end
end

context "with organization and space" do
let(:space) { create(:participatory_process, organization: organization) }

it "clears cache with correct key" do
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}"
)
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}/space_#{space.id}"
)

subject.clear_cache
end
end

context "with organization, space and component" do
let(:space) { create(:participatory_process, organization: organization) }
let(:component) { create(:proposal_component, participatory_space: space) }

it "clears cache with correct key" do
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}"
)
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}/space_#{space.id}"
)
expect(Rails.cache).to receive(:delete).with(
"decidim_term_customizer/organization_#{organization.id}/space_#{space.id}/component_#{component.id}"
)

subject.clear_cache
end
end
end
end

def flatten_hash(hash)
Expand Down

0 comments on commit 7c1bee2

Please sign in to comment.