from __future__ import annotations import time from collections import OrderedDict class IrcDedupeCache: def __init__(self, maxsize: int = 5000, ttl_seconds: int = 300): self._maxsize = maxsize self._ttl = ttl_seconds self._cache: OrderedDict[str, float] = OrderedDict() def __contains__(self, key: str) -> bool: now = time.monotonic() if key in self._cache: if now - self._cache[key] < self._ttl: self._cache.move_to_end(key) return True del self._cache[key] return False def add(self, key: str) -> None: now = time.monotonic() self._evict_expired(now) self._cache[key] = now self._cache.move_to_end(key) if len(self._cache) > self._maxsize: self._cache.popitem(last=False) def _evict_expired(self, now: float) -> None: expired = [k for k, ts in self._cache.items() if now - ts >= self._ttl] for k in expired: del self._cache[k] def __len__(self) -> int: return len(self._cache)