0.2.14
Some checks failed
deploy / deploy (push) Failing after 2m1s

This commit is contained in:
2023-11-22 19:38:39 +03:00
parent e2082b48d3
commit db76ba3733
22 changed files with 271 additions and 314 deletions

View File

@@ -1,6 +1,7 @@
import redis.asyncio as aredis
from settings import REDIS_URL
class RedisCache:
def __init__(self, uri=REDIS_URL):
self._uri: str = uri
@@ -11,28 +12,25 @@ class RedisCache:
self._client = aredis.Redis.from_url(self._uri, decode_responses=True)
async def disconnect(self):
await self._client.aclose()
if self._client:
await self._client.close()
async def execute(self, command, *args, **kwargs):
if not self._client:
await self.connect()
try:
print(f"[redis] {command} {args}")
return await self._client.execute_command(command, *args, **kwargs)
except Exception as e:
print(f"[redis] ERROR: {e} with: {command} {args}")
import traceback
traceback.print_exc()
if self._client:
try:
print("[redis] " + command + " " + " ".join(args))
r = await self._client.execute_command(command, *args, **kwargs)
return r
except Exception as e:
print(f"[redis] error: {e}")
return None
async def subscribe(self, *channels):
if not self._client:
await self.connect()
async with self._client.pubsub() as pubsub:
for channel in channels:
await pubsub.subscribe(channel)
self.pubsub_channels.append(channel)
if self._client:
async with self._client.pubsub() as pubsub:
for channel in channels:
await pubsub.subscribe(channel)
self.pubsub_channels.append(channel)
async def unsubscribe(self, *channels):
if not self._client:
@@ -48,12 +46,15 @@ class RedisCache:
await self._client.publish(channel, data)
async def lrange(self, key, start, stop):
print(f"[redis] LRANGE {key} {start} {stop}")
return await self._client.lrange(key, start, stop)
if self._client:
print(f"[redis] LRANGE {key} {start} {stop}")
return await self._client.lrange(key, start, stop)
async def mget(self, key, *keys):
print(f"[redis] MGET {key} {keys}")
return await self._client.mget(key, *keys)
if self._client:
print(f"[redis] MGET {key} {keys}")
return await self._client.mget(key, *keys)
redis = RedisCache()