File size: 839 Bytes
64d7fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import redis.asyncio as aioredis
from app.config import config
from app.utils.logger import logger


class RedisClient:
    def __init__(self):
        self.client = None
        
    async def connect(self):
        if self.client is None:
            redis_url = config["database"]["redis"]["url"]
            self.client = await aioredis.from_url(
                redis_url,
                encoding="utf-8",
                decode_responses=True
            )
            logger.info("Redis connected")
        return self.client
    
    async def disconnect(self):
        if self.client:
            await self.client.close()
            logger.info("Redis disconnected")
    
    async def get_client(self):
        if self.client is None:
            await self.connect()
        return self.client


redis_client = RedisClient()