Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expire fragment cache when locale changes?

I'm trying to use fragment cache to cache the footer and navigation bar on a Ruby on Rails Site that uses I18n. The problem is, changing the language then shows you the footer and navigation bar in the wrong language. How do you go about expiring fragment cache when locale changes?

like image 335
octimizer Avatar asked May 20 '12 08:05

octimizer


2 Answers

Rather than expiring the fragment cache, you should make the locale part of the cache key, i.e. something like

cache :locale => I18n.locale, ... do
  ...
end

This way different users can see different language versions of the footer/navigation bar but all will see cached versions.

like image 127
Frederick Cheung Avatar answered Sep 28 '22 05:09

Frederick Cheung


When caching a fragment in Rails 3, this did the trick for me:

- cache([object, locale: I18n.locale]) do
like image 20
wspruijt Avatar answered Sep 28 '22 03:09

wspruijt