Skip to content

Commit ff22bac

Browse files
committed
Maintain invariant of maxsize items in cache attribute in MultiHitLRUCache
This minor change ensures the cache never has more than `maxsize` items in `cache`. The previous implementation would temporarily exceed `maxsize` items by one item when the cache adds the newest entry to `cache` before removing the least recently used item from `cache` with `self.cache.popitem(last=False)`.
1 parent e2232f1 commit ff22bac

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Doc/library/collections.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,9 +1260,9 @@ variants of :func:`functools.lru_cache`:
12601260
else:
12611261
# entry has been seen more than cache_after times
12621262
self.requests.pop(args) # no longer need to keep track of how many times this entry has been seen
1263-
self.cache[args] = result
1264-
if len(self.cache) > self.maxsize:
1263+
if len(self.cache) == self.maxsize:
12651264
self.cache.popitem(last=False)
1265+
self.cache[args] = result
12661266
return result
12671267
12681268
.. doctest::

0 commit comments

Comments
 (0)