aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2019-07-15 21:57:20 +0100
committerGitHub <noreply@github.com>2019-07-15 21:57:20 +0100
commitf30f76af747858826d3618866676cd5a4979e64a (patch)
treeef01a081fd44ab0f9733bcb46a116808b0f5771a /activesupport
parentbcbe3dc6108704769d660fb6fd56b70d1ae77092 (diff)
parentf08bf726d3038f062dd7485614d1ea85d952f121 (diff)
downloadrails-f30f76af747858826d3618866676cd5a4979e64a.tar.gz
rails-f30f76af747858826d3618866676cd5a4979e64a.tar.bz2
rails-f30f76af747858826d3618866676cd5a4979e64a.zip
Merge pull request #36656 from Edouard-chin/ec-local-cache-reference
Return a copy of the cache entry when local_cache exists:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache/strategy/local_cache.rb5
-rw-r--r--activesupport/test/cache/behaviors/local_cache_behavior.rb9
2 files changed, 13 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb
index 67c455771e..8e80946fbb 100644
--- a/activesupport/lib/active_support/cache/strategy/local_cache.rb
+++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb
@@ -74,7 +74,10 @@ module ActiveSupport
end
def fetch_entry(key, options = nil) # :nodoc:
- @data.fetch(key) { @data[key] = yield }
+ entry = @data.fetch(key) { @data[key] = yield }
+ dup_entry = entry.dup
+ dup_entry&.dup_value!
+ dup_entry
end
end
diff --git a/activesupport/test/cache/behaviors/local_cache_behavior.rb b/activesupport/test/cache/behaviors/local_cache_behavior.rb
index baa38ba6ac..6f5d53c190 100644
--- a/activesupport/test/cache/behaviors/local_cache_behavior.rb
+++ b/activesupport/test/cache/behaviors/local_cache_behavior.rb
@@ -46,6 +46,15 @@ module LocalCacheBehavior
end
end
+ def test_local_cache_of_read_returns_a_copy_of_the_entry
+ @cache.with_local_cache do
+ @cache.write(:foo, type: "bar")
+ value = @cache.read(:foo)
+ assert_equal("bar", value.delete(:type))
+ assert_equal({ type: "bar" }, @cache.read(:foo))
+ end
+ end
+
def test_local_cache_of_read
@cache.write("foo", "bar")
@cache.with_local_cache do