fix: use AUTH_SNAPSHOT and PLUGIN_SNAPSHOT cache types instead of TEMP/PLUGINS

This commit is contained in:
HibiKier
2025-12-30 15:10:14 +08:00
parent ae4aa5c29c
commit 358c7f502c
14 changed files with 51 additions and 27 deletions
+2 -2
View File
@@ -271,7 +271,7 @@ class SnapshotBuilder:
queries.append(f"""
SELECT 'level_global' as query_type, NULL as gold, user_level,
NULL as ban_time, NULL as duration
FROM level_user WHERE user_id = {ph()} AND group_id IS NULL
FROM level_users WHERE user_id = {ph()} AND group_id IS NULL
""")
params.append(user_id)
@@ -280,7 +280,7 @@ class SnapshotBuilder:
queries.append(f"""
SELECT 'level_group' as query_type, NULL as gold, user_level,
NULL as ban_time, NULL as duration
FROM level_user
FROM level_users
WHERE user_id = {ph()} AND group_id = {ph()}
""")
params.extend([user_id, group_id])
+10 -9
View File
@@ -8,7 +8,6 @@ import asyncio
import time
from nonebot.adapters import Bot, Event
from nonebot.exception import IgnoredException
from nonebot.matcher import Matcher
from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_uninfo import Uninfo
@@ -19,10 +18,11 @@ from zhenxun.utils.enum import BlockType, GoldHandle
from zhenxun.utils.platform import PlatformUtils
from zhenxun.utils.utils import get_entity_ids
from .exception import IsSuperuserException, SkipPluginException
from .models import AuthSnapshot, PluginSnapshot
from .service import AuthSnapshotService, PluginSnapshotService
LOG_COMMAND = "auth_checker_v2"
LOG_COMMAND = "AuthSnapshotChecker"
WARNING_THRESHOLD = 0.5 # 警告阈值(秒)
@@ -78,7 +78,7 @@ class OptimizedAuthChecker:
if not module:
result.fail("Matcher插件名称不存在...")
raise IgnoredException(result.skip_reason)
raise SkipPluginException(result.skip_reason)
# 2. 获取权限快照(第一次查询)
snapshot_start = time.time()
@@ -96,12 +96,12 @@ class OptimizedAuthChecker:
if not plugin_snapshot:
result.fail(f"插件:{module} 数据不存在...")
raise IgnoredException(result.skip_reason)
raise SkipPluginException(result.skip_reason)
# 4. 检查是否为隐藏插件
if plugin_snapshot.is_hidden():
result.fail(f"插件: {plugin_snapshot.name}:{module} 为HIDDEN...")
raise IgnoredException(result.skip_reason)
return
# 5. 检查超级用户
is_superuser = session.user.id in bot.config.superusers
@@ -122,7 +122,7 @@ class OptimizedAuthChecker:
# 7. 处理检查结果
if not result.passed:
logger.info(result.skip_reason, LOG_COMMAND, session=session)
raise IgnoredException(result.skip_reason)
raise SkipPluginException(result.skip_reason)
# 8. 超级用户跳过后续限制
if is_superuser:
@@ -162,12 +162,13 @@ class OptimizedAuthChecker:
logger.error(
f"扣除金币超时,模块: {module}", LOG_COMMAND, session=session
)
except IgnoredException:
except IsSuperuserException:
raise
except SkipPluginException:
raise
except Exception as e:
logger.error(f"权限检查异常: {e}", LOG_COMMAND, session=session, e=e)
raise IgnoredException("权限检查异常") from e
raise SkipPluginException("权限检查异常") from e
finally:
# 记录总执行时间
total_time = time.time() - start_time
@@ -0,0 +1,26 @@
class IsSuperuserException(Exception):
pass
class SkipPluginException(Exception):
def __init__(self, info: str, *args: object) -> None:
super().__init__(*args)
self.info = info
def __str__(self) -> str:
return self.info
def __repr__(self) -> str:
return self.info
class PermissionExemption(Exception):
def __init__(self, info: str, *args: object) -> None:
super().__init__(*args)
self.info = info
def __str__(self) -> str:
return self.info
def __repr__(self) -> str:
return self.info
+5 -5
View File
@@ -108,7 +108,7 @@ class AuthSnapshotService:
# 2. 尝试从Redis获取
if not force_refresh and cache_config.cache_mode != CacheMode.NONE:
try:
cached = await CacheRoot.get(CacheType.TEMP, cache_key)
cached = await CacheRoot.get(CacheType.AUTH_SNAPSHOT, cache_key)
if cached and isinstance(cached, dict):
snapshot = AuthSnapshot.model_validate(cached)
if not snapshot.is_expired(AUTH_REDIS_TTL):
@@ -201,7 +201,7 @@ class AuthSnapshotService:
"""异步存入Redis"""
try:
await CacheRoot.set(
CacheType.TEMP,
CacheType.AUTH_SNAPSHOT,
cache_key,
snapshot.model_dump(),
expire=AUTH_REDIS_TTL,
@@ -329,7 +329,7 @@ class PluginSnapshotService:
# 2. 尝试从Redis获取
if not force_refresh and cache_config.cache_mode != CacheMode.NONE:
try:
cached = await CacheRoot.get(CacheType.PLUGINS, cache_key)
cached = await CacheRoot.get(CacheType.PLUGIN_SNAPSHOT, cache_key)
if cached and isinstance(cached, dict):
snapshot = PluginSnapshot.model_validate(cached)
if not snapshot.is_expired(PLUGIN_REDIS_TTL):
@@ -395,7 +395,7 @@ class PluginSnapshotService:
"""异步存入Redis"""
try:
await CacheRoot.set(
CacheType.PLUGINS,
CacheType.PLUGIN_SNAPSHOT,
cache_key,
snapshot.model_dump(),
expire=PLUGIN_REDIS_TTL,
@@ -420,7 +420,7 @@ class PluginSnapshotService:
# 清理Redis缓存
if cache_config.cache_mode != CacheMode.NONE:
try:
await CacheRoot.delete(CacheType.PLUGINS, cache_key)
await CacheRoot.delete(CacheType.PLUGIN_SNAPSHOT, cache_key)
except Exception:
pass