util/cache.lua
changeset 13178 8ec7b7d6556f
parent 11202 c4c06fbb7d87
child 13179 bbdaa770b955
equal deleted inserted replaced
13177:4906d4990ffe 13178: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);