mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 13:50:00 +08:00
✨ feat(hook): 增强认证钩子和运行时缓存管理 (#2106)
* ✨ feat(hook): 增强认证钩子和运行时缓存管理 ``` ♻ refactor(hook): 移除未使用的配置项并优化缓存设置 移除 AUTH_HOOKS_CONCURRENCY_LIMIT 配置项,该配置项不再使用 ✨feat(auth_ban): 简化缓存配置并添加实体参数支持 将 BAN_CACHE_TTL 相关配置从动态配置改为常量定义, 移除复杂的 TTL 值转换逻辑,并为 auth_ban 函数添加可选的 entity 参数以支持外部传入实体信息 ♻ refactor(auth_limit): 移除未使用的配置依赖 移除 AUTH_LIMIT_NOTICE_CD 配置项,直接使用常量值 2 作为限制通知冷却时间 📦 依赖更新: update playwright dependency to version 1.57.0 in pyproject.toml and requirements.txt ``` * ✨ feat(auth_checker): 增强插件模块处理和预过滤逻辑,支持用户插件兼容性 * ✨ feat(http_utils): 添加内容缓存机制以优化HTTP响应处理 * ✨ feat(cache): 添加群组插件设置视图缓存类型并更新相关逻辑 * ✨ feat(renderer): 优化渲染引擎,增加内存缓存管理和HTML文档处理逻辑 * ✨ feat(sign_in): 添加好感度排行和好感度总排行命令 * ✨ feat(renderer): 增强浏览器实例管理和模板预处理,支持历史 include 语法兼容 * ✨ feat(renderer): 优化 Playwright 环境检查逻辑,增加结果缓存以提高性能 * ✨ feat(renderer): 增强模板文件渲染策略,优化资产加载路径处理 * 🚨 auto fix by pre-commit hooks * ✨ feat(log): 增加日志内容安全序列化,避免超长 base64 等污染日志 ✨ feat(log_sanitizer): 添加对嵌入超长 base64/data URI 的清理功能 ✨ feat(auth_checker): 添加 Alconna 快捷方式缓存检查,优化路由匹配逻辑 * feat(renderer): 添加渲染结果内存缓存功能以优化性能 feat(help): 实现帮助菜单图像缓存机制 feat(sign_in): 更新HTML卡片生成以支持动画禁用和剪裁 feat(superuser): 在启动时预热超级用户帮助缓存 feat(theme): 优化主题管理器的资源解析缓存 * 🚨 auto fix by pre-commit hooks * feat(renderer): 增加全页面视口最大宽度限制并优化内容尺寸计算 --------- Co-authored-by: ATTomatoo <1126160939@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com>
This commit is contained in:
co-authored by
ATTomatoo
pre-commit-ci[bot]
HibiKier
parent
51f4773e14
commit
ce94f63d9a
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
import asyncio
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
import inspect
|
||||
@@ -313,8 +314,12 @@ class AssetResolutionService:
|
||||
def resolve_asset_uri(self, asset_path: str, current_template_name: str) -> str:
|
||||
hot_reload = Config.get_config("UI", "HOT_RELOAD", False)
|
||||
cache_key = (asset_path, current_template_name)
|
||||
if not hot_reload and cache_key in self.theme_manager._asset_resolution_cache:
|
||||
return self.theme_manager._asset_resolution_cache[cache_key]
|
||||
if not hot_reload:
|
||||
if cached_uri := self.theme_manager._get_lru_entry(
|
||||
self.theme_manager._asset_resolution_cache,
|
||||
cache_key,
|
||||
):
|
||||
return cached_uri
|
||||
request = AssetRequest(
|
||||
asset_path=asset_path,
|
||||
template_name=current_template_name,
|
||||
@@ -324,7 +329,7 @@ class AssetResolutionService:
|
||||
if result_path := resolver.resolve(request):
|
||||
uri = result_path.absolute().as_uri()
|
||||
if not hot_reload:
|
||||
self.theme_manager._asset_resolution_cache[cache_key] = uri
|
||||
self.theme_manager._set_asset_resolution_cache(cache_key, uri)
|
||||
return uri
|
||||
logger.warning(
|
||||
f"资源文件未找到: '{asset_path}' (在 '{current_template_name}' 中)"
|
||||
@@ -333,6 +338,10 @@ class AssetResolutionService:
|
||||
|
||||
|
||||
class ThemeManager:
|
||||
_ASSET_RESOLUTION_CACHE_MAX = 2048
|
||||
_GLOBAL_TEMPLATE_CACHE_MAX = 512
|
||||
_COMPONENT_DEP_CACHE_MAX = 512
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
主题管理器,负责UI主题的加载、解析和模板渲染。
|
||||
@@ -347,11 +356,58 @@ class ThemeManager:
|
||||
self.current_theme_context: dict[str, Any] = {}
|
||||
self.current_default_palette: dict[str, Any] = {}
|
||||
|
||||
self._asset_resolution_cache: dict[tuple[str, str], str] = {}
|
||||
self._global_template_cache: dict[str, str] = {}
|
||||
self._component_dependency_cache: dict[
|
||||
self._asset_resolution_cache: OrderedDict[tuple[str, str], str] = OrderedDict()
|
||||
self._global_template_cache: OrderedDict[str, str] = OrderedDict()
|
||||
self._component_dependency_cache: OrderedDict[
|
||||
tuple[type, str, str | None], ComponentDependency
|
||||
] = {}
|
||||
] = OrderedDict()
|
||||
|
||||
@staticmethod
|
||||
def _get_lru_entry(cache: OrderedDict, key: Any) -> Any:
|
||||
value = cache.get(key)
|
||||
if value is not None:
|
||||
cache.move_to_end(key)
|
||||
return value
|
||||
|
||||
@staticmethod
|
||||
def _set_lru_entry(
|
||||
cache: OrderedDict,
|
||||
key: Any,
|
||||
value: Any,
|
||||
max_items: int,
|
||||
) -> None:
|
||||
cache[key] = value
|
||||
cache.move_to_end(key)
|
||||
while len(cache) > max_items:
|
||||
cache.popitem(last=False)
|
||||
|
||||
def _set_asset_resolution_cache(self, key: tuple[str, str], value: str) -> None:
|
||||
self._set_lru_entry(
|
||||
self._asset_resolution_cache,
|
||||
key,
|
||||
value,
|
||||
self._ASSET_RESOLUTION_CACHE_MAX,
|
||||
)
|
||||
|
||||
def _set_global_template_cache(self, key: str, value: str) -> None:
|
||||
self._set_lru_entry(
|
||||
self._global_template_cache,
|
||||
key,
|
||||
value,
|
||||
self._GLOBAL_TEMPLATE_CACHE_MAX,
|
||||
)
|
||||
|
||||
def _set_component_dependency_cache(
|
||||
self,
|
||||
key: tuple[type, str, str | None],
|
||||
value: ComponentDependency,
|
||||
) -> None:
|
||||
self._set_lru_entry(
|
||||
self._component_dependency_cache,
|
||||
key,
|
||||
value,
|
||||
self._COMPONENT_DEP_CACHE_MAX,
|
||||
)
|
||||
|
||||
def bind_template_engine(self, env: Environment):
|
||||
"""绑定模板引擎环境,用于Manifest加载和asset解析"""
|
||||
@@ -429,9 +485,15 @@ class ThemeManager:
|
||||
"""为独立模板创建一个专用的 asset loader。"""
|
||||
|
||||
def asset_loader(asset_path: str) -> str:
|
||||
full_path = local_base_path / asset_path
|
||||
if full_path.exists():
|
||||
return full_path.absolute().as_uri()
|
||||
clean_path = asset_path[2:] if asset_path.startswith("./") else asset_path
|
||||
candidate_paths = [
|
||||
local_base_path / asset_path,
|
||||
local_base_path / clean_path,
|
||||
local_base_path / "assets" / clean_path,
|
||||
]
|
||||
for full_path in candidate_paths:
|
||||
if full_path.exists():
|
||||
return full_path.absolute().as_uri()
|
||||
return ""
|
||||
|
||||
return asset_loader
|
||||
@@ -575,10 +637,12 @@ class ThemeManager:
|
||||
variant = getattr(component, "variant", None)
|
||||
cache_key = f"{component_path_base}::{variant or 'default'}"
|
||||
|
||||
if not hot_reload and (
|
||||
cached_path := self._global_template_cache.get(cache_key)
|
||||
):
|
||||
return cached_path
|
||||
if not hot_reload:
|
||||
if cached_path := self._get_lru_entry(
|
||||
self._global_template_cache,
|
||||
cache_key,
|
||||
):
|
||||
return cached_path
|
||||
|
||||
if not hot_reload and (
|
||||
cached_path := context.resolved_template_paths.get(cache_key)
|
||||
@@ -630,7 +694,7 @@ class ThemeManager:
|
||||
logger.debug(f"解析到模板路径: '{path}'")
|
||||
if not hot_reload:
|
||||
context.resolved_template_paths[cache_key] = path
|
||||
self._global_template_cache[cache_key] = path
|
||||
self._set_global_template_cache(cache_key, path)
|
||||
return path
|
||||
except TemplateNotFound:
|
||||
continue
|
||||
@@ -731,8 +795,8 @@ class DependencyCollector:
|
||||
|
||||
cached_dep = None
|
||||
if not hot_reload:
|
||||
cached_dep = context.theme_manager._component_dependency_cache.get(
|
||||
cache_key
|
||||
cached_dep = context.theme_manager._get_lru_entry(
|
||||
context.theme_manager._component_dependency_cache, cache_key
|
||||
)
|
||||
|
||||
if cached_dep:
|
||||
@@ -798,7 +862,10 @@ class DependencyCollector:
|
||||
new_dep.asset_styles = component.get_required_styles()
|
||||
|
||||
if not hot_reload:
|
||||
context.theme_manager._component_dependency_cache[cache_key] = new_dep
|
||||
context.theme_manager._set_component_dependency_cache(
|
||||
cache_key,
|
||||
new_dep,
|
||||
)
|
||||
|
||||
context.collected_inline_css.extend(cached_css_results)
|
||||
context.collected_scripts.update(new_dep.scripts)
|
||||
|
||||
Reference in New Issue
Block a user