2022-07-26 10:07:31 +00:00
|
|
|
from typing import List
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.base_service import BaseService
|
|
|
|
from core.dependence.redisdb import RedisDB
|
|
|
|
from core.services.quiz.models import Answer, Question
|
2022-08-05 11:14:29 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("QuizCache",)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
class QuizCache(BaseService.Component):
|
2022-07-26 10:07:31 +00:00
|
|
|
def __init__(self, redis: RedisDB):
|
|
|
|
self.client = redis.client
|
|
|
|
self.question_qname = "quiz:question"
|
|
|
|
self.answer_qname = "quiz:answer"
|
|
|
|
|
|
|
|
async def get_all_question(self) -> List[Question]:
|
|
|
|
temp_list = []
|
|
|
|
qname = self.question_qname + "id_list"
|
2022-10-10 11:07:28 +00:00
|
|
|
data_list = [self.question_qname + f":{question_id}" for question_id in await self.client.lrange(qname, 0, -1)]
|
2022-07-26 10:07:31 +00:00
|
|
|
data = await self.client.mget(data_list)
|
|
|
|
for i in data:
|
2023-03-14 01:27:22 +00:00
|
|
|
temp_list.append(Question.parse_raw(i))
|
2022-07-26 10:07:31 +00:00
|
|
|
return temp_list
|
|
|
|
|
|
|
|
async def get_all_question_id_list(self) -> List[str]:
|
|
|
|
qname = self.question_qname + ":id_list"
|
|
|
|
return await self.client.lrange(qname, 0, -1)
|
|
|
|
|
|
|
|
async def get_one_question(self, question_id: int) -> Question:
|
|
|
|
qname = f"{self.question_qname}:{question_id}"
|
|
|
|
data = await self.client.get(qname)
|
2022-08-31 06:46:04 +00:00
|
|
|
json_data = str(data, encoding="utf-8")
|
2023-03-14 01:27:22 +00:00
|
|
|
return Question.parse_raw(json_data)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-31 06:46:04 +00:00
|
|
|
async def get_one_answer(self, answer_id: int) -> Answer:
|
2022-07-26 10:07:31 +00:00
|
|
|
qname = f"{self.answer_qname}:{answer_id}"
|
2022-08-31 06:46:04 +00:00
|
|
|
data = await self.client.get(qname)
|
|
|
|
json_data = str(data, encoding="utf-8")
|
2023-03-14 01:27:22 +00:00
|
|
|
return Answer.parse_raw(json_data)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-09 06:09:11 +00:00
|
|
|
async def add_question(self, question_list: List[Question] = None) -> int:
|
|
|
|
if not question_list:
|
|
|
|
return 0
|
2022-07-26 10:07:31 +00:00
|
|
|
for question in question_list:
|
2023-03-14 01:27:22 +00:00
|
|
|
await self.client.set(f"{self.question_qname}:{question.question_id}", question.json())
|
2022-07-26 10:07:31 +00:00
|
|
|
question_id_list = [question.question_id for question in question_list]
|
|
|
|
await self.client.lpush(f"{self.question_qname}:id_list", *question_id_list)
|
|
|
|
return await self.client.llen(f"{self.question_qname}:id_list")
|
|
|
|
|
|
|
|
async def del_all_question(self):
|
|
|
|
keys = await self.client.keys(f"{self.question_qname}*")
|
|
|
|
if keys is not None:
|
|
|
|
for key in keys:
|
|
|
|
await self.client.delete(key)
|
|
|
|
|
|
|
|
async def del_all_answer(self):
|
|
|
|
keys = await self.client.keys(f"{self.answer_qname}*")
|
|
|
|
if keys is not None:
|
|
|
|
for key in keys:
|
|
|
|
await self.client.delete(key)
|
|
|
|
|
2022-09-09 06:09:11 +00:00
|
|
|
async def add_answer(self, answer_list: List[Answer] = None) -> int:
|
|
|
|
if not answer_list:
|
|
|
|
return 0
|
2022-07-26 10:07:31 +00:00
|
|
|
for answer in answer_list:
|
2023-03-14 01:27:22 +00:00
|
|
|
await self.client.set(f"{self.answer_qname}:{answer.answer_id}", answer.json())
|
2022-07-26 10:07:31 +00:00
|
|
|
answer_id_list = [answer.answer_id for answer in answer_list]
|
|
|
|
await self.client.lpush(f"{self.answer_qname}:id_list", *answer_id_list)
|
|
|
|
return await self.client.llen(f"{self.answer_qname}:id_list")
|