toxic-detector/app/core/cache.py

27 lines
691 B
Python

import aioredis
from app.core.config import settings
class RedisCache:
def __init__(self):
self.redis = None
async def connect(self):
self.redis = await aioredis.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True)
async def disconnect(self):
if self.redis:
await self.redis.close()
async def get(self, key: str):
if not self.redis:
await self.connect()
return await self.redis.get(key)
async def set(self, key: str, value: str, expire: int = 3600):
if not self.redis:
await self.connect()
await self.redis.set(key, value, ex=expire)
cache = RedisCache()