How to expire fragment cache when changing a locale?

I am trying to use the fragment cache to cache the footer and navigation bar on a Ruby on Rails site using I18n. The problem is that when you change the language, the footer and navigation bar in the wrong language are displayed. How are you going to flush the fragment cache when changing the locale?

+7
source share
3 answers

Instead of completing the fragment cache, you should make the local part of the cache key, i.e. something like

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

Thus, different users can see different language versions of the footer / navigation bar, but everyone will see cached versions.

+15
source

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

 - cache([object, locale: I18n.locale]) do 
+6
source

I use this helper, so I don't need to explicitly pass the locale for every cache call:

 # frozen_string_literal: true module CacheHelper # Always using current I18n.locale to cache things. def cache(name = {}, options = {}, &block) name_with_locale = [name].flatten << I18n.locale.to_s super(name_with_locale, options, &block) end end 
0
source

Source: https://habr.com/ru/post/916187/


All Articles