fix(redis): 添加Redis缓存模块导入异常处理

This commit is contained in:
Windpicker-owo
2025-12-08 20:54:23 +08:00
parent 094861e6b7
commit f76cf36bae
2 changed files with 6 additions and 6 deletions

View File

@@ -33,7 +33,6 @@ from .preloader import (
get_preloader, get_preloader,
record_preload_access, record_preload_access,
) )
from .redis_cache import RedisCache, close_redis_cache, get_redis_cache
__all__ = [ __all__ = [
"AccessPattern", "AccessPattern",
@@ -53,16 +52,12 @@ __all__ = [
# Memory Cache # Memory Cache
"MultiLevelCache", "MultiLevelCache",
"Priority", "Priority",
# Redis Cache
"RedisCache",
"close_batch_scheduler", "close_batch_scheduler",
"close_cache", "close_cache",
"close_preloader", "close_preloader",
"close_redis_cache",
"get_batch_scheduler", "get_batch_scheduler",
"get_cache", "get_cache",
"get_cache_backend_type", "get_cache_backend_type",
"get_preloader", "get_preloader",
"record_preload_access", "record_preload_access",
"get_redis_cache"
] ]

View File

@@ -887,7 +887,12 @@ async def _create_redis_cache(db_config: Any) -> CacheBackend:
Raises: Raises:
RuntimeError: Redis 连接失败时抛出异常 RuntimeError: Redis 连接失败时抛出异常
""" """
from src.common.database.optimization.redis_cache import RedisCache try:
from src.common.database.optimization.redis_cache import RedisCache
except ModuleNotFoundError as exc:
raise RuntimeError(
"Redis 缓存后端需要可选依赖 'redis',请执行 `pip install redis` 或将 cache_backend 配置为 'memory'"
) from exc
logger.info( logger.info(
f"创建 Redis 缓存: {db_config.redis_host}:{db_config.redis_port}/{db_config.redis_db}, " f"创建 Redis 缓存: {db_config.redis_host}:{db_config.redis_port}/{db_config.redis_db}, "