# HG changeset patch # User Kim Alvefur # Date 1688155315 -7200 # Node ID bbdaa770b955d4f5e8c704239307b9341f22d5f7 # Parent 8ec7b7d6556f72adca1427557190b6feb4605766 util.cache: Pass cache itself to eviction callback Simplifies access to the cache without moving code around a lot given the currently common pattern of local some_cache = cache.new(size, function(k,v) end) diff -r 8ec7b7d6556f -r bbdaa770b955 spec/util_cache_spec.lua --- a/spec/util_cache_spec.lua Fri Jun 30 22:01:49 2023 +0200 +++ b/spec/util_cache_spec.lua Fri Jun 30 22:01:55 2023 +0200 @@ -390,8 +390,7 @@ end); it("eviction stuff", function () - local c; - c = cache.new(4, function(_k,_v) + local c = cache.new(4, function(_k,_v,c) if c.size < 10 then c:resize(c.size*2); end diff -r 8ec7b7d6556f -r bbdaa770b955 util/cache.lua --- a/util/cache.lua Fri Jun 30 22:01:49 2023 +0200 +++ b/util/cache.lua Fri Jun 30 22:01:55 2023 +0200 @@ -55,7 +55,7 @@ local tail = self._tail; local on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; - local do_evict = on_evict and on_evict(evicted_key, evicted_value); + local do_evict = on_evict and on_evict(evicted_key, evicted_value, self); if do_evict == false then -- Cache is full, and we're not allowed to evict @@ -129,7 +129,7 @@ while self._count > new_size do local tail = self._tail; local evicted_key, evicted_value = tail.key, tail.value; - if on_evict ~= nil and (on_evict == false or on_evict(evicted_key, evicted_value) == false) then + if on_evict ~= nil and (on_evict == false or on_evict(evicted_key, evicted_value, self) == false) then -- Cache is full, and we're not allowed to evict return false; end