mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
added dedicated test file and moved timed_cache to tools
This commit is contained in:
20
libp2p/tools/timed_cache/first_seen_cache.py
Normal file
20
libp2p/tools/timed_cache/first_seen_cache.py
Normal file
@ -0,0 +1,20 @@
|
||||
import time
|
||||
|
||||
from .base_timed_cache import (
|
||||
BaseTimedCache,
|
||||
)
|
||||
|
||||
|
||||
class FirstSeenCache(BaseTimedCache):
|
||||
"""Cache where expiry is set only when first added."""
|
||||
|
||||
def add(self, key: bytes) -> bool:
|
||||
with self.lock:
|
||||
if key in self.cache:
|
||||
return False
|
||||
self.cache[key] = int(time.time()) + self.ttl
|
||||
return True
|
||||
|
||||
def has(self, key: bytes) -> bool:
|
||||
with self.lock:
|
||||
return key in self.cache
|
||||
Reference in New Issue
Block a user