Fix MemCacheStore local cache duplication

Followup: https://github.com/rails/rails/pull/42833

The previous fix wasn't working in practice because the LocalCache
middleware doesn't use `with_local_cache` but directly set a
regular `LocalStore` instance in the registry.

So instead we redecorate it on every access. It may cause some extra
allocations, but since it only happens in 6.1 mode, it's not as
much of a concern.
This commit is contained in:
Jean Boussier 2021-07-29 11:38:14 +02:00
parent c166b3b64a
commit 5d1e8884bd
1 changed files with 13 additions and 9 deletions

View File

@ -7,6 +7,7 @@ rescue LoadError => e
raise e
end
require "delegate"
require "active_support/core_ext/enumerable"
require "active_support/core_ext/array/extract_options"
@ -33,7 +34,7 @@ module ActiveSupport
prepend Strategy::LocalCache
module DupLocalCache
class LocalStore < Strategy::LocalCache::LocalStore
class DupLocalStore < DelegateClass(Strategy::LocalCache::LocalStore)
def write_entry(_key, entry)
if entry.is_a?(Entry)
entry.dup_value!
@ -42,12 +43,12 @@ module ActiveSupport
end
def fetch_entry(key)
entry = @data.fetch(key) do
entry = super do
new_entry = yield
if entry.is_a?(Entry)
new_entry.dup_value!
end
@data[key] = new_entry
new_entry
end
entry = entry.dup
@ -59,13 +60,16 @@ module ActiveSupport
end
end
def with_local_cache
if ActiveSupport::Cache.format_version == 6.1
use_temporary_local_cache(LocalStore.new) { yield }
else
super
private
def local_cache
if ActiveSupport::Cache.format_version == 6.1
if local_cache = super
DupLocalStore.new(local_cache)
end
else
super
end
end
end
end
prepend DupLocalCache