Skip to content

Commit

Permalink
Merge pull request #37587 from Shopify/fix-local-cache-leak
Browse files Browse the repository at this point in the history
Duplicate the cached value before writing it in the local cache
  • Loading branch information
rafaelfranca committed Oct 29, 2019
2 parents 4b978d8 + 9b09187 commit 58fe42e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Expand Up @@ -64,8 +64,9 @@ def read_multi_entries(keys, **options)
values
end

def write_entry(key, value, **options)
@data[key] = value
def write_entry(key, entry, **options)
entry.dup_value!
@data[key] = entry
true
end

Expand Down
19 changes: 19 additions & 0 deletions activesupport/test/cache/behaviors/local_cache_behavior.rb
Expand Up @@ -150,6 +150,25 @@ def test_local_cache_of_read_multi
end
end

def test_initial_object_mutation_after_write
@cache.with_local_cache do
initial = +"bar"
@cache.write("foo", initial)
initial << "baz"
assert_equal "bar", @cache.read("foo")
end
end

def test_initial_object_mutation_after_fetch
@cache.with_local_cache do
initial = +"bar"
@cache.fetch("foo") { initial }
initial << "baz"
assert_equal "bar", @cache.read("foo")
assert_equal "bar", @cache.fetch("foo")
end
end

def test_middleware
app = lambda { |env|
result = @cache.write("foo", "bar")
Expand Down

0 comments on commit 58fe42e

Please sign in to comment.