Comparison

util/cache.lua @ 7289:42e7545d5ae3

util.cache: Add head() and tail() methods (and tests)
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2016 19:07:40 +0000
parent 7016:e0a0af42b09f
child 7290:70ab13e81cf5
comparison
equal deleted inserted replaced
7287:867f107409d9 7289:42e7545d5ae3
90 90
91 function cache_methods:count() 91 function cache_methods:count()
92 return self._count; 92 return self._count;
93 end 93 end
94 94
95 function cache_methods:head()
96 local head = self._head;
97 if not head then return nil, nil; end
98 return head.key, head.value;
99 end
100
101 function cache_methods:tail()
102 local tail = self._tail;
103 if not tail then return nil, nil; end
104 return tail.key, tail.value;
105 end
106
95 local function new(size, on_evict) 107 local function new(size, on_evict)
96 size = assert(tonumber(size), "cache size must be a number"); 108 size = assert(tonumber(size), "cache size must be a number");
97 size = math.floor(size); 109 size = math.floor(size);
98 assert(size > 0, "cache size must be greater than zero"); 110 assert(size > 0, "cache size must be greater than zero");
99 local data = {}; 111 local data = {};