|
@@ -1,11 +1,11 @@
|
|
|
package cn.com.qmth.examcloud.core.oe.student.service.impl;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.base.utils.QuestionTypeUtil;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.entity.ExamQuestionEntity;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.entity.ExamRecordDataEntity;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.entity.ExamRecordQuestionsEntity;
|
|
|
-import cn.com.qmth.examcloud.core.oe.common.entity.ExamStudentEntity;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.enums.ExamType;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.repository.ExamRecordDataRepo;
|
|
|
import cn.com.qmth.examcloud.core.oe.common.repository.ExamRecordQuestionsRepo;
|
|
@@ -16,8 +16,6 @@ import cn.com.qmth.examcloud.core.oe.student.bean.ExamStudentQuestionInfo;
|
|
|
import cn.com.qmth.examcloud.core.oe.student.service.ExamRecordQuestionsService;
|
|
|
import cn.com.qmth.examcloud.core.oe.student.service.ExamSessionInfoService;
|
|
|
import cn.com.qmth.examcloud.core.questions.api.ExtractConfigCloudService;
|
|
|
-import cn.com.qmth.examcloud.core.questions.api.request.GetQuestionReq;
|
|
|
-import cn.com.qmth.examcloud.core.questions.api.response.GetQuestionResp;
|
|
|
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
|
|
|
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionGroup;
|
|
|
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionStructureWrapper;
|
|
@@ -27,17 +25,18 @@ import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestionUnit;
|
|
|
import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
|
import cn.com.qmth.examcloud.support.cache.bean.QuestionCacheBean;
|
|
|
import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.google.common.cache.Cache;
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Comparator;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author chenken
|
|
@@ -69,6 +68,10 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
|
|
|
@Autowired
|
|
|
private ExamStudentRepo examStudentRepo;
|
|
|
|
|
|
+ //本地题干缓存,最大200道题,默认缓存120秒
|
|
|
+ private Cache<String, String> questionContentCache = CacheBuilder.newBuilder().maximumSize(200)
|
|
|
+ .expireAfterWrite(120, TimeUnit.SECONDS).build();
|
|
|
+
|
|
|
// @Value("${exam_question_key_prefix}")
|
|
|
// private String examQuestionKeyPrefix;
|
|
|
|
|
@@ -169,8 +172,8 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
|
|
|
@Override
|
|
|
public void submitQuestionAnswer(Long studentId, List<ExamStudentQuestionInfo> examQuestionInfos) {
|
|
|
ExamSessionInfo examSessionInfo = examSessionInfoService.getExamSessionInfo(studentId);
|
|
|
- if (null == examSessionInfo){
|
|
|
- throw new StatusException("200001","考试会话已过期");
|
|
|
+ if (null == examSessionInfo) {
|
|
|
+ throw new StatusException("200001", "考试会话已过期");
|
|
|
}
|
|
|
long examRecordDataId = examSessionInfo.getExamRecordDataId();
|
|
|
|
|
@@ -192,24 +195,37 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public DefaultQuestionStructure getQuestionContent(Long studentId, String questionId) {
|
|
|
+ public String getQuestionContent(Long studentId, String questionId) {
|
|
|
ExamSessionInfo examSessionInfo = examSessionInfoService.getExamSessionInfo(studentId);
|
|
|
if (examSessionInfo == null) {
|
|
|
throw new StatusException("100002", "考试已结束,不允许获取试题内容");
|
|
|
}
|
|
|
|
|
|
- QuestionCacheBean getQuestionResp = CacheHelper.getQuestion(examSessionInfo.getExamId(),
|
|
|
- examSessionInfo.getCourseCode(), examSessionInfo.getPaperType(), questionId);
|
|
|
-
|
|
|
- DefaultQuestionStructure questionStructure = getQuestionResp.getDefaultQuestion().getMasterVersion();
|
|
|
- List<DefaultQuestionUnit> questionUnits = questionStructure.getQuestionUnitList();
|
|
|
+ //本地缓存key规则:examId_courseCode_paperType_questionId
|
|
|
+ String cacheKey = examSessionInfo.getExamId() + "_" + examSessionInfo.getCourseCode()
|
|
|
+ + "_" + examSessionInfo.getPaperType() + "_" + questionId;
|
|
|
+ String contentJson = questionContentCache.getIfPresent(cacheKey);
|
|
|
|
|
|
- //在线考试,清除答案
|
|
|
- if (examSessionInfo.getExamType().equals(ExamType.ONLINE.name())) {
|
|
|
- for (DefaultQuestionUnit questionUnit : questionUnits) {
|
|
|
- questionUnit.setRightAnswer(null);
|
|
|
+ //如果本地缓存中存在,则从本地缓存中获取
|
|
|
+ if (StringUtils.isNotEmpty(contentJson)){
|
|
|
+ return contentJson;
|
|
|
+ }
|
|
|
+ //如果本地缓存不存在,则从redis中获取, 并在本地缓存存在存储一份
|
|
|
+ else {
|
|
|
+ QuestionCacheBean getQuestionResp = CacheHelper.getQuestion(examSessionInfo.getExamId(),
|
|
|
+ examSessionInfo.getCourseCode(), examSessionInfo.getPaperType(), questionId);
|
|
|
+ DefaultQuestionStructure questionStructure = getQuestionResp.getDefaultQuestion().getMasterVersion();
|
|
|
+ List<DefaultQuestionUnit> questionUnits = questionStructure.getQuestionUnitList();
|
|
|
+
|
|
|
+ //在线考试,清除答案
|
|
|
+ if (examSessionInfo.getExamType().equals(ExamType.ONLINE.name())) {
|
|
|
+ for (DefaultQuestionUnit questionUnit : questionUnits) {
|
|
|
+ questionUnit.setRightAnswer(null);
|
|
|
+ }
|
|
|
}
|
|
|
+ String resultJson = JsonUtil.toJson(questionStructure);
|
|
|
+ questionContentCache.put(cacheKey, resultJson);
|
|
|
+ return resultJson;
|
|
|
}
|
|
|
- return questionStructure;
|
|
|
}
|
|
|
}
|