aboutsummaryrefslogtreecommitdiffstats
path: root/util/cache.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-11-18 21:35:31 +0100
committerKim Alvefur <zash@zash.se>2017-11-18 21:35:31 +0100
commitfbe0a8387aa756fe4d8977f53f5e139bd7b7c92b (patch)
tree3dda9ddc281899c79fd7b2553bc9916d25ff1c07 /util/cache.lua
parent41d8ce349314d6faaabe877d0eda10b59cc76743 (diff)
downloadprosody-fbe0a8387aa756fe4d8977f53f5e139bd7b7c92b.tar.gz
prosody-fbe0a8387aa756fe4d8977f53f5e139bd7b7c92b.zip
util.cache: Add a method to resize the cache
Diffstat (limited to 'util/cache.lua')
-rw-r--r--util/cache.lua14
1 files changed, 14 insertions, 0 deletions
diff --git a/util/cache.lua b/util/cache.lua
index d12551cc..beb55112 100644
--- a/util/cache.lua
+++ b/util/cache.lua
@@ -116,6 +116,20 @@ function cache_methods:tail()
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