This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import redis.asyncio as aredis
|
||||
from settings import REDIS_URL
|
||||
@@ -47,25 +48,19 @@ class RedisCache:
|
||||
return
|
||||
await self._client.publish(channel, data)
|
||||
|
||||
async def lrange(self, 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):
|
||||
if self._client:
|
||||
print(f"[redis] MGET {key} {keys}")
|
||||
return await self._client.mget(key, *keys)
|
||||
|
||||
async def listen(self, channel):
|
||||
pubsub = self._client.pubsub()
|
||||
await pubsub.subscribe(channel)
|
||||
if self._client:
|
||||
pubsub = self._client.pubsub()
|
||||
await pubsub.subscribe(channel)
|
||||
|
||||
while True:
|
||||
message = pubsub.get_message()
|
||||
if message:
|
||||
yield message
|
||||
await asyncio.sleep(0.1)
|
||||
try:
|
||||
while True:
|
||||
message = await pubsub.get_message()
|
||||
if message:
|
||||
yield json.loads(message['data'])
|
||||
await asyncio.sleep(0.1)
|
||||
finally:
|
||||
await pubsub.unsubscribe(channel)
|
||||
|
||||
|
||||
redis = RedisCache()
|
||||
|
Reference in New Issue
Block a user