新增 Matrix 渠道扩展,支持在 Yuxi 平台中集成 Matrix 去中心化通讯协议。 包含以下功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 端到端加密 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - room_resolver: 房间解析 - dm_tracker: 私聊追踪 - rate_limiter: 速率限制 - actions: 动作处理 - constants: 常量定义 - utils: 工具函数 - types: 类型定义
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class DedupeCache:
|
|
def __init__(self, maxsize: int = 5000, ttl_ms: int = 300_000):
|
|
self._maxsize = maxsize
|
|
self._ttl = ttl_ms / 1000
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
|
|
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._evict_expired(now)
|
|
self._cache[key] = now
|
|
self._cache.move_to_end(key)
|
|
if len(self._cache) > self._maxsize:
|
|
self._cache.popitem(last=False)
|
|
|
|
def _evict_expired(self, now: float) -> None:
|
|
expired = [k for k, ts in self._cache.items() if now - ts >= self._ttl]
|
|
for k in expired:
|
|
del self._cache[k]
|
|
|
|
def __len__(self) -> int:
|
|
return len(self._cache) |