from __future__ import annotations import time import threading from collections import OrderedDict DEFAULT_TTL_SECONDS = 300 DEFAULT_MAX_ENTRIES = 10000 class MSTeamsDedupeStore: def __init__(self, ttl_seconds: int = DEFAULT_TTL_SECONDS, max_entries: int = DEFAULT_MAX_ENTRIES): self._ttl_seconds = ttl_seconds self._max_entries = max_entries self._lock = threading.Lock() self._store: OrderedDict[str, float] = OrderedDict() def is_duplicate(self, key: str) -> bool: now = time.monotonic() with self._lock: self._evict_expired(now) return key in self._store def mark_seen(self, key: str) -> None: now = time.monotonic() with self._lock: self._store[key] = now self._store.move_to_end(key) self._evict_expired(now) def reset(self) -> None: with self._lock: self._store.clear() def _evict_expired(self, now: float) -> None: cutoff = now - self._ttl_seconds expired = [k for k, ts in self._store.items() if ts < cutoff] for k in expired: self._store.pop(k, None) while len(self._store) > self._max_entries: self._store.popitem(last=False) @property def ttl_seconds(self) -> int: return self._ttl_seconds @property def max_entries(self) -> int: return self._max_entries