mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
fix: test_timed_cache.py::test_readding_after_expiry
This commit is contained in:
@ -9,12 +9,24 @@ class FirstSeenCache(BaseTimedCache):
|
|||||||
"""Cache where expiry is set only when first added."""
|
"""Cache where expiry is set only when first added."""
|
||||||
|
|
||||||
def add(self, key: bytes) -> bool:
|
def add(self, key: bytes) -> bool:
|
||||||
|
now = int(time.time())
|
||||||
with self.lock:
|
with self.lock:
|
||||||
if key in self.cache:
|
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
|
return False
|
||||||
self.cache[key] = int(time.time()) + self.ttl
|
self.cache[key] = now + self.ttl
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def has(self, key: bytes) -> bool:
|
def has(self, key: bytes) -> bool:
|
||||||
|
now = int(time.time())
|
||||||
with self.lock:
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user