Whole file

tkem/cachetools

The author described this change as Fix #292, fix #205, fix #103: TTLCache.expire() returns iterable of expired (key, value) pairs.. It counts as a record because the checks below fail on the code as it stood at 726b0111e and pass on bb4b37cfc, with nothing else changed between the two runs.

Fix saved2024-08-18
Sharing licenceMIT · LICENSE
Change size+8 4

What the code was meant to do, written into the code itself as a save note

Fix #292, fix #205, fix #103: TTLCache.expire() returns iterable of expired (key, value) pairs.

The change

2626
2727
2828 class _DefaultSize:
29-
3029 __slots__ = ()
3130
3231 def __getitem__(self, _):
378377 """LRU Cache implementation with per-item time-to-live (TTL) value."""
379378
380379 class _Link:
381-
382380 __slots__ = ("key", "expires", "next", "prev")
383381
384382 def __init__(self, key=None, expires=None):
469467 return self.__ttl
470468
471469 def expire(self, time=None):
472- """Remove expired items from the cache."""
470+ """Remove expired items from the cache and return an iterable of the
471+ expired `(key, value)` pairs.
472+
473+ """
473474 if time is None:
474475 time = self.timer()
475476 root = self.__root
476477 curr = root.next
477478 links = self.__links
479+ expired = []
478480 cache_delitem = Cache.__delitem__
481+ cache_getitem = Cache.__getitem__
479482 while curr is not root and not (time < curr.expires):
483+ expired.append((curr.key, cache_getitem(self, curr.key)))
480484 cache_delitem(self, curr.key)
481485 del links[curr.key]
482486 next = curr.next
483487 curr.unlink()
484488 curr = next
489+ return expired
485490
486491 def popitem(self):
487492 """Remove and return the `(key, value)` pair least recently used that
508513
509514 @functools.total_ordering
510515 class _Item:
511-
512516 __slots__ = ("key", "expires", "removed")
513517
514518 def __init__(self, key=None, expires=None):

The check that tells the two apart

failpass·tests/test_ttl.py::TTLCacheTest::test_ttl_datetime
failpass·tests/test_ttl.py::TTLCacheTest::test_ttl_expire

Check file tests/test_ttl.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it726b0111e06f655c332ea4765a89d4669719649b
Broken version dated2024-07-15
Modulecachetools.__init__
Units changedTLRUCache, TTLCache, _DefaultSize
Fingerprint3530635704c69878
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in tkem/cachetools