Mercurial > prosody-hg
comparison util/cache.lua @ 8398:3eff1b60269a
util.cache: Call on-eviction callback when shrinking
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 18 Nov 2017 21:35:40 +0100 |
| parents | 99d85731e3ee |
| children | c4c06fbb7d87 |
comparison
equal
deleted
inserted
replaced
| 8397:99d85731e3ee | 8398:3eff1b60269a |
|---|---|
| 118 | 118 |
| 119 function cache_methods:resize(new_size) | 119 function cache_methods:resize(new_size) |
| 120 new_size = assert(tonumber(new_size), "cache size must be a number"); | 120 new_size = assert(tonumber(new_size), "cache size must be a number"); |
| 121 new_size = math.floor(new_size); | 121 new_size = math.floor(new_size); |
| 122 assert(new_size > 0, "cache size must be greater than zero"); | 122 assert(new_size > 0, "cache size must be greater than zero"); |
| 123 local on_evict = self._on_evict; | |
| 123 while self._count > new_size do | 124 while self._count > new_size do |
| 124 local tail = self._tail; | 125 local tail = self._tail; |
| 125 local evicted_key = tail.key; | 126 local evicted_key, evicted_value = tail.key, tail.value; |
| 127 if on_evict ~= nil and (on_evict == false or on_evict(evicted_key, evicted_value) == false) then | |
| 128 -- Cache is full, and we're not allowed to evict | |
| 129 return false; | |
| 130 end | |
| 126 _remove(self, tail); | 131 _remove(self, tail); |
| 127 self._data[evicted_key] = nil; | 132 self._data[evicted_key] = nil; |
| 128 end | 133 end |
| 129 self.size = new_size; | 134 self.size = new_size; |
| 130 return true; | 135 return true; |
