aboutsummaryrefslogtreecommitdiffstats
path: root/util/cache.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2015-12-22 20:10:07 +0000
committerMatthew Wild <mwild1@gmail.com>2015-12-22 20:10:07 +0000
commit1b680c04ecbdfc849b113cb82d60a4271a7cf14b (patch)
treef783300ed6da959b1e3af4e37002c43375c98fbc /util/cache.lua
parentbf2a083f24654a1011a52ac0e10c00daaf3ad297 (diff)
downloadprosody-1b680c04ecbdfc849b113cb82d60a4271a7cf14b.tar.gz
prosody-1b680c04ecbdfc849b113cb82d60a4271a7cf14b.zip
util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
Diffstat (limited to 'util/cache.lua')
-rw-r--r--util/cache.lua11
1 files changed, 6 insertions, 5 deletions
diff --git a/util/cache.lua b/util/cache.lua
index 72b74351..d3639b3f 100644
--- a/util/cache.lua
+++ b/util/cache.lua
@@ -51,19 +51,20 @@ function cache_methods:set(k, v)
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