完成Help Scout渠道的完整功能实现,包含API客户端、webhook处理、轮询同步、自动回复、工单标签分配、客户资料管理等核心能力,附带完整的配置Schema与插件元信息
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
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, dedup_key: str) -> bool:
|
|
if not dedup_key:
|
|
return False
|
|
|
|
self._evict_expired()
|
|
|
|
if dedup_key in self._cache:
|
|
return True
|
|
|
|
self._cache[dedup_key] = time.monotonic()
|
|
self._trim_if_needed()
|
|
return False
|
|
|
|
def _evict_expired(self):
|
|
now = time.monotonic()
|
|
expired = [k for k, ts in self._cache.items() if now - ts > self._ttl_seconds]
|
|
for k in expired:
|
|
del self._cache[k]
|
|
|
|
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 k, _ in sorted_items[:to_remove]:
|
|
del self._cache[k]
|