1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-02-23 14:41:43 +00:00

lrucache: cleanup (-10 lines)

dict.pop() will raise KeyError for us if necessary.  I was confused
because we used to have lrucache.pop() with a bug, that returned None
instead.

Great catch by @ThomasWaldmann.
This commit is contained in:
Alan Jenkins 2015-08-14 10:14:17 +01:00
parent db298268e4
commit 76f6737e9d

View file

@ -1,6 +1,3 @@
class _NotFound:
pass
class LRUCache:
def __init__(self, capacity, dispose):
self._cache = {}
@ -18,22 +15,15 @@ def __setitem__(self, key, value):
self._cache[key] = value
def __getitem__(self, key):
try:
self._lru.remove(key)
self._lru.append(key)
except ValueError:
pass
return self._cache[key]
value = self._cache[key] # raise KeyError if not found
self._lru.remove(key)
self._lru.append(key)
return value
def __delitem__(self, key):
try:
self._lru.remove(key)
except ValueError:
pass
item = self._cache.pop(key, _NotFound)
if item is _NotFound:
raise KeyError(key)
self._dispose(item)
value = self._cache.pop(key) # raise KeyError if not found
self._dispose(value)
self._lru.remove(key)
def __contains__(self, key):
return key in self._cache