1 local cache_methods = {};
2 local cache_mt = { __index = cache_methods };
4 local function new(size)
5 assert(size > 0, "cache size must be greater than zero");
7 return setmetatable({ data = data, count = 0, size = size, head = nil, tail = nil }, cache_mt);
10 local function _remove(list, m)
17 if list.tail == m then
20 if list.head == m then
23 list.count = list.count - 1;
26 local function _insert(list, m)
30 m.prev, m.next = nil, list.head;
35 list.count = list.count + 1;
38 function cache_methods:set(k, v)
39 local m = self.data[k];
43 -- Bump to head of list
58 -- Check whether we need to remove oldest k/v
59 if self.count == self.size then
60 self.data[self.tail.key] = nil;
61 _remove(self, self.tail);
64 m = { key = k, value = v, prev = nil, next = nil };
69 function cache_methods:get(k)
70 local m = self.data[k];
77 function cache_methods:items()
83 local k, v = m.key, m.value;