Software /
code /
prosody
Diff
util/cache.lua @ 7290:70ab13e81cf5
util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 17 Mar 2016 19:08:42 +0000 |
parent | 7289:42e7545d5ae3 |
child | 7354:8ca7f1c2c660 |
line wrap: on
line diff
--- a/util/cache.lua Thu Mar 17 19:07:40 2016 +0000 +++ b/util/cache.lua Thu Mar 17 19:08:42 2016 +0000 @@ -51,10 +51,13 @@ 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; - on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; + 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 + -- Cache is full, and we're not allowed to evict + return false; + end _remove(self, tail); self._data[evicted_key] = nil; end @@ -62,9 +65,6 @@ 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