diff options
author | Matthew Wild <mwild1@gmail.com> | 2016-05-22 18:18:23 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2016-05-22 18:18:23 +0100 |
commit | 59957bc13cbf9c18c8e7379559455d5d1802e7ec (patch) | |
tree | cec43dc364d25338b5c10aebde4e93b0fad31cdf /util | |
parent | 288422c9c30c40727577f896b55596b40e0120f6 (diff) | |
download | prosody-59957bc13cbf9c18c8e7379559455d5d1802e7ec.tar.gz prosody-59957bc13cbf9c18c8e7379559455d5d1802e7ec.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')
-rw-r--r-- | util/cache.lua | 22 |
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); |