fix: 修复 Redis SCAN cursor 类型问题并优化缓存清除日志

- CacheService.delete_pattern 中 cursor 可能为 int 或 str,统一转为 int 比较
- 模型缓存清除时区分有/无缓存的日志级别(info/debug)
This commit is contained in:
fawney19
2026-01-23 16:23:17 +08:00
parent 6f597a5f54
commit f45fd06fb0
2 changed files with 7 additions and 3 deletions

View File

@@ -73,7 +73,10 @@ async def invalidate_models_list_cache() -> None:
try:
# 使用通配符删除所有 models:list:* 缓存(包括多格式组合的 key
deleted = await CacheService.delete_pattern(f"{_CACHE_KEY_PREFIX}:*")
logger.debug(f"[ModelsService] 已清除 {deleted}{_CACHE_KEY_PREFIX} 缓存")
if deleted > 0:
logger.info(f"[ModelsService] 已清除 {deleted}{_CACHE_KEY_PREFIX} 缓存")
else:
logger.debug(f"[ModelsService] 无 {_CACHE_KEY_PREFIX} 缓存需要清除")
except Exception as e:
logger.warning(f"[ModelsService] 清除缓存失败: {e}")

View File

@@ -125,7 +125,7 @@ class CacheService:
# 使用 SCAN 遍历匹配的键
deleted_count = 0
cursor = 0
cursor: int = 0
while True:
cursor, keys = await redis.scan(cursor, match=pattern, count=batch_size)
if keys:
@@ -134,7 +134,8 @@ class CacheService:
batch = keys[i : i + batch_size]
await redis.delete(*batch)
deleted_count += len(batch)
if cursor == 0:
# cursor 可能是 int 或 str取决于 decode_responses 配置),统一转为 int 比较
if int(cursor) == 0:
break
if deleted_count > 0: