# HG changeset patch # User Kim Alvefur # Date 1511037331 -3600 # Node ID 99d85731e3ee7aa77ff80c8656f6022374503ca5 # Parent fbe1f99fb245e196369beba4c5309fc045b8e3e3 util.cache: Add a method to resize the cache diff -r fbe1f99fb245 -r 99d85731e3ee util/cache.lua --- a/util/cache.lua Tue Nov 07 00:38:47 2017 +0100 +++ b/util/cache.lua Sat Nov 18 21:35:31 2017 +0100 @@ -116,6 +116,20 @@ return tail.key, tail.value; end +function cache_methods:resize(new_size) + new_size = assert(tonumber(new_size), "cache size must be a number"); + new_size = math.floor(new_size); + assert(new_size > 0, "cache size must be greater than zero"); + while self._count > new_size do + local tail = self._tail; + local evicted_key = tail.key; + _remove(self, tail); + self._data[evicted_key] = nil; + end + self.size = new_size; + return true; +end + function cache_methods:table() --luacheck: ignore 212/t if not self.proxy_table then