Skip to content

Commit

Permalink
Fix #221: Change exact time of expiration in TTLCache.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Oct 14, 2021
1 parent 2fd87a9 commit dfbebc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/cachetools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,15 @@ 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:
link = self.__getlink(key)
except KeyError:
expired = False
else:
expired = link.expire < self.__timer()
expired = not (self.__timer() < link.expire)
if expired:
return self.__missing__(key)
else:
Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions tests/test_ttl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import unittest

from cachetools import TTLCache
Expand All @@ -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)


Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)])
Expand Down

0 comments on commit dfbebc2

Please sign in to comment.