# -*- coding: utf-8 -*- from typing import AsyncGenerator from fastapi import Depends from redis.asyncio import ConnectionPool, Redis import setting REDIS_URL = setting.REDIS_URL pool = ConnectionPool.from_url( REDIS_URL, max_connections=200, decode_responses=True, ) async def get_redis() -> AsyncGenerator[Redis, None]: async with Redis(connection_pool=pool) as conn: yield conn async def set_cache( key: str, value: str, ttl: int | None = None, r: Redis = Depends(get_redis), ): await r.set(key, value, ex=ttl) async def get_cache(key: str, r: Redis = Depends(get_redis)): return await r.get(key) async def del_cache(key: str, r: Redis = Depends(get_redis)): await r.delete(key)