This commit is contained in:
2023-10-13 19:45:30 +03:00
parent 1c46fc1d48
commit 84e6841331
8 changed files with 84 additions and 73 deletions

View File

@@ -11,25 +11,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("[redis] " + command + " " + " ".join(args))
return await self._client.execute_command(command, *args, **kwargs)
except Exception as e:
print(f"[redis] error: {e}")
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:
@@ -45,12 +45,14 @@ 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()