26 lines
746 B
Python
26 lines
746 B
Python
|
|
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) -> None:
|
||
|
|
entry = {
|
||
|
|
"channel": target,
|
||
|
|
"content": content,
|
||
|
|
"timestamp": time.time(),
|
||
|
|
}
|
||
|
|
cache_key = f"{target}:{len(self._cache)}"
|
||
|
|
self._cache[cache_key] = entry
|
||
|
|
while len(self._cache) > self._max_size:
|
||
|
|
self._cache.popitem(last=False)
|
||
|
|
|
||
|
|
def get_all(self) -> list[dict[str, Any]]:
|
||
|
|
return list(self._cache.values())
|