diff options
author | Matthew Wild <mwild1@gmail.com> | 2016-03-17 19:08:42 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2016-03-17 19:08:42 +0000 |
commit | 67ede3e0936f1e0dd396e18d56b040684b7b9842 (patch) | |
tree | 1e6d472788da75932563d756ae0135d1e19425f0 /util | |
parent | 7070585c169306ee8226d23afa73e7f3f1251984 (diff) | |
download | prosody-67ede3e0936f1e0dd396e18d56b040684b7b9842.tar.gz prosody-67ede3e0936f1e0dd396e18d56b040684b7b9842.zip |
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.
Diffstat (limited to 'util')
-rw-r--r-- | util/cache.lua | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/util/cache.lua b/util/cache.lua index e53bf4bf..074916cd 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -51,10 +51,13 @@ 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; - 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 @@ function cache_methods:set(k, v) 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 |