3

I am using rails 3 with cache_digests and I have a fragment cache in a template:

<% cache [I18n.locale,'city-list'] do %>
...
<% end %>

In an observer I want to expire that cache for all locales like this:

I18n.available_locales.each do |loc|
  ActionController::Base.new.expire_fragment("#{loc}/city-list")
end

But obviously this won't work, because I am missing the digest of that fragment in the expire call.

Is there a clean way to expire this cache?

Benjamin
  • 1,726
  • 1
  • 14
  • 35
  • I had a similar issue, needing to cache a json response. Shared my solution here: http://stackoverflow.com/a/23783119/252799 - hopefully it is helpful – oma May 21 '14 at 12:34

2 Answers2

5

It seems like the answer is "No way to make cache_digests work with manual expiration", see these answers from DHH.

To work around there are two options:

  1. Tie the key to some model and rely on the key-based expiration: <% cache [I18n.locale, city, "city-list"] do %>

  2. Skip the digest in the cache call, and expire manually like showed in the question: <% cache [I18n.locale, "city-list"], skip_digest: true do %>

Benjamin
  • 1,726
  • 1
  • 14
  • 35
  • Not sure 1 works. Can you expand on it? I'd like to keep the digest but expire with digest if possible. – Abram Oct 29 '16 at 19:32
0

Try,

<% cache "#{I18n.locale} city-list" do %>
...
<% end %>

#To clear
I18n.available_locales.each do |loc|
  ActionController::Base.new.expire_fragment("#{loc} city-list")
end
Thaha kp
  • 3,689
  • 1
  • 26
  • 25