Customized cache_key based on i18n.locale
Hi,
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] = Proc.new { |request|
[I18n.locale, ':', Rack::Cache::Key.new(request).generate].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 => Proc.new { |request|
[I18n.locale, ':', Rack::Cache::Key.new(request).generate].join
}
}
Am I missing something? Thanks for your help. Bastien
PS: I currently deactivated Rack::Cache in order to serve the right language
Did you execute the config.action_dispatch.rack_cache[:cache_key] = ... AFTER the config.action_dispatch.rack_cache = { code?
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 :)