Files
zhenxun_bot/zhenxun/services/renderer/result_cache.py
T
5d92ccd3b0 性能优化 (#2126)
* 性能优化

* 代码改进

* 优化浏览器代际切换逻辑

* 统一缓存与生命周期

* 添加aiomysql依赖

* 优化插件路径处理逻辑,简化条件判断;在虚拟环境包管理器中添加编码和错误处理参数以增强稳定性

* 🚨 auto fix by pre-commit hooks

* 优化Windows下的关闭逻辑

* 代码优化

* bugfix:修复配置重载问题

* bugfix:修复插件加载启动竞态问题

* 收敛事件入口和权限上下文

* 优化 Windows launcher 关闭重启兜底

---------

Co-authored-by: HibiKier <775757368@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-04-26 15:50:15 +08:00

46 lines
1.3 KiB
Python

from __future__ import annotations
import hashlib
from typing import Any
from zhenxun.services.cache.bounded_ttl import BoundedTTLCache
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
)
self._cache = BoundedTTLCache[str, bytes](
"RENDER_RESULT",
ttl_seconds=self._ttl_seconds,
max_items=self._max_items,
max_total_bytes=self._max_total_bytes,
)
@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:
return await self._cache.get(key)
async def set(self, key: str, value: bytes) -> None:
await self._cache.set(key, value)