From dfbebc23f35d38a9a43ad7bba795f25f2653202c Mon Sep 17 00:00:00 2001 From: Thomas Kemmer Date: Thu, 14 Oct 2021 06:34:02 +0200 Subject: [PATCH] Fix #221: Change exact time of expiration in TTLCache. --- src/cachetools/__init__.py | 12 ++++++------ tests/test_ttl.py | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/cachetools/__init__.py b/src/cachetools/__init__.py index 42822f0..552abf1 100644 --- a/src/cachetools/__init__.py +++ b/src/cachetools/__init__.py @@ -358,7 +358,7 @@ def __contains__(self, key): except KeyError: return False else: - return not (link.expire < self.__timer()) + return self.__timer() < link.expire def __getitem__(self, key, cache_getitem=Cache.__getitem__): try: @@ -366,7 +366,7 @@ def __getitem__(self, key, cache_getitem=Cache.__getitem__): except KeyError: expired = False else: - expired = link.expire < self.__timer() + expired = not (self.__timer() < link.expire) if expired: return self.__missing__(key) else: @@ -391,7 +391,7 @@ def __delitem__(self, key, cache_delitem=Cache.__delitem__): cache_delitem(self, key) link = self.__links.pop(key) link.unlink() - if link.expire < self.__timer(): + if not (self.__timer() < link.expire): raise KeyError(key) def __iter__(self): @@ -400,7 +400,7 @@ def __iter__(self): while curr is not root: # "freeze" time for iterator access with self.__timer as time: - if not (curr.expire < time): + if time < curr.expire: yield curr.key curr = curr.next @@ -409,7 +409,7 @@ def __len__(self): curr = root.next time = self.__timer() count = len(self.__links) - while curr is not root and curr.expire < time: + while curr is not root and not (time < curr.expire): count -= 1 curr = curr.next return count @@ -453,7 +453,7 @@ def expire(self, time=None): curr = root.next links = self.__links cache_delitem = Cache.__delitem__ - while curr is not root and curr.expire < time: + while curr is not root and not (time < curr.expire): cache_delitem(self, curr.key) del links[curr.key] next = curr.next diff --git a/tests/test_ttl.py b/tests/test_ttl.py index 6e51a59..db5107d 100644 --- a/tests/test_ttl.py +++ b/tests/test_ttl.py @@ -1,3 +1,4 @@ +import math import unittest from cachetools import TTLCache @@ -20,7 +21,7 @@ def tick(self): class TTLTestCache(TTLCache): - def __init__(self, maxsize, ttl=0, **kwargs): + def __init__(self, maxsize, ttl=math.inf, **kwargs): TTLCache.__init__(self, maxsize, ttl=ttl, timer=Timer(), **kwargs) @@ -29,9 +30,9 @@ class TTLCacheTest(unittest.TestCase, CacheTestMixin): Cache = TTLTestCache def test_ttl(self): - cache = TTLCache(maxsize=2, ttl=1, timer=Timer()) + cache = TTLCache(maxsize=2, ttl=2, timer=Timer()) self.assertEqual(0, cache.timer()) - self.assertEqual(1, cache.ttl) + self.assertEqual(2, cache.ttl) cache[1] = 1 self.assertEqual({1}, set(cache)) @@ -84,7 +85,7 @@ def test_ttl(self): del cache[3] def test_ttl_lru(self): - cache = TTLCache(maxsize=2, ttl=0, timer=Timer()) + cache = TTLCache(maxsize=2, ttl=1, timer=Timer()) cache[1] = 1 cache[2] = 2 @@ -112,10 +113,10 @@ def test_ttl_lru(self): self.assertEqual(cache[5], 5) def test_ttl_expire(self): - cache = TTLCache(maxsize=3, ttl=2, timer=Timer()) + cache = TTLCache(maxsize=3, ttl=3, timer=Timer()) with cache.timer as time: self.assertEqual(time, cache.timer()) - self.assertEqual(2, cache.ttl) + self.assertEqual(3, cache.ttl) cache[1] = 1 cache.timer.tick() @@ -159,7 +160,7 @@ def test_ttl_expire(self): self.assertNotIn(3, cache) def test_ttl_atomic(self): - cache = TTLCache(maxsize=1, ttl=1, timer=Timer(auto=True)) + cache = TTLCache(maxsize=1, ttl=2, timer=Timer(auto=True)) cache[1] = 1 self.assertEqual(1, cache[1]) cache[1] = 1 @@ -173,8 +174,8 @@ def test_ttl_atomic(self): self.assertEqual(0, len(cache)) def test_ttl_tuple_key(self): - cache = TTLCache(maxsize=1, ttl=0, timer=Timer()) - self.assertEqual(0, cache.ttl) + cache = TTLCache(maxsize=1, ttl=1, timer=Timer()) + self.assertEqual(1, cache.ttl) cache[(1, 2, 3)] = 42 self.assertEqual(42, cache[(1, 2, 3)])