ForcePilot/backend/package/yuxi/channel/extensions/zalouser/dedupe.py
Kris d5e36d33b7 feat(channel): 添加 Zalo OA、Zoom Chat 和 Zulip 渠道扩展
新增 Zalo OA、Zoom Chat、Zulip 三个渠道扩展。

Zalo OA 渠道扩展主要模块:sidecar_client, config, gateway, outbound, streaming, pairing, security, auth, dedupe, directory, monitor, status, session, reactions, tools

Zoom Chat 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, crypto, dedupe, actions, media, mentions, monitor, status, session, reactions, threading

Zulip 渠道扩展主要模块:client, config, gateway, outbound, streaming, pairing, security, monitor, status
2026-05-21 12:06:26 +08:00

42 lines
1.2 KiB
Python

from __future__ import annotations
import time
from collections import OrderedDict
class DedupeCache:
"""LRU + TTL dedup cache for ZaloUser messages.
Uses msg_id + cli_msg_id dual-ID key. TTL defaults to 10 minutes.
"""
def __init__(self, maxsize: int = 5000, ttl_ms: int = 600_000):
self._maxsize = maxsize
self._ttl = ttl_ms / 1000.0
self._cache: OrderedDict[str, float] = OrderedDict()
@staticmethod
def make_key(msg_id: str, cli_msg_id: str | None = None) -> str:
if cli_msg_id:
return f"{msg_id}:{cli_msg_id}"
return msg_id
def __contains__(self, key: str) -> bool:
now = time.monotonic()
if key in self._cache:
if now - self._cache[key] < self._ttl:
self._cache.move_to_end(key)
return True
del self._cache[key]
return False
def add(self, key: str) -> None:
now = time.monotonic()
self._cache[key] = now
self._cache.move_to_end(key)
if len(self._cache) > self._maxsize:
self._cache.popitem(last=False)
def __len__(self) -> int:
return len(self._cache)