mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-16 12:02:16 +00:00
8cb3ff9ca7
* 🐛 兼容 python3.8 * 🐛 修复 redis 中无数据时的错误
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
import ujson as json
|
|
|
|
from models.wiki.base import Model
|
|
from utils.redisdb import RedisDB
|
|
|
|
|
|
class WikiCache:
|
|
def __init__(self, redis: RedisDB):
|
|
self.client = redis.client
|
|
self.qname = "wiki"
|
|
|
|
async def set(self, key: str, value):
|
|
qname = f"{self.qname}:{key}"
|
|
if isinstance(value, Model):
|
|
value = value.json()
|
|
elif isinstance(value, (dict, list)):
|
|
value = json.dumps(value)
|
|
await self.client.set(qname, value)
|
|
|
|
async def delete(self, key: str):
|
|
qname = f"{self.qname}:{key}"
|
|
await self.client.delete(qname)
|
|
|
|
async def get(self, key: str) -> dict:
|
|
qname = f"{self.qname}:{key}"
|
|
# noinspection PyBroadException
|
|
try:
|
|
result = json.loads(await self.client.get(qname))
|
|
except Exception:
|
|
result = []
|
|
if isinstance(result, list) and len(result) > 0:
|
|
for num, item in enumerate(result):
|
|
result[num] = json.loads(item)
|
|
return result
|