Spaces:
Sleeping
Sleeping
File size: 2,369 Bytes
f171e60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | from fastapi import APIRouter, HTTPException, Depends
from app.core.cache.manager import cache_manager
from app.middleware.auth import optional_api_key
from app.utils.logger import logger
router = APIRouter()
@router.get(
"/stats",
summary="Get cache statistics",
description="Lấy thông tin thống kê về cache"
)
async def get_cache_stats(api_key: str = Depends(optional_api_key)):
"""
Get cache statistics
Returns thông tin về:
- Cache type (memory/redis)
- Number of cached items
- Memory usage (nếu Redis)
- Etc.
"""
try:
stats = await cache_manager.get_stats()
return stats
except Exception as e:
logger.error(f"Failed to get cache stats: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.delete(
"/clear",
summary="Clear cache",
description="Xóa toàn bộ cache"
)
async def clear_cache(api_key: str = Depends(optional_api_key)):
"""
Clear toàn bộ cache
**Cảnh báo**: Thao tác này sẽ xóa tất cả cached responses
"""
try:
success = await cache_manager.clear()
if success:
logger.info("Cache cleared via API")
return {
"message": "Cache cleared successfully",
"success": True
}
else:
raise HTTPException(status_code=500, detail="Failed to clear cache")
except Exception as e:
logger.error(f"Failed to clear cache: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.delete(
"/delete/{key}",
summary="Delete cache key",
description="Xóa một cache key cụ thể"
)
async def delete_cache_key(key: str, api_key: str = Depends(optional_api_key)):
"""
Delete một cache key cụ thể
"""
try:
success = await cache_manager.delete(key)
if success:
logger.info(f"Cache key deleted: {key}")
return {
"message": f"Cache key '{key}' deleted",
"success": True
}
else:
return {
"message": f"Cache key '{key}' not found",
"success": False
}
except Exception as e:
logger.error(f"Failed to delete cache key: {e}")
raise HTTPException(status_code=500, detail=str(e)) |