Skip to content

Commit 46d98f1

Browse files
Merge pull request rails#50276 from fusion2004/fix-read-multi-raw-with-mem-cache-store
fix LocalCache#read_multi_entries not namespacing keys & not deserializing raw values correctly
2 parents 572f475 + d5c7f7c commit 46d98f1

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def serialize_entry(entry, **options)
826826
end
827827
end
828828

829-
def deserialize_entry(payload)
829+
def deserialize_entry(payload, **)
830830
payload.nil? ? nil : @coder.load(payload)
831831
rescue DeserializationError
832832
nil

activesupport/lib/active_support/cache/strategy/local_cache.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,20 @@ def read_serialized_entry(key, raw: false, **options)
131131
end
132132
end
133133

134-
def read_multi_entries(keys, **options)
134+
def read_multi_entries(names, **options)
135135
return super unless local_cache
136136

137-
local_entries = local_cache.read_multi_entries(keys)
137+
keys_to_names = names.index_by { |name| normalize_key(name, options) }
138+
139+
local_entries = local_cache.read_multi_entries(keys_to_names.keys)
140+
local_entries.transform_keys! { |key| keys_to_names[key] }
138141
local_entries.transform_values! do |payload|
139-
deserialize_entry(payload)&.value
142+
deserialize_entry(payload, **options)&.value
140143
end
141-
missed_keys = keys - local_entries.keys
144+
missed_names = names - local_entries.keys
142145

143-
if missed_keys.any?
144-
local_entries.merge!(super(missed_keys, **options))
146+
if missed_names.any?
147+
local_entries.merge!(super(missed_names, **options))
145148
else
146149
local_entries
147150
end

activesupport/test/cache/behaviors/local_cache_behavior.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def test_local_cache_of_increment
190190
@cache.with_local_cache do
191191
@cache.write(key, 1, raw: true)
192192
@peek.write(key, 2, raw: true)
193+
193194
@cache.increment(key)
194195

195196
expected = @peek.read(key, raw: true)
@@ -205,6 +206,7 @@ def test_local_cache_of_decrement
205206
@peek.write(key, 3, raw: true)
206207

207208
@cache.decrement(key)
209+
208210
expected = @peek.read(key, raw: true)
209211
assert_equal 2, Integer(expected)
210212
assert_equal expected, @cache.read(key, raw: true)
@@ -230,7 +232,7 @@ def test_local_cache_of_read_multi
230232
other_value = SecureRandom.alphanumeric
231233
@cache.with_local_cache do
232234
@cache.write(key, value, raw: true)
233-
@cache.write(other_key, other_value, raw: true)
235+
@peek.write(other_key, other_value, raw: true)
234236
values = @cache.read_multi(key, other_key, raw: true)
235237
assert_equal value, @cache.read(key, raw: true)
236238
assert_equal other_value, @cache.read(other_key, raw: true)
@@ -239,6 +241,16 @@ def test_local_cache_of_read_multi
239241
end
240242
end
241243

244+
def test_local_cache_of_read_multi_prioritizes_local_entries
245+
key = "key#{rand}"
246+
@cache.with_local_cache do
247+
@cache.write(key, "foo")
248+
@cache.send(:bypass_local_cache) { @cache.write(key, "bar") }
249+
250+
assert_equal({ key => "foo" }, @cache.read_multi(key))
251+
end
252+
end
253+
242254
def test_initial_object_mutation_after_write
243255
key = SecureRandom.uuid
244256
@cache.with_local_cache do

0 commit comments

Comments
 (0)