26 lines
586 B
Python
26 lines
586 B
Python
"""Redis client module."""
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
from redis.asyncio import Redis, from_url
|
|
|
|
from app.core.config import settings
|
|
|
|
_redis_pool: Redis | None = None
|
|
|
|
|
|
async def get_redis() -> Redis:
|
|
"""Get global Redis client instance."""
|
|
global _redis_pool
|
|
if _redis_pool is None:
|
|
_redis_pool = from_url(settings.redis_url, encoding="utf-8", decode_responses=True)
|
|
return _redis_pool
|
|
|
|
|
|
async def close_redis():
|
|
"""Close Redis connection."""
|
|
global _redis_pool
|
|
if _redis_pool:
|
|
await _redis_pool.close()
|
|
_redis_pool = None
|