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.

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

1515 except KeyError:
1616 return False
1717 else:
18- return not (link.expire < self.__timer())
18+ return self.__timer() < link.expire
1919
2020 def __getitem__(self, key, cache_getitem=Cache.__getitem__):
2121 try:
2323 except KeyError:
2424 expired = False
2525 else:
26- expired = link.expire < self.__timer()
26+ expired = not (self.__timer() < link.expire)
2727 if expired:
2828 return self.__missing__(key)
2929 else:
4848 cache_delitem(self, key)
4949 link = self.__links.pop(key)
5050 link.unlink()
51- if link.expire < self.__timer():
51+ if not (self.__timer() < link.expire):
5252 raise KeyError(key)
5353
5454 def __iter__(self):
5757 while curr is not root:
5858 # "freeze" time for iterator access
5959 with self.__timer as time:
60- if not (curr.expire < time):
60+ if time < curr.expire:
6161 yield curr.key
6262 curr = curr.next
6363
6666 curr = root.next
6767 time = self.__timer()
6868 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):
7070 count -= 1
7171 curr = curr.next
7272 return count
110110 curr = root.next
111111 links = self.__links
112112 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):
114114 cache_delitem(self, curr.key)
115115 del links[curr.key]
116116 next = curr.next

The check that tells the two apart

failpass·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