Diff

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
line wrap: on
line diff
--- a/util/cache.lua	Thu Mar 17 18:08:16 2016 +0100
+++ b/util/cache.lua	Thu Mar 17 19:07:40 2016 +0000
@@ -92,6 +92,18 @@
 	return self._count;
 end
 
+function cache_methods:head()
+	local head = self._head;
+	if not head then return nil, nil; end
+	return head.key, head.value;
+end
+
+function cache_methods:tail()
+	local tail = self._tail;
+	if not tail then return nil, nil; end
+	return tail.key, tail.value;
+end
+
 local function new(size, on_evict)
 	size = assert(tonumber(size), "cache size must be a number");
 	size = math.floor(size);