diff options
author | Matthew Wild <mwild1@gmail.com> | 2016-03-17 19:16:43 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2016-03-17 19:16:43 +0000 |
commit | 2e443086a9b85b2b39b6d1b15c1a36c3894eaa2e (patch) | |
tree | 554b21ae3a5aed22205975f61994d87a561099b3 /util | |
parent | c08a9c7d78063b4632f54468d3d3a0cbf0a1f204 (diff) | |
parent | aa6d79191613b687877e39b54732d875ea0ee767 (diff) | |
download | prosody-2e443086a9b85b2b39b6d1b15c1a36c3894eaa2e.tar.gz prosody-2e443086a9b85b2b39b6d1b15c1a36c3894eaa2e.zip |
Merge 0.10->trunk
Diffstat (limited to 'util')
-rw-r--r-- | util/cache.lua | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/util/cache.lua b/util/cache.lua index d3639b3f..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 @@ -92,6 +92,18 @@ function cache_methods:count() return self._count; end +function cache_methods:head() + local head = self._head; + if not head then return nil, nil; end + return head.key, head.value; +end + +function cache_methods:tail() + local tail = self._tail; + if not tail then return nil, nil; end + return tail.key, tail.value; +end + local function new(size, on_evict) size = assert(tonumber(size), "cache size must be a number"); size = math.floor(size); |