from __future__ import annotations import logging import time from collections import OrderedDict from yuxi.channel.protocols import DedupeProtocol logger = logging.getLogger(__name__) class JiraDeduplicator(DedupeProtocol): def __init__(self, max_entries: int = 5000, ttl_seconds: int = 86400): self._cache: OrderedDict[str, float] = OrderedDict() self._max_entries = max_entries self._ttl_seconds = ttl_seconds @property def ttl_seconds(self) -> int: return self._ttl_seconds @property def max_entries(self) -> int: return self._max_entries def is_duplicate(self, key: str) -> bool: if not key: return False now = time.monotonic() self._evict_expired(now) if key in self._cache: return True self._cache[key] = now while len(self._cache) > self._max_entries: self._cache.popitem(last=False) return False def mark_seen(self, key: str) -> None: if not key: return now = time.monotonic() self._evict_expired(now) self._cache[key] = now def _evict_expired(self, now: float): expired = [k for k, v in self._cache.items() if now - v > self._ttl_seconds] for k in expired: del self._cache[k] def reset(self): self._cache.clear() _deduplicators: dict[str, JiraDeduplicator] = {} def get_deduplicator(account_id: str) -> JiraDeduplicator: if account_id not in _deduplicators: _deduplicators[account_id] = JiraDeduplicator() return _deduplicators[account_id]