util/cache.lua
changeset 7019 e0a0af42b09f
parent 6948 d779c55058c6
child 7292 42e7545d5ae3
--- a/util/cache.lua	Wed Dec 16 16:45:57 2015 +0000
+++ b/util/cache.lua	Tue Dec 22 20:10:07 2015 +0000
@@ -51,19 +51,20 @@
 		return true;
 	end
 	-- Check whether we need to remove oldest k/v
+	local on_evict, evicted_key, evicted_value;
 	if self._count == self.size then
 		local tail = self._tail;
-		local on_evict = self._on_evict;
-		if on_evict then
-			on_evict(tail.key, tail.value);
-		end
+		on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value;
 		_remove(self, tail);
-		self._data[tail.key] = nil;
+		self._data[evicted_key] = nil;
 	end
 
 	m = { key = k, value = v, prev = nil, next = nil };
 	self._data[k] = m;
 	_insert(self, m);
+	if on_evict and evicted_key then
+		on_evict(evicted_key, evicted_value, self);
+	end
 	return true;
 end