mirror of
https://github.com/borgbackup/borg.git
synced 2026-05-27 11:43:18 -04:00
LRUCache fixes
fix: resolve KeyError and memory leaks in LRUCache - __setitem__: assign value before popping from _lru to avoid KeyError when exceeding capacity. - clear(): clear the _lru list also to prevent stale keys causing KeyErrors during future evictions.
This commit is contained in:
parent
b055b713af
commit
32cfddc34c
1 changed files with 2 additions and 1 deletions
|
|
@ -12,10 +12,10 @@ class LRUCache:
|
|||
assert key not in self._cache, (
|
||||
"Unexpected attempt to replace a cached item,"
|
||||
" without first deleting the old item.")
|
||||
self._cache[key] = value
|
||||
self._lru.append(key)
|
||||
while len(self._lru) > self._capacity:
|
||||
del self[self._lru[0]]
|
||||
self._cache[key] = value
|
||||
|
||||
def __getitem__(self, key):
|
||||
value = self._cache[key] # raise KeyError if not found
|
||||
|
|
@ -49,6 +49,7 @@ class LRUCache:
|
|||
for value in self._cache.values():
|
||||
self._dispose(value)
|
||||
self._cache.clear()
|
||||
self._lru.clear()
|
||||
|
||||
def items(self):
|
||||
return self._cache.items()
|
||||
|
|
|
|||
Loading…
Reference in a new issue