Skip to content

Commit

Permalink
Merge pull request rails#4007 from exviva/expand_cache_key_for_one_el…
Browse files Browse the repository at this point in the history
…ement_array

Fix expanding cache key for single element arrays
  • Loading branch information
josevalim committed Dec 16, 2011
2 parents 0065f37 + abe915f commit 4f35d5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def expand_cache_key(key, namespace = nil)
def retrieve_cache_key(key)
case
when key.respond_to?(:cache_key) then key.cache_key
when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param
when key.is_a?(Array) then ['Array', *key.map { |element| retrieve_cache_key(element) }].to_param
else key.to_param
end.to_s
end
Expand Down
22 changes: 15 additions & 7 deletions activesupport/test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

class CacheKeyTest < ActiveSupport::TestCase
def test_expand_cache_key
assert_equal '1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true])
assert_equal 'name/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true], :name)
assert_equal 'Array/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true])
assert_equal 'name/Array/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true], :name)
end

def test_expand_cache_key_with_rails_cache_id
begin
ENV['RAILS_CACHE_ID'] = 'c99'
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
assert_equal 'c99/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
assert_equal 'c99/Array/foo', ActiveSupport::Cache.expand_cache_key([:foo])
assert_equal 'c99/Array/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key(:foo, :nm)
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
assert_equal 'nm/c99/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
assert_equal 'nm/c99/Array/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
assert_equal 'nm/c99/Array/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
ensure
ENV['RAILS_CACHE_ID'] = nil
end
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_expand_cache_key_array_with_something_that_responds_to_cache_key
def key.cache_key
:foo_key
end
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key([key])
assert_equal 'Array/foo_key', ActiveSupport::Cache.expand_cache_key([key])
end

def test_expand_cache_key_of_nil
Expand All @@ -69,6 +69,14 @@ def test_expand_cache_key_of_false
def test_expand_cache_key_of_true
assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true)
end

def test_expand_cache_key_of_one_element_array_different_than_key_of_element
element = 'foo'
array = [element]
element_cache_key = ActiveSupport::Cache.expand_cache_key(element)
array_cache_key = ActiveSupport::Cache.expand_cache_key(array)
assert_not_equal element_cache_key, array_cache_key
end
end

class CacheStoreSettingTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 4f35d5a

Please sign in to comment.