Spaces:
Configuration error
Configuration error
Download app/cache.py from cuatrolabs/cuatrolabs-analytics-ms: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/cuatrolabs/cuatrolabs-analytics-ms/resolve/main/app/cache.py
- Command line
-
hf download hf://spaces/cuatrolabs/cuatrolabs-analytics-ms/app/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/cuatrolabs/cuatrolabs-analytics-ms/resolve/main/app/cache.py
1.53 kB
| """ | |
| Redis connection management for Analytics Microservice. | |
| """ | |
| import redis.asyncio as redis | |
| from app.core.logging import get_logger | |
| from app.core.config import settings | |
| logger = get_logger(__name__) | |
| redis_client: redis.Redis = None | |
| async def connect_to_redis(): | |
| global redis_client | |
| try: | |
| pool_params = { | |
| "host": settings.REDIS_HOST, | |
| "port": settings.REDIS_PORT, | |
| "db": settings.REDIS_DB, | |
| "decode_responses": True, | |
| "max_connections": 5, | |
| "socket_keepalive": True, | |
| "socket_connect_timeout": 5, | |
| "socket_timeout": 5, | |
| "retry_on_timeout": False, | |
| "health_check_interval": 30, | |
| } | |
| if settings.REDIS_PASSWORD: | |
| pool_params["password"] = settings.REDIS_PASSWORD | |
| pool = redis.ConnectionPool(**pool_params) | |
| redis_client = redis.Redis(connection_pool=pool) | |
| await redis_client.ping() | |
| logger.info("Redis connected successfully", extra={"event": "redis_connected"}) | |
| except Exception as e: | |
| logger.warning("Redis connection failed - continuing without cache", extra={"event": "redis_connect_failure", "error": str(e)}) | |
| redis_client = None | |
| async def close_redis_connection(): | |
| global redis_client | |
| if redis_client: | |
| await redis_client.aclose() | |
| redis_client = None | |
| logger.info("Redis connection closed", extra={"event": "redis_disconnected"}) | |
| def get_redis() -> redis.Redis: | |
| return redis_client | |