PamGram/core/wiki/cache.py
Karako 91a133b694
♻️ 重写 wiki 模块和相关插件
1. 使用 `pydantic` 重写了 wiki 模块所使用的 model
2. 添加了 weapon_level.json 用于后续计算武器升级所需的经验
3. 修改了 wiki 插件,以适应新的 model
2022-08-28 22:37:31 +08:00

31 lines
923 B
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}"
result = json.loads(await self.client.get(qname))
if isinstance(result, list) and len(result) > 0:
for num, item in enumerate(result):
result[num] = json.loads(item)
return result