Download src/api/cache.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/api/cache.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/api/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/api/cache.py
3.16 kB
| """ | |
| YAZ Care Platform - Cache Management API | |
| Provides endpoints for monitoring and managing the response cache | |
| """ | |
| from fastapi import APIRouter, HTTPException | |
| from typing import Optional | |
| import structlog | |
| logger = structlog.get_logger(__name__) | |
| router = APIRouter(prefix="/api/cache", tags=["Cache Management"]) | |
| async def get_cache_stats(): | |
| """Get cache statistics and hit rates""" | |
| try: | |
| from middleware.response_cache import get_cache_middleware | |
| middleware = get_cache_middleware() | |
| if middleware is None: | |
| return { | |
| "status": "disabled", | |
| "message": "Response caching is not enabled" | |
| } | |
| stats = middleware.get_stats() | |
| return { | |
| "status": "active", | |
| **stats | |
| } | |
| except Exception as e: | |
| logger.error(f"Failed to get cache stats: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def clear_cache(pattern: Optional[str] = None): | |
| """ | |
| Clear cache entries | |
| Args: | |
| pattern: Optional pattern to match cache keys (None = clear all) | |
| """ | |
| try: | |
| from middleware.response_cache import get_cache_middleware | |
| middleware = get_cache_middleware() | |
| if middleware is None: | |
| return { | |
| "status": "disabled", | |
| "message": "Response caching is not enabled" | |
| } | |
| cleared = middleware.clear_cache(pattern) | |
| return { | |
| "status": "success", | |
| "cleared_entries": cleared, | |
| "pattern": pattern or "all" | |
| } | |
| except Exception as e: | |
| logger.error(f"Failed to clear cache: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def cache_health_check(): | |
| """Check cache system health""" | |
| try: | |
| from middleware.response_cache import get_cache_middleware | |
| middleware = get_cache_middleware() | |
| if middleware is None: | |
| return { | |
| "status": "disabled", | |
| "healthy": True, | |
| "message": "Response caching is not enabled" | |
| } | |
| stats = middleware.get_stats() | |
| # Calculate health metrics | |
| is_healthy = True | |
| warnings = [] | |
| # Check if cache is near capacity | |
| usage_percent = (stats["cache_size"] / stats["max_size"]) * 100 | |
| if usage_percent > 90: | |
| warnings.append(f"Cache usage high: {usage_percent:.1f}%") | |
| # Check hit rate | |
| if stats["total_requests"] > 100 and stats["hit_rate_percent"] < 30: | |
| warnings.append(f"Low cache hit rate: {stats['hit_rate_percent']}%") | |
| return { | |
| "status": "active", | |
| "healthy": is_healthy, | |
| "warnings": warnings, | |
| "metrics": stats | |
| } | |
| except Exception as e: | |
| logger.error(f"Cache health check failed: {e}") | |
| return { | |
| "status": "error", | |
| "healthy": False, | |
| "error": str(e) | |
| } | |