2022-08-28 14:37:31 +00:00
|
|
|
import ujson as json
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
from models.wiki.base import Model
|
2022-07-26 10:07:31 +00:00
|
|
|
from utils.redisdb import RedisDB
|
|
|
|
|
|
|
|
|
|
|
|
class WikiCache:
|
|
|
|
def __init__(self, redis: RedisDB):
|
|
|
|
self.client = redis.client
|
|
|
|
self.qname = "wiki"
|
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
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)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
async def delete(self, key: str):
|
|
|
|
qname = f"{self.qname}:{key}"
|
2022-07-26 10:07:31 +00:00
|
|
|
await self.client.delete(qname)
|
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
async def get(self, key: str) -> dict:
|
|
|
|
qname = f"{self.qname}:{key}"
|
2022-08-31 07:27:14 +00:00
|
|
|
data = await self.client.get(qname)
|
|
|
|
json_data = str(data, encoding="utf-8")
|
|
|
|
result = json.loads(json_data)
|
2022-08-28 14:37:31 +00:00
|
|
|
if isinstance(result, list) and len(result) > 0:
|
|
|
|
for num, item in enumerate(result):
|
|
|
|
result[num] = json.loads(item)
|
|
|
|
return result
|