Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Revert "Merge pull request #14269 from arthurnn/expanded_key_array"

This reverts commit 475c965, reversing
changes made to 705915a.

We decided that this is not worth busting everyone's cache as this
seems like a very unlikely problem. The problem only occurs when the
user is 1) not using a namespace, or 2) using the same namesapce for
different *kinds* of cache items. The recommended "fix" is to put
those cache items into their own namspace:

    id = 1
    Rails.cache.fetch(id, namespace: "user"){ User.find(id) }

    ids = [1]
    Rails.cache.fetch(ids, namespace: "users"){ User.find(ids) }

See the discussion on #14269 for details.
  • Loading branch information...
commit ccf8f27dddcc36fa5c91f614647e0b0bac861d83 1 parent 475c965
@chancancode chancancode authored
5 activesupport/
@@ -1,8 +1,3 @@
-* Cache key should add a trailing slash when the key is an array,
- so `cache.fetch('foo')` and `cache.fetch(['foo'])` wont conflict.
- *arthurnn*
* Change the signature of `fetch_multi` to return a hash rather than an
array. This makes it consistent with the output of `read_multi`.
7 activesupport/lib/active_support/cache.rb
@@ -511,7 +511,7 @@ def merged_options(call_options) # :nodoc:
# called. If the key is a Hash, then keys will be sorted alphabetically.
def expanded_key(key) # :nodoc:
return key.cache_key.to_s if key.respond_to?(:cache_key)
- trailing_slash = false
case key
when Array
if key.size > 1
@@ -519,12 +519,11 @@ def expanded_key(key) # :nodoc:
key = key.first
- trailing_slash = true
when Hash
key = key.sort_by { |k,_| k.to_s }.collect{|k,v| "#{k}=#{v}"}
- key = key.to_param
- trailing_slash ? "#{key}/" : key
+ key.to_param
# Prefix a key with the namespace. Namespace and key will be delimited
6 activesupport/test/caching_test.rb
@@ -257,10 +257,6 @@ def test_fetch_with_cached_nil
assert_nil @cache.fetch('foo') { 'baz' }
- def test_fetch_with_array_and_without
- assert_not_equal @cache.fetch('foo') { 'barz' }, @cache.fetch(['foo']) { 'barr' }
- end
def test_should_read_and_write_hash
assert @cache.write('foo', {:a => "b"})
assert_equal({:a => "b"},'foo'))
@@ -353,7 +349,7 @@ def obj.to_param
def test_array_as_cache_key
@cache.write([:fu, "foo"], "bar")
- assert_equal "bar","fu/foo/")
+ assert_equal "bar","fu/foo")
def test_hash_as_cache_key

2 comments on commit ccf8f27


Correction: it seems like namespaced_key is intended for the global namespace on the entire Rails.cache. So while this would work as long as you don't have a global namesapce, I'd go with what @arthurnn suggested, or manually expand your own key with expand_cache_key.

But the spirit of the recommendation still stand: put different types of cache items into different namespaces.

Please sign in to comment.
Something went wrong with that request. Please try again.