Mercurial > prosody-hg
comparison util/cache.lua @ 13174:8ec7b7d6556f
util.cache: Keep eviction candidate if callback resized to make room
Previously either the old or the new values would be rejected, even if
the cache was resized to allow more items.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 30 Jun 2023 22:01:49 +0200 |
| parents | c4c06fbb7d87 |
| children | bbdaa770b955 |
comparison
equal
deleted
inserted
replaced
| 13173:4906d4990ffe | 13174:8ec7b7d6556f |
|---|---|
| 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 if self._count == self.size then | 54 if self._count == self.size then |
| 55 local tail = self._tail; | 55 local tail = self._tail; |
| 56 local on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; | 56 local on_evict, evicted_key, evicted_value = self._on_evict, tail.key, tail.value; |
| 57 if on_evict ~= nil and (on_evict == false or on_evict(evicted_key, evicted_value) == false) then | 57 |
| 58 local do_evict = on_evict and on_evict(evicted_key, evicted_value); | |
| 59 | |
| 60 if do_evict == false then | |
| 58 -- Cache is full, and we're not allowed to evict | 61 -- Cache is full, and we're not allowed to evict |
| 59 return false; | 62 return false; |
| 63 elseif self._count == self.size then | |
| 64 -- Cache wasn't grown | |
| 65 _remove(self, tail); | |
| 66 self._data[evicted_key] = nil; | |
| 60 end | 67 end |
| 61 _remove(self, tail); | |
| 62 self._data[evicted_key] = nil; | |
| 63 end | 68 end |
| 64 | 69 |
| 65 m = { key = k, value = v, prev = nil, next = nil }; | 70 m = { key = k, value = v, prev = nil, next = nil }; |
| 66 self._data[k] = m; | 71 self._data[k] = m; |
| 67 _insert(self, m); | 72 _insert(self, m); |
