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:
Thomas Waldmann 2026-04-24 10:13:00 +02:00
parent b055b713af
commit 32cfddc34c
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01

View file

@ -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()