1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-24 08:45:13 +00:00

Simpler and faster LRUCache implementation

This commit is contained in:
Jonas Borgström 2012-12-06 22:57:55 +01:00
parent f28933254a
commit e80e600d41

View file

@ -3,78 +3,37 @@
import unittest import unittest
class LRUCache(DictMixin): class LRUCache(dict):
"""Heap queue based Least Recently Used Cache implementation
"""
class Node(object): def __init__(self, capacity):
"""Internal cache node super(LRUCache, self).__init__()
""" self._lru = []
__slots__ = ('key', 'value', 't') self._capacity = capacity
def __init__(self, key, value, t):
self.key = key
self.value = value
self.t = t
def __cmp__(self, other):
return cmp(self.t, other.t)
def __init__(self, size):
self.size = size
self._t = 0
self.clear()
def clear(self):
self._heap = []
self._dict = {}
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._t += 1
try: try:
node = self._dict[key] self._lru.remove(key)
node.value = value except ValueError:
node.t = self._t pass
heapify(self._heap) self._lru.append(key)
except KeyError: while len(self._lru) > self._capacity:
node = self.Node(key, value, self._t) del self[self._lru[0]]
self._dict[key] = node return super(LRUCache, self).__setitem__(key, value)
if len(self) < self.size:
heappush(self._heap, node)
else:
old = heapreplace(self._heap, node)
del self._dict[old.key]
def __getitem__(self, key): def __getitem__(self, key):
node = self._dict[key] try:
self[key] = node.value self._lru.remove(key)
return node.value self._lru.append(key)
except ValueError:
pass
return super(LRUCache, self).__getitem__(key)
def __delitem__(self, key): def __delitem__(self, key):
node = self._dict[key] try:
del self._dict[key] self._lru.remove(key)
self._heap.remove(node) except ValueError:
heapify(self._heap) pass
return super(LRUCache, self).__delitem__(key)
def __iter__(self):
copy = self._heap[:]
while copy:
yield heappop(copy).key
def iteritems(self):
copy = self._heap[:]
while copy:
node = heappop(copy)
yield node.key, node.value
def keys(self):
return self._dict.keys()
def __contains__(self, key):
return key in self._dict
def __len__(self):
return len(self._heap)
class LRUCacheTestCase(unittest.TestCase): class LRUCacheTestCase(unittest.TestCase):
@ -85,8 +44,8 @@ def test(self):
for i, x in enumerate('abc'): for i, x in enumerate('abc'):
c[x] = i c[x] = i
self.assertEqual(len(c), 2) self.assertEqual(len(c), 2)
self.assertEqual(list(c), ['b', 'c']) self.assertEqual(set(c), set(['b', 'c']))
self.assertEqual(list(c.iteritems()), [('b', 1), ('c', 2)]) self.assertEqual(set(c.iteritems()), set([('b', 1), ('c', 2)]))
self.assertEqual(False, 'a' in c) self.assertEqual(False, 'a' in c)
self.assertEqual(True, 'b' in c) self.assertEqual(True, 'b' in c)
self.assertRaises(KeyError, lambda: c['a']) self.assertRaises(KeyError, lambda: c['a'])