新增 IRC 渠道的完整扩展实现,包括客户端连接管理、消息收发与流式处理、安全策略与配对验证、消息去重与规范化、状态监控与健康探测、配置模型与账户管理、错误处理等模块。
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class IrcDedupeCache:
|
|
|
|
def __init__(self, maxsize: int = 5000, ttl_seconds: int = 300):
|
|
self._maxsize = maxsize
|
|
self._ttl = ttl_seconds
|
|
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)
|