From e0a08c9a2b429bd72e3957640785b3f6dd7bbdd5 Mon Sep 17 00:00:00 2001 From: acul71 Date: Sun, 20 Apr 2025 16:50:51 +0200 Subject: [PATCH] fix: test_timed_cache.py::test_readding_after_expiry --- libp2p/tools/timed_cache/first_seen_cache.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libp2p/tools/timed_cache/first_seen_cache.py b/libp2p/tools/timed_cache/first_seen_cache.py index 473af04b..f0e0d1c1 100644 --- a/libp2p/tools/timed_cache/first_seen_cache.py +++ b/libp2p/tools/timed_cache/first_seen_cache.py @@ -9,12 +9,24 @@ class FirstSeenCache(BaseTimedCache): """Cache where expiry is set only when first added.""" def add(self, key: bytes) -> bool: + now = int(time.time()) with self.lock: if key in self.cache: + # Check if the key is expired + if self.cache[key] <= now: + # Key is expired, update the expiry and treat as a new entry + self.cache[key] = now + self.ttl + return True return False - self.cache[key] = int(time.time()) + self.ttl + self.cache[key] = now + self.ttl return True def has(self, key: bytes) -> bool: + now = int(time.time()) with self.lock: - return key in self.cache + if key in self.cache: + # Check if key is expired + if self.cache[key] <= now: + return False + return True + return False