Files
py-libp2p/libp2p/timed_cache/last_seen_cache.py
2025-03-17 09:27:13 -06:00

23 lines
559 B
Python

import time
from .basic_time_cache import (
TimedCache,
)
class LastSeenCache(TimedCache):
"""Cache where expiry is updated on every access."""
def add(self, key: bytes) -> bool:
with self.lock:
is_new = key not in self.cache
self.cache[key] = int(time.time()) + self.ttl
return is_new
def has(self, key: bytes) -> bool:
with self.lock:
if key in self.cache:
self.cache[key] = int(time.time()) + self.ttl
return True
return False