39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
|
|
import logging
|
||
|
|
import time
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class MessageDeduplicator:
|
||
|
|
def __init__(self, max_size: int = 10000, ttl_seconds: int = 300):
|
||
|
|
self._cache: dict[str, float] = {}
|
||
|
|
self._max_size = max_size
|
||
|
|
self._ttl_seconds = ttl_seconds
|
||
|
|
|
||
|
|
def is_duplicate(self, message_id: str) -> bool:
|
||
|
|
if not message_id:
|
||
|
|
return False
|
||
|
|
|
||
|
|
self._evict_expired()
|
||
|
|
|
||
|
|
if message_id in self._cache:
|
||
|
|
return True
|
||
|
|
|
||
|
|
self._cache[message_id] = time.monotonic()
|
||
|
|
self._trim_if_needed()
|
||
|
|
return False
|
||
|
|
|
||
|
|
def _evict_expired(self):
|
||
|
|
now = time.monotonic()
|
||
|
|
expired = [mid for mid, ts in self._cache.items() if now - ts > self._ttl_seconds]
|
||
|
|
for mid in expired:
|
||
|
|
del self._cache[mid]
|
||
|
|
|
||
|
|
def _trim_if_needed(self):
|
||
|
|
if len(self._cache) > self._max_size:
|
||
|
|
sorted_items = sorted(self._cache.items(), key=lambda x: x[1])
|
||
|
|
to_remove = len(self._cache) - self._max_size + 500
|
||
|
|
for mid, _ in sorted_items[:to_remove]:
|
||
|
|
del self._cache[mid]
|
||
|
|
logger.warning("ClickUp dedupe cache trimmed: removed %d entries", to_remove)
|