Merge pull request #42833 from Shopify/fix-memcached-6.1-local-store

MemCacheStore: Properly duplicate local cache values in 6.1 mode
This commit is contained in:
Jean Boussier 2021-07-21 22:03:02 +02:00 committed by GitHub
commit 212077a972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 29 deletions

View File

@ -32,6 +32,43 @@ module ActiveSupport
prepend Strategy::LocalCache
module DupLocalCache
class LocalStore < Strategy::LocalCache::LocalStore
def write_entry(_key, entry)
if entry.is_a?(Entry)
entry.dup_value!
end
super
end
def fetch_entry(key)
entry = @data.fetch(key) do
new_entry = yield
if entry.is_a?(Entry)
new_entry.dup_value!
end
@data[key] = new_entry
end
entry = entry.dup
if entry.is_a?(Entry)
entry.dup_value!
end
entry
end
end
def with_local_cache
if ActiveSupport::Cache.format_version == 6.1
use_temporary_local_cache(LocalStore.new) { yield }
else
super
end
end
end
prepend DupLocalCache
ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
# Creates a new Dalli::Client instance with specified addresses and options.

View File

@ -7,15 +7,15 @@ module LocalCacheBehavior
end
assert_equal @cache.class.name, events[0].payload[:store]
events = with_instrumentation "read" do
@cache.with_local_cache do
@cache.with_local_cache do
events = with_instrumentation "read" do
@cache.read("foo")
@cache.read("foo")
end
end
expected = [@cache.class.name, "ActiveSupport::Cache::Strategy::LocalCache::LocalStore"]
assert_equal expected, events.map { |p| p.payload[:store] }
expected = [@cache.class.name, @cache.send(:local_cache).class.name]
assert_equal expected, events.map { |p| p.payload[:store] }
end
end
def test_local_writes_are_persistent_on_the_remote_cache

View File

@ -268,30 +268,6 @@ class MemCacheStoreTest < ActiveSupport::TestCase
end
end
def test_initial_object_mutation_after_fetch
if ActiveSupport::Cache.format_version == 6.1
skip "Local cache mutation can't be prevented on legacy MemCacheStore"
else
super
end
end
def test_initial_object_mutation_after_write
if ActiveSupport::Cache.format_version == 6.1
skip "Local cache mutation can't be prevented on legacy MemCacheStore"
else
super
end
end
def test_local_cache_of_read_returns_a_copy_of_the_entry
if ActiveSupport::Cache.format_version == 6.1
skip "Local cache mutation can't be prevented on legacy MemCacheStore"
else
super
end
end
private
def random_string(length)
(0...length).map { (65 + rand(26)).chr }.join