aboutsummaryrefslogtreecommitdiffstats
path: root/util/cache.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2016-05-22 18:18:23 +0100
committerMatthew Wild <mwild1@gmail.com>2016-05-22 18:18:23 +0100
commit83eb775cb0b7287da70b8203cf7acfd5fd4b9c26 (patch)
treecec43dc364d25338b5c10aebde4e93b0fad31cdf /util/cache.lua
parentf7c696bc0b7d8878647ba115c36ac2309c3b970b (diff)
downloadprosody-83eb775cb0b7287da70b8203cf7acfd5fd4b9c26.tar.gz
prosody-83eb775cb0b7287da70b8203cf7acfd5fd4b9c26.zip
util.cache: Add support for creating a proxy table to a cache, that looks and acts (mostly) like a normal table. No tests yet.
Diffstat (limited to 'util/cache.lua')
-rw-r--r--util/cache.lua22
1 files changed, 22 insertions, 0 deletions
diff --git a/util/cache.lua b/util/cache.lua
index 54f3e10b..44bbfe30 100644
--- a/util/cache.lua
+++ b/util/cache.lua
@@ -116,6 +116,28 @@ function cache_methods:tail()
return tail.key, tail.value;
end
+function cache_methods:table()
+ if not self.proxy_table then
+ self.proxy_table = setmetatable({}, {
+ __index = function (t, k)
+ return self:get(k);
+ end;
+ __newindex = function (t, k, v)
+ if not self:set(k, v) then
+ error("failed to insert key into cache - full");
+ end
+ end;
+ __pairs = function (t)
+ return self:items();
+ end;
+ __len = function (t)
+ return self:count();
+ end;
+ });
+ end
+ return self.proxy_table;
+end
+
local function new(size, on_evict)
size = assert(tonumber(size), "cache size must be a number");
size = math.floor(size);