From 9b967776a2cf95d9b0767f3c966bf15f6156b278 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Mon, 11 Aug 2008 08:22:28 -0700 Subject: [PATCH] dup in MemoryStore so future changes don't taint cache --- .../lib/active_support/cache/memory_store.rb | 11 +++++++++-- activesupport/test/caching_test.rb | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index a44f877..b2a31af 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -14,12 +14,12 @@ module ActiveSupport def read(name, options = nil) super - @data[name] + dup_value(@data[name]) end def write(name, value, options = nil) super - @data[name] = value + @data[name] = dup_value(value) end def delete(name, options = nil) @@ -49,6 +49,13 @@ module ActiveSupport def clear @data.clear end + + protected + def dup_value(value) + value.dup + rescue TypeError + value + end end end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index f3220d2..2b897fc 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -70,3 +70,20 @@ uses_mocha 'high-level cache store tests' do end end end + +class MemoryStoreTest < Test::Unit::TestCase + def setup + @cache = ActiveSupport::Cache.lookup_store(:memory_store) + end + + def test_should_not_change_state_after_cache + hash = {:a => 1} + @cache.write('foo', hash) + hash[:a] = 2 + assert_equal 1, @cache.read('foo')[:a] + end + + def test_should_return_nil_for_non_existing_key + assert_equal nil, @cache.read('doesntexist') + end +end -- 1.5.6.4