Rails cache with locale
Automatically adding current application locale (I18n.locale) as a part of caching key in Rails views.
Problem & Solution
For example you have:
- Rails app with different locales
- you want to add fragment caching in views
When you have a different locales and you adding a caching you writing code like this
= cache [@user, I18n.locale] do = render @user
As you see you need to add
I18n.locale as a part of caching key. And you need to do it everywhere. What if you forget about it in some view?
This gem is a simple solution which allows you to DRY your code. So now you can write
= cache @user do = render @user
And let gem handle all work. You don't need to specify locale as a cache sufix/prefix.
This gem also works for collection caching like this:
= render partial: :post, collection: @posts, cached: true
Just add this gem to your Gemfile.
Add this line to your application's Gemfile:
And then execute:
- initial version, ready for production
The gem is available as open source under the terms of the MIT License.