refactor: replace legacy auth checker with optimized auth checker for improved performance

This commit is contained in:
HibiKier
2025-12-29 17:03:25 +08:00
parent 7ec11474f8
commit ae4aa5c29c
2 changed files with 10 additions and 30 deletions
@@ -16,17 +16,11 @@
""" """
import nonebot import nonebot
from nonebot.adapters import Bot, Event
from nonebot.matcher import Matcher
from nonebot.message import run_preprocessor
from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_uninfo import Uninfo
from zhenxun.services.auth_snapshot import ( from zhenxun.services.auth_snapshot import (
AuthSnapshotService, AuthSnapshotService,
PluginSnapshotService, PluginSnapshotService,
) )
from zhenxun.services.auth_snapshot.checker import optimized_auth_checker
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.utils.manager.priority_manager import PriorityLifecycle from zhenxun.utils.manager.priority_manager import PriorityLifecycle
@@ -49,19 +43,3 @@ async def _cleanup_cache():
AuthSnapshotService.clear_all_cache() AuthSnapshotService.clear_all_cache()
PluginSnapshotService.clear_all_cache() PluginSnapshotService.clear_all_cache()
logger.info("快照缓存已清理", "auth_checker_v2") logger.info("快照缓存已清理", "auth_checker_v2")
# 权限检查前处理器
@run_preprocessor
async def auth_check_v2(
matcher: Matcher,
event: Event,
bot: Bot,
session: Uninfo,
message: UniMsg,
):
"""优化后的权限检查
使用预聚合的权限快照进行检查,大幅减少数据库/缓存查询次数
"""
await optimized_auth_checker.check(matcher, event, bot, session, message)
+10 -8
View File
@@ -6,23 +6,25 @@ from nonebot.message import run_postprocessor, run_preprocessor
from nonebot_plugin_alconna import UniMsg from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_uninfo import Uninfo from nonebot_plugin_uninfo import Uninfo
from zhenxun.services.auth_snapshot.checker import optimized_auth_checker
from zhenxun.services.log import logger from zhenxun.services.log import logger
from .auth.config import LOGGER_COMMAND from .auth.config import LOGGER_COMMAND
from .auth_checker import LimitManager, _auth_checker from .auth_checker import LimitManager
# # 权限检测 # # 权限检测
@run_preprocessor @run_preprocessor
async def _(matcher: Matcher, event: Event, bot: Bot, session: Uninfo, message: UniMsg): async def _(matcher: Matcher, event: Event, bot: Bot, session: Uninfo, message: UniMsg):
start_time = time.time() start_time = time.time()
await _auth_checker.check( # await _auth_checker.check(
matcher, # matcher,
event, # event,
bot, # bot,
session, # session,
message, # message,
) # )
await optimized_auth_checker.check(matcher, event, bot, session, message)
logger.debug(f"权限检测耗时:{time.time() - start_time}秒", LOGGER_COMMAND) logger.debug(f"权限检测耗时:{time.time() - start_time}秒", LOGGER_COMMAND)