33 lines
711 B
Python
33 lines
711 B
Python
|
|
"""Redis connection management."""
|
||
|
|
from redis.asyncio import Redis
|
||
|
|
|
||
|
|
from app.core.config import settings
|
||
|
|
|
||
|
|
_redis: Redis | None = None
|
||
|
|
|
||
|
|
|
||
|
|
async def get_redis() -> Redis:
|
||
|
|
"""Get or create Redis connection."""
|
||
|
|
global _redis
|
||
|
|
if _redis is None:
|
||
|
|
_redis = Redis.from_url(settings.REDIS_URL, decode_responses=True)
|
||
|
|
return _redis
|
||
|
|
|
||
|
|
|
||
|
|
async def close_redis() -> None:
|
||
|
|
"""Close Redis connection."""
|
||
|
|
global _redis
|
||
|
|
if _redis:
|
||
|
|
await _redis.close()
|
||
|
|
_redis = None
|
||
|
|
|
||
|
|
|
||
|
|
async def check_redis_health() -> bool:
|
||
|
|
"""Check if Redis is reachable."""
|
||
|
|
try:
|
||
|
|
redis = await get_redis()
|
||
|
|
await redis.ping()
|
||
|
|
return True
|
||
|
|
except Exception:
|
||
|
|
return False
|