Skip to content

Commit b09ae67

Browse files
grcooperrafaelfranca
authored andcommitted
Pass options to write_entry in handle_expired_entry method
1 parent 3359240 commit b09ae67

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,8 @@ def handle_expired_entry(entry, key, options)
10301030
# When an entry has a positive :race_condition_ttl defined, put the stale entry back into the cache
10311031
# for a brief period while the entry is being recalculated.
10321032
entry.expires_at = Time.now.to_f + race_ttl
1033-
write_entry(key, entry, expires_in: race_ttl * 2)
1033+
options[:expires_in] = race_ttl * 2
1034+
write_entry(key, entry, **options)
10341035
else
10351036
delete_entry(key, **options)
10361037
end

activesupport/test/cache/behaviors/cache_store_coder_behavior.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
module CacheStoreCoderBehavior
44
class SpyCoder
5-
attr_reader :dumped_entries, :loaded_entries
5+
attr_reader :dumped_entries, :loaded_entries, :dump_compressed_entries
66

77
def initialize
88
@dumped_entries = []
99
@loaded_entries = []
10+
@dump_compressed_entries = []
1011
end
1112

1213
def dump(entry)
@@ -19,6 +20,15 @@ def load(payload)
1920
@loaded_entries << entry
2021
entry
2122
end
23+
24+
def dump_compressed(entry, threshold)
25+
if threshold == 0
26+
@dump_compressed_entries << entry
27+
Marshal.dump(entry)
28+
else
29+
dump(entry)
30+
end
31+
end
2232
end
2333

2434
def test_coder_receive_the_entry_on_write
@@ -83,4 +93,28 @@ def test_nil_coder_bypasses_serialization
8393
entry = ActiveSupport::Cache::Entry.new("value")
8494
assert_same entry, @store.send(:serialize_entry, entry)
8595
end
96+
97+
def test_coder_is_used_during_handle_expired_entry_when_expired
98+
coder = SpyCoder.new
99+
@store = lookup_store(coder: coder)
100+
@store.write("foo", "bar", expires_in: 1.second)
101+
assert_equal 0, coder.loaded_entries.size
102+
assert_equal 1, coder.dumped_entries.size
103+
104+
travel_to(2.seconds.from_now) do
105+
val = @store.fetch(
106+
"foo",
107+
race_condition_ttl: 5,
108+
compress: true,
109+
compress_threshold: 0
110+
) { "baz" }
111+
assert_equal "baz", val
112+
assert_equal 1, coder.loaded_entries.size # 1 read in fetch
113+
assert_equal "bar", coder.loaded_entries.first.value
114+
assert_equal 1, coder.dumped_entries.size # did not change from original write
115+
assert_equal 2, coder.dump_compressed_entries.size # 1 write the expired entry handler, 1 in fetch
116+
assert_equal "bar", coder.dump_compressed_entries.first.value
117+
assert_equal "baz", coder.dump_compressed_entries.last.value
118+
end
119+
end
86120
end

0 commit comments

Comments
 (0)