ForcePilot/backend/package/yuxi/channels/adapters/twitch/outbound_cache.py

35 lines
1.1 KiB
Python
Raw Normal View History

from __future__ import annotations
import time
from collections import OrderedDict
from typing import Any
class OutboundCacheManager:
def __init__(self, max_size: int = 500):
self._cache: OrderedDict[str, dict[str, Any]] = OrderedDict()
self._max_size = max_size
def record(self, target: str, content: str, message_id: str | None = None) -> str:
entry = {
"channel": target,
"content": content,
"timestamp": time.time(),
}
cache_key = message_id or f"{target}:{len(self._cache)}"
if message_id:
entry["message_id"] = message_id
self._cache[cache_key] = entry
while len(self._cache) > self._max_size:
self._cache.popitem(last=False)
return cache_key
def get_all(self) -> list[dict[str, Any]]:
return list(self._cache.values())
def find_by_message_id(self, message_id: str) -> dict[str, Any] | None:
for entry in self._cache.values():
if entry.get("message_id") == message_id:
return entry
return None