Customized cache_key based on i18n.locale #68

bmurzeau opened this Issue Jun 26, 2012 · 2 comments


None yet
2 participants


I have a project running Rails 3.2.5 with Rack::Cache activated. The application_controller handles the detection of the user's preferred language and set I18n.locale accordingly.

When the first request arrives Rack::Cache caches the page (let's say in english) properly, but then if the next request, from another web browser in french, Rack::Cache serves the english version. The solution to fix this issue is to add a custom cache_key but I can't get it working. Here the two codes I've tried in production.rb:

config.action_dispatch.rack_cache[:cache_key] = { |request|
  [I18n.locale, ':',].join
config.action_dispatch.rack_cache = { 
  :metastore => "file:tmp/rack-cache/meta",
  :entitystore => "file:tmp/rack-cache/body",
  :verbose => true,
  :private_headers  => ['Cookie', 'Set-Cookie', 'Authorization'],
  :cache_key => { |request|
      [I18n.locale, ':',].join

Am I missing something? Thanks for your help.

PS: I currently deactivated Rack::Cache in order to serve the right language

patroza commented Nov 13, 2012

Did you execute the config.action_dispatch.rack_cache[:cache_key] = ... AFTER the config.action_dispatch.rack_cache = { code?

patroza commented Nov 13, 2012

Actually after further inspection, I can't see how this approach could work to begin with.
It will actually properly store pages correctly based on language, but in it's current implementation I can only see it return default locale pages upon cache hit - because I18n.locale is not actually set when determining the cache key to read.

Unless we are missing a piece of the puzzle where I18n.locale is set in some Rack plugin, e.g read from cookie :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment