diff options
author | Kim Alvefur <zash@zash.se> | 2023-06-30 22:01:49 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2023-06-30 22:01:49 +0200 |
commit | 29b6ed4f0a40f0f595d21537e1308f744b9a4907 (patch) | |
tree | 021def1fc118a9a7594d0369c120912811c25cc8 /util | |
parent | 8ba5e50cc295e820ec188a80f6459b69bde2d8f9 (diff) | |
download | prosody-29b6ed4f0a40f0f595d21537e1308f744b9a4907.tar.gz prosody-29b6ed4f0a40f0f595d21537e1308f744b9a4907.zip |
util.cache: Keep eviction candidate if callback resized to make room
Previously either the old or the new values would be rejected, even if
the cache was resized to allow more items.
Diffstat (limited to 'util')
-rw-r--r-- | util/cache.lua | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/util/cache.lua b/util/cache.lua index cd1b4544..a72a535f 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -54,12 +54,17 @@ function cache_methods:set(k, v) if self._count == self.size then local tail = self._tail; local on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; - if on_evict ~= nil and (on_evict == false or on_evict(evicted_key, evicted_value) == false) then + + local do_evict = on_evict and on_evict(evicted_key, evicted_value); + + if do_evict == false then -- Cache is full, and we're not allowed to evict return false; + elseif self._count == self.size then + -- Cache wasn't grown + _remove(self, tail); + self._data[evicted_key] = nil; end - _remove(self, tail); - self._data[evicted_key] = nil; end m = { key = k, value = v, prev = nil, next = nil }; |