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.
Projecttkem/cachetools
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
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | 28 | class _DefaultSize: | |
| 29 | - | ||
| 30 | 29 | __slots__ = () | |
| 31 | 30 | ||
| 32 | 31 | def __getitem__(self, _): | |
| ⋯ | |||
| 378 | 377 | """LRU Cache implementation with per-item time-to-live (TTL) value.""" | |
| 379 | 378 | ||
| 380 | 379 | class _Link: | |
| 381 | - | ||
| 382 | 380 | __slots__ = ("key", "expires", "next", "prev") | |
| 383 | 381 | ||
| 384 | 382 | def __init__(self, key=None, expires=None): | |
| ⋯ | |||
| 469 | 467 | return self.__ttl | |
| 470 | 468 | ||
| 471 | 469 | 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 | + | """ | |
| 473 | 474 | if time is None: | |
| 474 | 475 | time = self.timer() | |
| 475 | 476 | root = self.__root | |
| 476 | 477 | curr = root.next | |
| 477 | 478 | links = self.__links | |
| 479 | + | expired = [] | |
| 478 | 480 | cache_delitem = Cache.__delitem__ | |
| 481 | + | cache_getitem = Cache.__getitem__ | |
| 479 | 482 | while curr is not root and not (time < curr.expires): | |
| 483 | + | expired.append((curr.key, cache_getitem(self, curr.key))) | |
| 480 | 484 | cache_delitem(self, curr.key) | |
| 481 | 485 | del links[curr.key] | |
| 482 | 486 | next = curr.next | |
| 483 | 487 | curr.unlink() | |
| 484 | 488 | curr = next | |
| 489 | + | return expired | |
| 485 | 490 | ||
| 486 | 491 | def popitem(self): | |
| 487 | 492 | """Remove and return the `(key, value)` pair least recently used that | |
| ⋯ | |||
| 508 | 513 | ||
| 509 | 514 | @functools.total_ordering | |
| 510 | 515 | class _Item: | |
| 511 | - | ||
| 512 | 516 | __slots__ = ("key", "expires", "removed") | |
| 513 | 517 | ||
| 514 | 518 | def __init__(self, key=None, expires=None): | |
The check that tells the two apart
fail→pass·tests/test_ttl.py::TTLCacheTest::test_ttl_datetime
fail→pass·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
- 2026-07-22TLRUCache
- 2026-03-08Fix #218: Fix and properly document @cachedmethod.cache_key handling.
- 2026-03-05_DescriptorBase
- 2024-07-15mru_cache
- 2021-12-21Fix #159: Pass self to @cachedmethod key function.
- 2021-12-18TTLCache