This commit is contained in:
2024-01-26 03:40:49 +03:00
parent 7beddea5b1
commit 59a1f8c902
14 changed files with 249 additions and 188 deletions

View File

@@ -1,14 +1,16 @@
import json
import redis.asyncio as aredis
import asyncio
from settings import REDIS_URL
import json
import logging
logger = logging.getLogger("\t[services.redis]\t")
import redis.asyncio as aredis
from settings import REDIS_URL
logger = logging.getLogger('\t[services.redis]\t')
logger.setLevel(logging.DEBUG)
class RedisCache:
def __init__(self, uri=REDIS_URL):
self._uri: str = uri
@@ -25,11 +27,11 @@ class RedisCache:
async def execute(self, command, *args, **kwargs):
if self._client:
try:
logger.debug(command + " " + " ".join(args))
logger.debug(command + ' ' + ' '.join(args))
r = await self._client.execute_command(command, *args, **kwargs)
return r
except Exception as e:
logger.error(f"{e}")
logger.error(f'{e}')
return None
async def subscribe(self, *channels):
@@ -59,15 +61,15 @@ class RedisCache:
while True:
message = await pubsub.get_message()
if message and isinstance(message["data"], (str, bytes, bytearray)):
logger.debug("pubsub got msg")
if message and isinstance(message['data'], (str, bytes, bytearray)):
logger.debug('pubsub got msg')
try:
yield json.loads(message["data"]), message.get("channel")
yield json.loads(message['data']), message.get('channel')
except Exception as e:
logger.error(f"{e}")
logger.error(f'{e}')
await asyncio.sleep(1)
redis = RedisCache()
__all__ = ["redis"]
__all__ = ['redis']