🐛 修复数据库超时问题

This commit is contained in:
HibiKier
2025-08-22 02:43:19 +08:00
parent a63f26c3b6
commit 8d8cbf20c3
6 changed files with 70 additions and 11 deletions
+13 -2
View File
@@ -7,6 +7,8 @@ from zhenxun.services.log import logger
T = TypeVar("T", bound=Model)
cache = CacheRoot.cache_dict("DB_TEST_BAN", 10, int)
class DataAccess(Generic[T]):
"""数据访问层,根据配置决定是否使用缓存
@@ -167,6 +169,7 @@ class DataAccess(Generic[T]):
return await with_db_timeout(
db_query_func(*args, **kwargs),
operation=f"{self.model_cls.__name__}.{db_query_func.__name__}",
source="DataAccess",
)
# 尝试从缓存获取
@@ -178,10 +181,11 @@ class DataAccess(Generic[T]):
# 如果成功构建缓存键,尝试从缓存获取
if cache_key is not None:
data = await self.cache.get(cache_key)
logger.debug(
f"{self.model_cls.__name__} self.cache.get(cache_key)"
logger.info(
f"{self.model_cls.__name__} key: {cache_key}"
f" 从缓存获取到的数据 {type(data)}: {data}"
)
if data == self._NULL_RESULT:
# 空结果缓存命中
self._cache_stats[self.cache_type]["null_hits"] += 1
@@ -193,6 +197,13 @@ class DataAccess(Generic[T]):
f"{self.model_cls.__name__} 从缓存获取"
f"到空结果: {cache_key}, 允许数据不存在,返回None"
)
if self.model_cls.__name__ == "BanConsole":
uid = kwargs.get("user_id")
if uid:
logger.info(
f"查询ban: {cache_key}:{uid}数据不存在返回NULL结果",
"DB_TEST__BAN",
)
return None
elif data:
# 缓存命中