import time class CommentDeduplicator: def __init__(self, max_size: int = 50_000, ttl_seconds: int = 600): self._max_size = max_size self._ttl = ttl_seconds self._cache: dict[str, float] = {} def is_duplicate(self, comment_id: str) -> bool: self._evict_expired() return comment_id in self._cache def mark(self, comment_id: str): self._evict_expired() self._cache[comment_id] = time.time() self._evict_oldest_if_needed() def _evict_expired(self): now = time.time() expired = [cid for cid, ts in self._cache.items() if now - ts > self._ttl] for cid in expired: del self._cache[cid] def _evict_oldest_if_needed(self): while len(self._cache) > self._max_size: oldest = min(self._cache, key=self._cache.get) del self._cache[oldest] def clear(self): self._cache.clear() def __len__(self) -> int: return len(self._cache)