✨ 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:
ManyManyTomato
2026-03-03 15:54:47 +08:00
committed by GitHub
co-authored by ATTomatoo pre-commit-ci[bot] HibiKier
parent 51f4773e14
commit ce94f63d9a
30 changed files with 2392 additions and 638 deletions
+35 -144
View File
@@ -8,7 +8,6 @@ import time
from typing import TYPE_CHECKING, Any, ClassVar
import uuid
from zhenxun.configs.config import Config
from zhenxun.services.cache.config import CacheMode
from zhenxun.services.log import logger
from zhenxun.utils.enum import LimitCheckType, LimitWatchType, PluginLimitType
@@ -19,108 +18,23 @@ if TYPE_CHECKING:
LOG_COMMAND = "RuntimeCache"
Config.add_plugin_config(
"hook",
"PLUGININFO_MEM_REFRESH_INTERVAL",
1800,
help="plugin info memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"BAN_MEM_REFRESH_INTERVAL",
900,
help="ban memory cache full refresh seconds",
)
Config.add_plugin_config(
"hook",
"BAN_MEM_CLEAN_INTERVAL",
900,
help="ban memory cache cleanup seconds",
)
Config.add_plugin_config(
"hook",
"BAN_MEM_CLEANUP_DB",
True,
help="delete expired ban records from database",
)
Config.add_plugin_config(
"hook",
"BAN_MEM_NEGATIVE_TTL",
5,
help="ban memory negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"BOT_MEM_REFRESH_INTERVAL",
900,
help="bot memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"BOT_MEM_NEGATIVE_TTL",
60,
help="bot memory negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"GROUP_MEM_REFRESH_INTERVAL",
900,
help="group memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"GROUP_MEM_NEGATIVE_TTL",
60,
help="group memory negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"LEVEL_MEM_REFRESH_INTERVAL",
900,
help="level memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"LEVEL_MEM_NEGATIVE_TTL",
60,
help="level memory negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"TASK_MEM_REFRESH_INTERVAL",
900,
help="task info memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"TASK_MEM_NEGATIVE_TTL",
60,
help="task info negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"LIMIT_MEM_REFRESH_INTERVAL",
900,
help="plugin limit memory cache refresh seconds",
)
Config.add_plugin_config(
"hook",
"LIMIT_MEM_NEGATIVE_TTL",
30,
help="plugin limit negative cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"RUNTIME_CACHE_SYNC_ENABLED",
True,
help="enable redis pubsub runtime cache sync",
)
Config.add_plugin_config(
"hook",
"RUNTIME_CACHE_SYNC_CHANNEL",
"ZHENXUN_RUNTIME_CACHE_SYNC",
help="redis pubsub channel for runtime cache sync",
)
PLUGININFO_MEM_REFRESH_INTERVAL = 300
BAN_MEM_REFRESH_INTERVAL = 60
BAN_MEM_CLEAN_INTERVAL = 60
BAN_MEM_CLEANUP_DB = True
BAN_MEM_NEGATIVE_TTL = 5
BOT_MEM_REFRESH_INTERVAL = 60
BOT_MEM_NEGATIVE_TTL = 60
GROUP_MEM_REFRESH_INTERVAL = 60
GROUP_MEM_NEGATIVE_TTL = 60
LEVEL_MEM_REFRESH_INTERVAL = 120
LEVEL_MEM_NEGATIVE_TTL = 60
TASK_MEM_REFRESH_INTERVAL = 900
TASK_MEM_NEGATIVE_TTL = 60
LIMIT_MEM_REFRESH_INTERVAL = 60
LIMIT_MEM_NEGATIVE_TTL = 30
RUNTIME_CACHE_SYNC_ENABLED = True
RUNTIME_CACHE_SYNC_CHANNEL = "ZHENXUN_RUNTIME_CACHE_SYNC"
def _coerce_int(value, default: int) -> int:
@@ -496,7 +410,7 @@ class RuntimeCacheSync:
@classmethod
def _sync_enabled(cls) -> bool:
enabled = bool(Config.get_config("hook", "RUNTIME_CACHE_SYNC_ENABLED", True))
enabled = RUNTIME_CACHE_SYNC_ENABLED
return enabled and _redis_enabled()
@classmethod
@@ -518,11 +432,7 @@ class RuntimeCacheSync:
return
port = _coerce_int(_env_get("REDIS_PORT"), 6379)
password = _env_get("REDIS_PASSWORD")
cls._channel = str(
Config.get_config(
"hook", "RUNTIME_CACHE_SYNC_CHANNEL", "ZHENXUN_RUNTIME_CACHE_SYNC"
)
)
cls._channel = RUNTIME_CACHE_SYNC_CHANNEL
try:
cls._redis = redis_async.Redis(
host=host,
@@ -730,10 +640,7 @@ class PluginInfoMemoryCache:
@classmethod
def start_refresh_task(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "PLUGININFO_MEM_REFRESH_INTERVAL", 300),
300,
)
interval = PLUGININFO_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -763,7 +670,7 @@ class BotMemoryCache:
@classmethod
def _negative_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "BOT_MEM_NEGATIVE_TTL", 60), 60)
return BOT_MEM_NEGATIVE_TTL
@classmethod
def _is_negative(cls, bot_id: str) -> bool:
@@ -887,9 +794,7 @@ class BotMemoryCache:
@classmethod
def start_tasks(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "BOT_MEM_REFRESH_INTERVAL", 60), 60
)
interval = BOT_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -929,7 +834,7 @@ class GroupMemoryCache:
@classmethod
def _negative_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "GROUP_MEM_NEGATIVE_TTL", 60), 60)
return GROUP_MEM_NEGATIVE_TTL
@classmethod
def _is_negative(cls, key: tuple[str, str]) -> bool:
@@ -1061,9 +966,7 @@ class GroupMemoryCache:
@classmethod
def start_tasks(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "GROUP_MEM_REFRESH_INTERVAL", 60), 60
)
interval = GROUP_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -1103,7 +1006,7 @@ class LevelUserMemoryCache:
@classmethod
def _negative_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "LEVEL_MEM_NEGATIVE_TTL", 60), 60)
return LEVEL_MEM_NEGATIVE_TTL
@classmethod
def _is_negative(cls, key: tuple[str, str]) -> bool:
@@ -1153,9 +1056,7 @@ class LevelUserMemoryCache:
@classmethod
async def ensure_fresh(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "LEVEL_MEM_REFRESH_INTERVAL", 120), 120
)
interval = LEVEL_MEM_REFRESH_INTERVAL
if not cls._loaded:
await cls.refresh()
return
@@ -1284,9 +1185,7 @@ class LevelUserMemoryCache:
@classmethod
def start_tasks(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "LEVEL_MEM_REFRESH_INTERVAL", 120), 120
)
interval = LEVEL_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -1316,7 +1215,7 @@ class TaskInfoMemoryCache:
@classmethod
def _negative_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "TASK_MEM_NEGATIVE_TTL", 60), 60)
return TASK_MEM_NEGATIVE_TTL
@classmethod
def _is_negative(cls, module: str) -> bool:
@@ -1423,9 +1322,7 @@ class TaskInfoMemoryCache:
@classmethod
def start_tasks(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "TASK_MEM_REFRESH_INTERVAL", 300), 300
)
interval = TASK_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -1456,7 +1353,7 @@ class PluginLimitMemoryCache:
@classmethod
def _negative_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "LIMIT_MEM_NEGATIVE_TTL", 30), 30)
return LIMIT_MEM_NEGATIVE_TTL
@classmethod
def _is_negative(cls, module: str) -> bool:
@@ -1598,9 +1495,7 @@ class PluginLimitMemoryCache:
@classmethod
def start_tasks(cls) -> None:
interval = _coerce_int(
Config.get_config("hook", "LIMIT_MEM_REFRESH_INTERVAL", 60), 60
)
interval = LIMIT_MEM_REFRESH_INTERVAL
if interval <= 0:
return
if cls._refresh_task and not cls._refresh_task.done():
@@ -1634,7 +1529,7 @@ class BanMemoryCache:
@classmethod
def _neg_ttl(cls) -> int:
return _coerce_int(Config.get_config("hook", "BAN_MEM_NEGATIVE_TTL", 5), 5)
return BAN_MEM_NEGATIVE_TTL
@classmethod
def _neg_key(
@@ -1894,13 +1789,9 @@ class BanMemoryCache:
@classmethod
def start_tasks(cls) -> None:
refresh_interval = _coerce_int(
Config.get_config("hook", "BAN_MEM_REFRESH_INTERVAL", 60), 60
)
clean_interval = _coerce_int(
Config.get_config("hook", "BAN_MEM_CLEAN_INTERVAL", 60), 60
)
cleanup_db = bool(Config.get_config("hook", "BAN_MEM_CLEANUP_DB", True))
refresh_interval = BAN_MEM_REFRESH_INTERVAL
clean_interval = BAN_MEM_CLEAN_INTERVAL
cleanup_db = BAN_MEM_CLEANUP_DB
if refresh_interval > 0 and (not cls._refresh_task or cls._refresh_task.done()):
cls._refresh_task = asyncio.create_task(cls._refresh_loop(refresh_interval))