2026-03-03 15:54:47 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-04-26 15:50:15 +08:00
|
|
|
from zhenxun.services.cache.bounded_ttl import BoundedTTLCache
|
2026-03-03 15:54:47 +08:00
|
|
|
from zhenxun.utils.pydantic_compat import dump_json_safely
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RenderResultMemoryCache:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
ttl_seconds: float,
|
|
|
|
|
max_items: int,
|
|
|
|
|
max_total_bytes: int | None = None,
|
|
|
|
|
):
|
|
|
|
|
self._ttl_seconds = max(ttl_seconds, 0.0)
|
|
|
|
|
self._max_items = max(max_items, 1)
|
|
|
|
|
self._max_total_bytes = (
|
|
|
|
|
max_total_bytes
|
|
|
|
|
if isinstance(max_total_bytes, int) and max_total_bytes > 0
|
|
|
|
|
else None
|
|
|
|
|
)
|
2026-04-26 15:50:15 +08:00
|
|
|
self._cache = BoundedTTLCache[str, bytes](
|
|
|
|
|
"RENDER_RESULT",
|
|
|
|
|
ttl_seconds=self._ttl_seconds,
|
|
|
|
|
max_items=self._max_items,
|
|
|
|
|
max_total_bytes=self._max_total_bytes,
|
|
|
|
|
)
|
2026-03-03 15:54:47 +08:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def build_key(payload: Any) -> str:
|
|
|
|
|
payload_text = dump_json_safely(
|
|
|
|
|
payload,
|
|
|
|
|
ensure_ascii=False,
|
|
|
|
|
sort_keys=True,
|
|
|
|
|
separators=(",", ":"),
|
|
|
|
|
)
|
|
|
|
|
return hashlib.sha256(payload_text.encode("utf-8")).hexdigest()
|
|
|
|
|
|
|
|
|
|
async def get(self, key: str) -> bytes | None:
|
2026-04-26 15:50:15 +08:00
|
|
|
return await self._cache.get(key)
|
2026-03-03 15:54:47 +08:00
|
|
|
|
|
|
|
|
async def set(self, key: str, value: bytes) -> None:
|
2026-04-26 15:50:15 +08:00
|
|
|
await self._cache.set(key, value)
|