core/base/redis.py

35 lines
918 B
Python
Raw Normal View History

2021-08-16 12:09:04 +00:00
import aioredis
from settings import REDIS_URL
2022-09-03 10:50:14 +00:00
2021-08-16 12:09:04 +00:00
class Redis:
2022-09-03 10:50:14 +00:00
def __init__(self, uri=REDIS_URL):
self._uri: str = uri
self._instance = None
2021-08-16 12:09:04 +00:00
2022-09-03 10:50:14 +00:00
async def connect(self):
if self._instance is not None:
return
self._instance = aioredis.from_url(self._uri, encoding="utf-8")
2021-08-16 12:09:04 +00:00
2022-09-03 10:50:14 +00:00
async def disconnect(self):
if self._instance is None:
return
self._instance.close()
await self._instance.wait_closed()
self._instance = None
2021-08-16 12:09:04 +00:00
2022-09-03 10:50:14 +00:00
async def execute(self, command, *args, **kwargs):
return await self._instance.execute_command(command, *args, **kwargs)
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
async def lrange(self, key, start, stop):
return await self._instance.lrange(key, start, stop)
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
async def mget(self, key, *keys):
return await self._instance.mget(key, *keys)
2021-08-16 12:09:04 +00:00
2022-08-11 05:53:14 +00:00
redis = Redis()
2022-09-03 10:50:14 +00:00
__all__ = ["redis"]