Skip to content

Commit

Permalink
Fix MemCacheStore local cache duplication
Browse files Browse the repository at this point in the history
Followup: #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.
  • Loading branch information
byroot committed Jul 29, 2021
1 parent c166b3b commit 5d1e888
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions activesupport/lib/active_support/cache/mem_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
raise e
end

require "delegate"
require "active_support/core_ext/enumerable"
require "active_support/core_ext/array/extract_options"

Expand All @@ -33,7 +34,7 @@ def self.supports_cache_versioning?
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!
Expand All @@ -42,12 +43,12 @@ def write_entry(_key, entry)
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

Expand All @@ -59,13 +60,16 @@ def fetch_entry(key)
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

Expand Down

0 comments on commit 5d1e888

Please sign in to comment.