Pass options to write_entry in handle_expired_entry method

This commit is contained in:
Graham Cooper 2024-04-11 19:55:47 -04:00 committed by Rafael Mendonça França
parent 33592402f9
commit b09ae673ab
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 37 additions and 2 deletions

@ -1030,7 +1030,8 @@ def handle_expired_entry(entry, key, options)
# When an entry has a positive :race_condition_ttl defined, put the stale entry back into the cache # When an entry has a positive :race_condition_ttl defined, put the stale entry back into the cache
# for a brief period while the entry is being recalculated. # for a brief period while the entry is being recalculated.
entry.expires_at = Time.now.to_f + race_ttl entry.expires_at = Time.now.to_f + race_ttl
write_entry(key, entry, expires_in: race_ttl * 2) options[:expires_in] = race_ttl * 2
write_entry(key, entry, **options)
else else
delete_entry(key, **options) delete_entry(key, **options)
end end

@ -2,11 +2,12 @@
module CacheStoreCoderBehavior module CacheStoreCoderBehavior
class SpyCoder class SpyCoder
attr_reader :dumped_entries, :loaded_entries attr_reader :dumped_entries, :loaded_entries, :dump_compressed_entries
def initialize def initialize
@dumped_entries = [] @dumped_entries = []
@loaded_entries = [] @loaded_entries = []
@dump_compressed_entries = []
end end
def dump(entry) def dump(entry)
@ -19,6 +20,15 @@ def load(payload)
@loaded_entries << entry @loaded_entries << entry
entry entry
end end
def dump_compressed(entry, threshold)
if threshold == 0
@dump_compressed_entries << entry
Marshal.dump(entry)
else
dump(entry)
end
end
end end
def test_coder_receive_the_entry_on_write def test_coder_receive_the_entry_on_write
@ -83,4 +93,28 @@ def test_nil_coder_bypasses_serialization
entry = ActiveSupport::Cache::Entry.new("value") entry = ActiveSupport::Cache::Entry.new("value")
assert_same entry, @store.send(:serialize_entry, entry) assert_same entry, @store.send(:serialize_entry, entry)
end end
def test_coder_is_used_during_handle_expired_entry_when_expired
coder = SpyCoder.new
@store = lookup_store(coder: coder)
@store.write("foo", "bar", expires_in: 1.second)
assert_equal 0, coder.loaded_entries.size
assert_equal 1, coder.dumped_entries.size
travel_to(2.seconds.from_now) do
val = @store.fetch(
"foo",
race_condition_ttl: 5,
compress: true,
compress_threshold: 0
) { "baz" }
assert_equal "baz", val
assert_equal 1, coder.loaded_entries.size # 1 read in fetch
assert_equal "bar", coder.loaded_entries.first.value
assert_equal 1, coder.dumped_entries.size # did not change from original write
assert_equal 2, coder.dump_compressed_entries.size # 1 write the expired entry handler, 1 in fetch
assert_equal "bar", coder.dump_compressed_entries.first.value
assert_equal "baz", coder.dump_compressed_entries.last.value
end
end
end end