Diff

util/cache.lua @ 13174:8ec7b7d6556f

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.
author Kim Alvefur <zash@zash.se>
date Fri, 30 Jun 2023 22:01:49 +0200
parent 11198:c4c06fbb7d87
child 13175:bbdaa770b955
line wrap: on
line diff
--- a/util/cache.lua	Fri Jun 30 18:51:03 2023 +0200
+++ b/util/cache.lua	Fri Jun 30 22:01:49 2023 +0200
@@ -54,12 +54,17 @@
 	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 };