ruff fix
This commit is contained in:
@@ -2,15 +2,15 @@
|
||||
缓存管理和监控接口
|
||||
提供缓存统计、清理和预热功能
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.database import get_redis
|
||||
from app.dependencies.user import get_current_user
|
||||
from app.service.user_cache_service import get_user_cache_service
|
||||
|
||||
from .router import router
|
||||
|
||||
from fastapi import Depends, HTTPException, Security
|
||||
from fastapi import Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from redis.asyncio import Redis
|
||||
|
||||
@@ -34,7 +34,7 @@ async def get_cache_stats(
|
||||
try:
|
||||
cache_service = get_user_cache_service(redis)
|
||||
user_cache_stats = await cache_service.get_cache_stats()
|
||||
|
||||
|
||||
# 获取 Redis 基本信息
|
||||
redis_info = await redis.info()
|
||||
redis_stats = {
|
||||
@@ -47,20 +47,17 @@ async def get_cache_stats(
|
||||
"evicted_keys": redis_info.get("evicted_keys", 0),
|
||||
"expired_keys": redis_info.get("expired_keys", 0),
|
||||
}
|
||||
|
||||
|
||||
# 计算缓存命中率
|
||||
hits = redis_stats["keyspace_hits"]
|
||||
misses = redis_stats["keyspace_misses"]
|
||||
hit_rate = hits / (hits + misses) * 100 if (hits + misses) > 0 else 0
|
||||
redis_stats["cache_hit_rate_percent"] = round(hit_rate, 2)
|
||||
|
||||
return CacheStatsResponse(
|
||||
user_cache=user_cache_stats,
|
||||
redis_info=redis_stats
|
||||
)
|
||||
|
||||
|
||||
return CacheStatsResponse(user_cache=user_cache_stats, redis_info=redis_stats)
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(500, f"Failed to get cache stats: {str(e)}")
|
||||
raise HTTPException(500, f"Failed to get cache stats: {e!s}")
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -80,7 +77,7 @@ async def invalidate_user_cache(
|
||||
await cache_service.invalidate_v1_user_cache(user_id)
|
||||
return {"message": f"Cache invalidated for user {user_id}"}
|
||||
except Exception as e:
|
||||
raise HTTPException(500, f"Failed to invalidate cache: {str(e)}")
|
||||
raise HTTPException(500, f"Failed to invalidate cache: {e!s}")
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -98,15 +95,15 @@ async def clear_all_user_cache(
|
||||
user_keys = await redis.keys("user:*")
|
||||
v1_user_keys = await redis.keys("v1_user:*")
|
||||
all_keys = user_keys + v1_user_keys
|
||||
|
||||
|
||||
if all_keys:
|
||||
await redis.delete(*all_keys)
|
||||
return {"message": f"Cleared {len(all_keys)} cache entries"}
|
||||
else:
|
||||
return {"message": "No cache entries found"}
|
||||
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(500, f"Failed to clear cache: {str(e)}")
|
||||
raise HTTPException(500, f"Failed to clear cache: {e!s}")
|
||||
|
||||
|
||||
class CacheWarmupRequest(BaseModel):
|
||||
@@ -127,18 +124,22 @@ async def warmup_cache(
|
||||
):
|
||||
try:
|
||||
cache_service = get_user_cache_service(redis)
|
||||
|
||||
|
||||
if request.user_ids:
|
||||
# 预热指定用户
|
||||
from app.dependencies.database import with_db
|
||||
|
||||
async with with_db() as session:
|
||||
await cache_service.preload_user_cache(session, request.user_ids)
|
||||
return {"message": f"Warmed up cache for {len(request.user_ids)} users"}
|
||||
else:
|
||||
# 预热活跃用户
|
||||
from app.scheduler.user_cache_scheduler import schedule_user_cache_preload_task
|
||||
from app.scheduler.user_cache_scheduler import (
|
||||
schedule_user_cache_preload_task,
|
||||
)
|
||||
|
||||
await schedule_user_cache_preload_task()
|
||||
return {"message": f"Warmed up cache for top {request.limit} active users"}
|
||||
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(500, f"Failed to warmup cache: {str(e)}")
|
||||
raise HTTPException(500, f"Failed to warmup cache: {e!s}")
|
||||
|
||||
Reference in New Issue
Block a user