ForcePilot/backend/package/yuxi/channels/adapters/bluebubbles/reply_cache.py
Kris 6aa1d52a8b feat(bluebubbles): 实现完整的BlueBubbles适配器基础组件
新增BlueBubbles适配器全套核心工具类与服务,包含会话管理、消息去重、Webhook验证、缓存系统、账号配置解析、聊天消息处理等完整功能模块,支持iMessage消息收发、群管理、反应特效、语音合成等能力,提供完善的健康检查与配置校验流程。
2026-05-12 00:42:57 +08:00

39 lines
1.2 KiB
Python

from __future__ import annotations
import time
class ReplyCache:
MAX_ENTRIES = 512
TTL_SECONDS = 600
def __init__(self) -> None:
self._cache: dict[tuple[str, str], tuple[str, float]] = {}
def resolve(self, chat_guid: str, short_id: str) -> str | None:
key = (chat_guid, short_id)
entry = self._cache.get(key)
if entry is None:
return None
uuid, ts = entry
if time.monotonic() - ts > self.TTL_SECONDS:
del self._cache[key]
return None
return uuid
def store(self, chat_guid: str, short_id: str, full_uuid: str) -> None:
key = (chat_guid, short_id)
self._cache[key] = (full_uuid, time.monotonic())
self._trim()
def _trim(self) -> None:
if len(self._cache) > self.MAX_ENTRIES:
now = time.monotonic()
expired = [k for k, v in self._cache.items() if now - v[1] > self.TTL_SECONDS]
for k in expired:
del self._cache[k]
if len(self._cache) > self.MAX_ENTRIES:
oldest = sorted(self._cache.items(), key=lambda x: x[1][1])[: len(self._cache) - self.MAX_ENTRIES]
for k, _ in oldest:
del self._cache[k]