Software /
code /
prosody
Comparison
util/cache.lua @ 7016:e0a0af42b09f
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)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 22 Dec 2015 20:10:07 +0000 |
parent | 6945:d779c55058c6 |
child | 7289:42e7545d5ae3 |
comparison
equal
deleted
inserted
replaced
7012:990b4ddaf582 | 7016:e0a0af42b09f |
---|---|
49 -- New key | 49 -- New key |
50 if v == nil then | 50 if v == nil then |
51 return true; | 51 return true; |
52 end | 52 end |
53 -- Check whether we need to remove oldest k/v | 53 -- Check whether we need to remove oldest k/v |
54 local on_evict, evicted_key, evicted_value; | |
54 if self._count == self.size then | 55 if self._count == self.size then |
55 local tail = self._tail; | 56 local tail = self._tail; |
56 local on_evict = self._on_evict; | 57 on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; |
57 if on_evict then | |
58 on_evict(tail.key, tail.value); | |
59 end | |
60 _remove(self, tail); | 58 _remove(self, tail); |
61 self._data[tail.key] = nil; | 59 self._data[evicted_key] = nil; |
62 end | 60 end |
63 | 61 |
64 m = { key = k, value = v, prev = nil, next = nil }; | 62 m = { key = k, value = v, prev = nil, next = nil }; |
65 self._data[k] = m; | 63 self._data[k] = m; |
66 _insert(self, m); | 64 _insert(self, m); |
65 if on_evict and evicted_key then | |
66 on_evict(evicted_key, evicted_value, self); | |
67 end | |
67 return true; | 68 return true; |
68 end | 69 end |
69 | 70 |
70 function cache_methods:get(k) | 71 function cache_methods:get(k) |
71 local m = self._data[k]; | 72 local m = self._data[k]; |