Software /
code /
prosody
Comparison
util/cache.lua @ 7435:8603b16e85c7
util.cache: Add support for creating a proxy table to a cache, that looks and acts (mostly) like a normal table. No tests yet.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 May 2016 18:18:23 +0100 |
parent | 7354:8ca7f1c2c660 |
child | 7702:9385c367e920 |
comparison
equal
deleted
inserted
replaced
7433:7eec6f3c7300 | 7435:8603b16e85c7 |
---|---|
114 local tail = self._tail; | 114 local tail = self._tail; |
115 if not tail then return nil, nil; end | 115 if not tail then return nil, nil; end |
116 return tail.key, tail.value; | 116 return tail.key, tail.value; |
117 end | 117 end |
118 | 118 |
119 function cache_methods:table() | |
120 if not self.proxy_table then | |
121 self.proxy_table = setmetatable({}, { | |
122 __index = function (t, k) | |
123 return self:get(k); | |
124 end; | |
125 __newindex = function (t, k, v) | |
126 if not self:set(k, v) then | |
127 error("failed to insert key into cache - full"); | |
128 end | |
129 end; | |
130 __pairs = function (t) | |
131 return self:items(); | |
132 end; | |
133 __len = function (t) | |
134 return self:count(); | |
135 end; | |
136 }); | |
137 end | |
138 return self.proxy_table; | |
139 end | |
140 | |
119 local function new(size, on_evict) | 141 local function new(size, on_evict) |
120 size = assert(tonumber(size), "cache size must be a number"); | 142 size = assert(tonumber(size), "cache size must be a number"); |
121 size = math.floor(size); | 143 size = math.floor(size); |
122 assert(size > 0, "cache size must be greater than zero"); | 144 assert(size > 0, "cache size must be greater than zero"); |
123 local data = {}; | 145 local data = {}; |