实现完整的Freshdesk和Freshchat集成支持,包含会话守卫、错误定义、消息去重、配置管理、webhook处理、出站消息发送、状态监控、安全校验、配对功能和流式回复支持
36 lines
874 B
Python
36 lines
874 B
Python
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class MessageDeduplicator:
|
|
MAX_SIZE = 10000
|
|
TTL_SECONDS = 60
|
|
|
|
def __init__(self, max_size: int = MAX_SIZE, ttl: int = TTL_SECONDS):
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
self._max_size = max_size
|
|
self._ttl = ttl
|
|
|
|
def is_duplicate(self, msg_id: str) -> bool:
|
|
if not msg_id:
|
|
return False
|
|
|
|
now = time.time()
|
|
expired = [k for k, ts in self._cache.items() if now - ts > self._ttl]
|
|
for k in expired:
|
|
del self._cache[k]
|
|
|
|
if msg_id in self._cache:
|
|
return True
|
|
|
|
self._cache[msg_id] = now
|
|
self._cache.move_to_end(msg_id)
|
|
|
|
while len(self._cache) > self._max_size:
|
|
self._cache.popitem(last=False)
|
|
|
|
return False
|
|
|
|
def reset(self):
|
|
self._cache.clear()
|