Skip to content

Commit c346af0

Browse files
perf: Cache Disabled for MCP Service
1 parent 1f74224 commit c346af0

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

backend/common/core/sqlbot_cache.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def decorator(func):
6868

6969
@wraps(func)
7070
async def wrapper(*args, **kwargs):
71-
if not settings.CACHE_TYPE or settings.CACHE_TYPE.lower() == "none":
71+
if not settings.CACHE_TYPE or settings.CACHE_TYPE.lower() == "none" or not is_cache_initialized():
7272
return await func(*args, **kwargs)
7373
# 生成缓存键
7474
cache_key = used_key_builder(
@@ -96,7 +96,7 @@ def clear_cache(
9696
def decorator(func):
9797
@wraps(func)
9898
async def wrapper(*args, **kwargs):
99-
if not settings.CACHE_TYPE or settings.CACHE_TYPE.lower() == "none":
99+
if not settings.CACHE_TYPE or settings.CACHE_TYPE.lower() == "none" or not is_cache_initialized():
100100
return await func(*args, **kwargs)
101101
cache_key = custom_key_builder(
102102
func=func,
@@ -138,4 +138,21 @@ def init_sqlbot_cache():
138138
SQLBotLogUtil.info(f"SQLBot 使用Redis缓存, 可使用多进程模式")
139139
else:
140140
SQLBotLogUtil.warning("SQLBot 未启用缓存, 可使用多进程模式")
141-
141+
142+
143+
def is_cache_initialized() -> bool:
144+
# 检查必要的属性是否存在
145+
if not hasattr(FastAPICache, "_backend") or not hasattr(FastAPICache, "_prefix"):
146+
return False
147+
148+
# 检查属性值是否为 None
149+
if FastAPICache._backend is None or FastAPICache._prefix is None:
150+
return False
151+
152+
# 尝试获取后端确认
153+
try:
154+
backend = FastAPICache.get_backend()
155+
return backend is not None
156+
except (AssertionError, AttributeError, Exception) as e:
157+
SQLBotLogUtil.debug(f"缓存初始化检查失败: {str(e)}")
158+
return False

0 commit comments

Comments
 (0)