ForcePilot/backend/package/yuxi/channel/extensions/webex/dedupe.py

30 lines
838 B
Python
Raw Normal View History

import time
from collections import OrderedDict
class MessageDeduplicator:
def __init__(self, max_size: int = 10000, ttl_seconds: int = 300):
self._max_size = max_size
self._ttl_seconds = ttl_seconds
self._cache: OrderedDict[str, float] = OrderedDict()
def is_duplicate(self, msg_id: str) -> bool:
now = time.time()
self._evict_expired(now)
if msg_id in self._cache:
return True
self._cache[msg_id] = now
if len(self._cache) > self._max_size:
self._cache.popitem(last=False)
return False
def _evict_expired(self, now: float):
expire_before = now - self._ttl_seconds
expired = [mid for mid, ts in self._cache.items() if ts < expire_before]
for mid in expired:
self._cache.pop(mid, None)