Skip to content
Browse files

Cache key should be different when is Array.

`cache.fetch(['foo'])` and `cache.fetch('foo')` should generate
different cache keys as they are not equivalents.

[related #8615]
[related #8614]
  • Loading branch information...
1 parent ffcc617 commit 7d98247446e9f043dc9b6b5cbdb938877dd00a81 @arthurnn arthurnn committed Mar 3, 2014
Showing with 14 additions and 4 deletions.
  1. +5 −0 activesupport/CHANGELOG.md
  2. +4 −3 activesupport/lib/active_support/cache.rb
  3. +5 −1 activesupport/test/caching_test.rb
View
5 activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* 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`.
View
7 activesupport/lib/active_support/cache.rb
@@ -511,19 +511,20 @@ 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
key = key.collect{|element| expanded_key(element)}
else
key = key.first
end
+ trailing_slash = true
when Hash
key = key.sort_by { |k,_| k.to_s }.collect{|k,v| "#{k}=#{v}"}
end
-
- key.to_param
+ key = key.to_param
+ trailing_slash ? "#{key}/" : key
end
# Prefix a key with the namespace. Namespace and key will be delimited
View
6 activesupport/test/caching_test.rb
@@ -257,6 +257,10 @@ def test_fetch_with_cached_nil
assert_nil @cache.fetch('foo') { 'baz' }
end
+ 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"}, @cache.read('foo'))
@@ -349,7 +353,7 @@ def obj.to_param
def test_array_as_cache_key
@cache.write([:fu, "foo"], "bar")
- assert_equal "bar", @cache.read("fu/foo")
+ assert_equal "bar", @cache.read("fu/foo/")
end
def test_hash_as_cache_key

0 comments on commit 7d98247

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