"""Redis cache client with async support."""

from functools import lru_cache

from redis.asyncio import Redis

from settings import settings


@lru_cache
def get_redis_client() -> Redis:
    return Redis.from_url(settings.REDIS_URL, decode_responses=True)


async def get_redis() -> Redis:
    """Dependency for FastAPI routes."""
    return get_redis_client()


async def cache_get(key: str) -> str | None:
    """Get a value from the cache."""
    client = get_redis_client()
    return await client.get(key)


async def cache_set(key: str, value: str, ttl: int = 3600) -> None:
    """Set a value in the cache with optional TTL."""
    client = get_redis_client()
    await client.set(key, value, ex=ttl)


async def cache_delete(key: str) -> None:
    """Delete a key from the cache."""
    client = get_redis_client()
    await client.delete(key)
