mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2024-12-27 11:49:04 +00:00
ac4610a57c
Use the default json when ujson is not available
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from core.base_service import BaseService
|
|
from core.dependence.redisdb import RedisDB
|
|
from modules.wiki.base import Model
|
|
|
|
try:
|
|
import ujson as jsonlib
|
|
except ImportError:
|
|
import json as jsonlib
|
|
|
|
|
|
__all__ = ["WikiCache"]
|
|
|
|
|
|
class WikiCache(BaseService.Component):
|
|
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 = jsonlib.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 = jsonlib.loads(await self.client.get(qname))
|
|
except Exception: # pylint: disable=W0703
|
|
result = []
|
|
if isinstance(result, list) and len(result) > 0:
|
|
for num, item in enumerate(result):
|
|
result[num] = jsonlib.loads(item)
|
|
return result
|