diff options
author | Kim Alvefur <zash@zash.se> | 2017-11-18 21:35:40 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2017-11-18 21:35:40 +0100 |
commit | a0a8ef1815389f449abe262fd156cb96fce369e0 (patch) | |
tree | 9b984ced088c9d301d8ff3fd92127414efb46601 /util/cache.lua | |
parent | f06af190960f0f5c8ea5f32c38e6ff3c49581f30 (diff) | |
download | prosody-a0a8ef1815389f449abe262fd156cb96fce369e0.tar.gz prosody-a0a8ef1815389f449abe262fd156cb96fce369e0.zip |
util.cache: Call on-eviction callback when shrinking
Diffstat (limited to 'util/cache.lua')
-rw-r--r-- | util/cache.lua | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/util/cache.lua b/util/cache.lua index beb55112..a5fd5e6d 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -120,9 +120,14 @@ function cache_methods:resize(new_size) new_size = assert(tonumber(new_size), "cache size must be a number"); new_size = math.floor(new_size); assert(new_size > 0, "cache size must be greater than zero"); + local on_evict = self._on_evict; while self._count > new_size do local tail = self._tail; - local evicted_key = tail.key; + 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 + -- Cache is full, and we're not allowed to evict + return false; + end _remove(self, tail); self._data[evicted_key] = nil; end |