Adding current locale to default caching key

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding current locale to default caching key

Мурыгин Антон
Hello, community.
Tonight was the first time i faced one new bug on my website, when i saw one cached partial to be returned in another language than currently selected one. That  was i guess because that cache was generated when another locale was enabled. 
Hence, shouldn't local be included by default into cache key?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Adding current locale to default caching key

Мурыгин Антон
fix: Hence, shouldn't locale be included by default into cache key?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Adding current locale to default caching key

Kir Shatrov
I don't think that it should be included by default, since a small minority of apps need localization (https://github.com/rails/rails/pull/21124#issuecomment-127995334).

The best thing we can do for it - introduce the `default_key_scope` method for Rails cache, and in this case you'd be able to set the prefix for all cache keys.

On Friday, September 11, 2015 at 12:52:58 AM UTC+3, Anthon Mur wrote:
fix: Hence, shouldn't locale be included by default into cache key?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Adding current locale to default caching key

Kir Shatrov
In reply to this post by Мурыгин Антон
Btw, I've looked into possible solution and here is the code to achieve what you want without modifying Rails:

```
def LocalizedCacheStore(klass)
  Class.new(klass) do
    def namespaced_key(key, options)
      "#{I18n.locale}:#{super}"
    end
  end
end

config.cache_store = LocalizedCacheStore(ActiveSupport::Cache::MemoryStore).new # you can pass any cache class to LocalizedCacheStore
```

It's a good question if we should include it to Rails or not - I'm interested to listen other's opinion.

On Friday, September 11, 2015 at 12:52:58 AM UTC+3, Anthon Mur wrote:
fix: Hence, shouldn't locale be included by default into cache key?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Adding current locale to default caching key

Yves-Eric Martin-4
In reply to this post by Kir Shatrov
Hi all,


I have been hit by this issue too a few years back, so I think I should contribute. First, I18n has been a first class citizen since Rails 2.2 (http://weblog.rubyonrails.org/2008/11/18/new-rails-2-2-i18n-defaults), so I would not be so quick to dismiss the problem because a minority of apps use the feature.

So the main question is: "shouldn't locale be included by default into cache key?"

One important thing to keep in mind: Rails cache can store HTML snippets (for which having the locale in the key would help), but can also be used to store other, non-i18n data (for which a key without the locale is fine). So let's look at what happens with and without locale in the default key namespace in these two use cases, when a developer adds I18n to his app:

1) Without locale in the default key namespace (current behavior)
  non-i18n data: works fine
  HTML: breaks, developer wastes time tracking the problem, and ends up adding locale to the key manually

2) With locale in the default key namespace
  non-i18n data: extra, unneeded cache miss per locale, but still works fine
  HTML: works fine out of the box.

Looking at it this way, it seems obvious to me that 2) should be the default behavior, since it works fine out of the box in both cases. And if you are worried about the performance impact of the extra cache misses for non-i18n data, they I'll wager back in the "a small minority of apps need localization", meaning most apps won't be affected at all.

So, +1 to including the locale in the default key namespace.



On Tuesday, 15 September 2015 02:56:20 UTC+9, Kir Shatrov wrote:
I don't think that it should be included by default, since a small minority of apps need localization (<a href="https://github.com/rails/rails/pull/21124#issuecomment-127995334" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F21124%23issuecomment-127995334\46sa\75D\46sntz\0751\46usg\75AFQjCNHAvSIhRHX6yfgF49djRuhzDCFLfA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F21124%23issuecomment-127995334\46sa\75D\46sntz\0751\46usg\75AFQjCNHAvSIhRHX6yfgF49djRuhzDCFLfA&#39;;return true;">https://github.com/rails/rails/pull/21124#issuecomment-127995334).

The best thing we can do for it - introduce the `default_key_scope` method for Rails cache, and in this case you'd be able to set the prefix for all cache keys.

On Friday, September 11, 2015 at 12:52:58 AM UTC+3, Anthon Mur wrote:
fix: Hence, shouldn't locale be included by default into cache key?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Adding current locale to default caching key

igorkasyanchuk@gmail.com
Possible solutions: https://github.com/igorkasyanchuk/cache_with_locale

On Tuesday, September 15, 2015 at 8:22:05 PM UTC+3, Yves-Eric wrote:
Hi all,


I have been hit by this issue too a few years back, so I think I should contribute. First, I18n has been a first class citizen since Rails 2.2 (<a href="http://weblog.rubyonrails.org/2008/11/18/new-rails-2-2-i18n-defaults" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fweblog.rubyonrails.org%2F2008%2F11%2F18%2Fnew-rails-2-2-i18n-defaults\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEkGJC_5rCdGZ7WcgOZxTYV9-LjaA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fweblog.rubyonrails.org%2F2008%2F11%2F18%2Fnew-rails-2-2-i18n-defaults\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEkGJC_5rCdGZ7WcgOZxTYV9-LjaA&#39;;return true;">http://weblog.rubyonrails.org/2008/11/18/new-rails-2-2-i18n-defaults), so I would not be so quick to dismiss the problem because a minority of apps use the feature.

So the main question is: "shouldn't locale be included by default into cache key?"

One important thing to keep in mind: Rails cache can store HTML snippets (for which having the locale in the key would help), but can also be used to store other, non-i18n data (for which a key without the locale is fine). So let's look at what happens with and without locale in the default key namespace in these two use cases, when a developer adds I18n to his app:

1) Without locale in the default key namespace (current behavior)
  non-i18n data: works fine
  HTML: breaks, developer wastes time tracking the problem, and ends up adding locale to the key manually

2) With locale in the default key namespace
  non-i18n data: extra, unneeded cache miss per locale, but still works fine
  HTML: works fine out of the box.

Looking at it this way, it seems obvious to me that 2) should be the default behavior, since it works fine out of the box in both cases. And if you are worried about the performance impact of the extra cache misses for non-i18n data, they I'll wager back in the "a small minority of apps need localization", meaning most apps won't be affected at all.

So, +1 to including the locale in the default key namespace.



On Tuesday, 15 September 2015 02:56:20 UTC+9, Kir Shatrov wrote:
I don't think that it should be included by default, since a small minority of apps need localization (<a href="https://github.com/rails/rails/pull/21124#issuecomment-127995334" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F21124%23issuecomment-127995334\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHAvSIhRHX6yfgF49djRuhzDCFLfA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F21124%23issuecomment-127995334\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHAvSIhRHX6yfgF49djRuhzDCFLfA&#39;;return true;">https://github.com/rails/rails/pull/21124#issuecomment-127995334).

The best thing we can do for it - introduce the `default_key_scope` method for Rails cache, and in this case you'd be able to set the prefix for all cache keys.

On Friday, September 11, 2015 at 12:52:58 AM UTC+3, Anthon Mur wrote:
fix: Hence, shouldn't locale be included by default into cache key?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.