Comparison

util/cache.lua @ 8397:99d85731e3ee

util.cache: Add a method to resize the cache
author Kim Alvefur <zash@zash.se>
date Sat, 18 Nov 2017 21:35:31 +0100
parent 8396:fbe1f99fb245
child 8398:3eff1b60269a
comparison
equal deleted inserted replaced
8396:fbe1f99fb245 8397:99d85731e3ee
114 local tail = self._tail; 114 local tail = self._tail;
115 if not tail then return nil, nil; end 115 if not tail then return nil, nil; end
116 return tail.key, tail.value; 116 return tail.key, tail.value;
117 end 117 end
118 118
119 function cache_methods:resize(new_size)
120 new_size = assert(tonumber(new_size), "cache size must be a number");
121 new_size = math.floor(new_size);
122 assert(new_size > 0, "cache size must be greater than zero");
123 while self._count > new_size do
124 local tail = self._tail;
125 local evicted_key = tail.key;
126 _remove(self, tail);
127 self._data[evicted_key] = nil;
128 end
129 self.size = new_size;
130 return true;
131 end
132
119 function cache_methods:table() 133 function cache_methods:table()
120 --luacheck: ignore 212/t 134 --luacheck: ignore 212/t
121 if not self.proxy_table then 135 if not self.proxy_table then
122 self.proxy_table = setmetatable({}, { 136 self.proxy_table = setmetatable({}, {
123 __index = function (t, k) 137 __index = function (t, k)