One function
TTLCache in tkem/cachetools
The author described this change as “Fix #221: Change exact time of expiration in TTLCache.”. It counts as a record because the check below fails on the code as it stood at 4d11ef92d and passes on 91aa4c63f, with nothing else changed between the two runs.
Projecttkem/cachetools
Fix saved2021-12-18
Sharing licenceMIT · LICENSE
Change size+6 −6
What the code was meant to do, written into the code itself as a docstring
LRU Cache implementation with per-item time-to-live (TTL) value.
The change
| 15 | 15 | except KeyError: | |
| 16 | 16 | return False | |
| 17 | 17 | else: | |
| 18 | - | return not (link.expire < self.__timer()) | |
| 18 | + | return self.__timer() < link.expire | |
| 19 | 19 | ||
| 20 | 20 | def __getitem__(self, key, cache_getitem=Cache.__getitem__): | |
| 21 | 21 | try: | |
| ⋯ | |||
| 23 | 23 | except KeyError: | |
| 24 | 24 | expired = False | |
| 25 | 25 | else: | |
| 26 | - | expired = link.expire < self.__timer() | |
| 26 | + | expired = not (self.__timer() < link.expire) | |
| 27 | 27 | if expired: | |
| 28 | 28 | return self.__missing__(key) | |
| 29 | 29 | else: | |
| ⋯ | |||
| 48 | 48 | cache_delitem(self, key) | |
| 49 | 49 | link = self.__links.pop(key) | |
| 50 | 50 | link.unlink() | |
| 51 | - | if link.expire < self.__timer(): | |
| 51 | + | if not (self.__timer() < link.expire): | |
| 52 | 52 | raise KeyError(key) | |
| 53 | 53 | ||
| 54 | 54 | def __iter__(self): | |
| ⋯ | |||
| 57 | 57 | while curr is not root: | |
| 58 | 58 | # "freeze" time for iterator access | |
| 59 | 59 | with self.__timer as time: | |
| 60 | - | if not (curr.expire < time): | |
| 60 | + | if time < curr.expire: | |
| 61 | 61 | yield curr.key | |
| 62 | 62 | curr = curr.next | |
| 63 | 63 | ||
| ⋯ | |||
| 66 | 66 | curr = root.next | |
| 67 | 67 | time = self.__timer() | |
| 68 | 68 | count = len(self.__links) | |
| 69 | - | while curr is not root and curr.expire < time: | |
| 69 | + | while curr is not root and not (time < curr.expire): | |
| 70 | 70 | count -= 1 | |
| 71 | 71 | curr = curr.next | |
| 72 | 72 | return count | |
| ⋯ | |||
| 110 | 110 | curr = root.next | |
| 111 | 111 | links = self.__links | |
| 112 | 112 | cache_delitem = Cache.__delitem__ | |
| 113 | - | while curr is not root and curr.expire < time: | |
| 113 | + | while curr is not root and not (time < curr.expire): | |
| 114 | 114 | cache_delitem(self, curr.key) | |
| 115 | 115 | del links[curr.key] | |
| 116 | 116 | next = curr.next | |
The check that tells the two apart
fail→pass·tests/test_ttl.py::TTLCacheTest::test_ttl
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 it4d11ef92d57e9d838e9160cb5c023baff099c60a
Broken version dated2021-12-18
Modulecachetools.__init__
Units changedTTLCache
Fingerprint769efe841cd73a9f
Checked2026-08-17 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-08-18Fix #292, fix #205, fix #103: TTLCache.expire() returns iterable of expired (key, value) pairs.
- 2024-07-15mru_cache
- 2021-12-21Fix #159: Pass self to @cachedmethod key function.