Bladeren bron

添加题库缓存

deason 5 jaren geleden
bovenliggende
commit
146fa6ca71

+ 42 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/cache/BasePaperCache.java

@@ -0,0 +1,42 @@
+package cn.com.qmth.examcloud.core.questions.service.cache;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
+import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
+import cn.com.qmth.examcloud.support.cache.bean.BasePaperCacheBean;
+import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class BasePaperCache extends RandomObjectRedisCache<BasePaperCacheBean> {
+    @Autowired
+    private ExtractConfigProviderService extractConfigProviderService;
+
+    @Override
+    public BasePaperCacheBean loadFromResource(Object... keys) {
+        String paperId = String.valueOf(keys[0]);
+
+        if (StringUtils.isBlank(paperId)) {
+            throw new StatusException("400", "paperId is empty");
+        }
+
+        DefaultPaper defaultPaper = extractConfigProviderService.getBaseDefaultPaper(paperId);
+
+        BasePaperCacheBean cacheBean = new BasePaperCacheBean();
+        cacheBean.setDefaultPaper(defaultPaper);
+        return cacheBean;
+    }
+
+    @Override
+    protected String getKeyPrefix() {
+        return "Q_BASE_PAPER:";
+    }
+
+    @Override
+    protected int getTimeout() {
+        return 5 * 60;// N分钟
+    }
+
+}

+ 57 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/cache/PaperCache.java

@@ -0,0 +1,57 @@
+package cn.com.qmth.examcloud.core.questions.service.cache;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
+import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
+import cn.com.qmth.examcloud.support.cache.bean.PaperCacheBean;
+import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class PaperCache extends RandomObjectRedisCache<PaperCacheBean> {
+    @Autowired
+    private ExtractConfigProviderService extractConfigProviderService;
+
+    @Override
+    public PaperCacheBean loadFromResource(Object... keys) {
+        Long examId = (Long) keys[0];
+        String courseCode = String.valueOf(keys[1]);
+        String groupCode = String.valueOf(keys[2]);
+
+        if (examId == null) {
+            throw new StatusException("400", "examId is null");
+        }
+
+        if (StringUtils.isBlank(courseCode)) {
+            throw new StatusException("400", "courseCode is empty");
+        }
+
+        if (StringUtils.isBlank(groupCode)) {
+            throw new StatusException("400", "groupCode is empty");
+        }
+
+        Map<String, Object> map = extractConfigProviderService.getDefaultPaper(examId, courseCode, groupCode);
+
+        PaperCacheBean cacheBean = new PaperCacheBean();
+        cacheBean.setDefaultPaper((DefaultPaper) map.get("defaultPaper"));
+        cacheBean.setPaperId((String) map.get("paperId"));
+        cacheBean.setSortQuestionOrder((Short) map.get("upSetOptionOrder") == 0 ? false : true);
+        cacheBean.setSortOptionOrder((Short) map.get("upSetOptionOrder") == 0 ? false : true);
+        return cacheBean;
+    }
+
+    @Override
+    protected String getKeyPrefix() {
+        return "Q_PAPER:";
+    }
+
+    @Override
+    protected int getTimeout() {
+        return 5 * 60;// N分钟
+    }
+
+}

+ 57 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/cache/QuestionCache.java

@@ -0,0 +1,57 @@
+package cn.com.qmth.examcloud.core.questions.service.cache;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
+import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestion;
+import cn.com.qmth.examcloud.support.cache.bean.QuestionCacheBean;
+import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class QuestionCache extends RandomObjectRedisCache<QuestionCacheBean> {
+    @Autowired
+    private ExtractConfigProviderService extractConfigProviderService;
+
+    @Override
+    public QuestionCacheBean loadFromResource(Object... keys) {
+        Long examId = (Long) keys[0];
+        String courseCode = String.valueOf(keys[1]);
+        String groupCode = String.valueOf(keys[2]);
+        String questionId = String.valueOf(keys[3]);
+
+        if (examId == null) {
+            throw new StatusException("400", "examId is null");
+        }
+
+        if (StringUtils.isBlank(courseCode)) {
+            throw new StatusException("400", "courseCode is empty");
+        }
+
+        if (StringUtils.isBlank(groupCode)) {
+            throw new StatusException("400", "groupCode is empty");
+        }
+
+        if (StringUtils.isBlank(questionId)) {
+            throw new StatusException("400", "questionId is empty");
+        }
+
+        DefaultQuestion defaultQuestion = extractConfigProviderService.getDefaultQuestion(examId, courseCode, groupCode, questionId);
+
+        QuestionCacheBean cacheBean = new QuestionCacheBean();
+        cacheBean.setDefaultQuestion(defaultQuestion);
+        return cacheBean;
+    }
+
+    @Override
+    protected String getKeyPrefix() {
+        return "Q_QUESTION:";
+    }
+
+    @Override
+    protected int getTimeout() {
+        return 5 * 60;// N分钟
+    }
+
+}

+ 58 - 0
examcloud-core-questions-starter/src/main/java/cn/com/qmth/examcloud/core/questions/starter/DemoController.java

@@ -0,0 +1,58 @@
+package cn.com.qmth.examcloud.core.questions.starter;
+
+import cn.com.qmth.examcloud.core.questions.service.cache.BasePaperCache;
+import cn.com.qmth.examcloud.core.questions.service.cache.PaperCache;
+import cn.com.qmth.examcloud.core.questions.service.cache.QuestionCache;
+import cn.com.qmth.examcloud.support.cache.CacheHelper;
+import cn.com.qmth.examcloud.support.cache.bean.BasePaperCacheBean;
+import cn.com.qmth.examcloud.support.cache.bean.PaperCacheBean;
+import cn.com.qmth.examcloud.support.cache.bean.QuestionCacheBean;
+import cn.com.qmth.examcloud.web.support.Naked;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Controller
+@RequestMapping("${api_cqb}/")
+public class DemoController {
+    private static final Logger log = LoggerFactory.getLogger(DemoController.class);
+    @Autowired
+    private BasePaperCache basePaperCache;
+    @Autowired
+    private PaperCache paperCache;
+    @Autowired
+    private QuestionCache questionCache;
+
+    @Naked
+    @ResponseBody
+    @RequestMapping(value = "/demo", method = RequestMethod.GET)
+    public String demo(HttpServletRequest request, HttpServletResponse response) throws Exception {
+        Long examId = 307L;
+        String courseCode = "course-0815-feng";
+        String groupCode = "X";
+        String paperId = "2085113d-b74c-4dea-9f94-b22bb4825f2a";
+        String questionId = "5d54d08def8fce19f8b3ab98";
+
+        PaperCacheBean paperCacheBean = CacheHelper.getPaper(examId, courseCode, groupCode);
+        log.info("paperId: " + paperCacheBean.getPaperId());
+        //paperCache.remove(examId, courseCode, groupCode);
+
+        BasePaperCacheBean basePaperCacheBean = CacheHelper.getBasePaper(paperId);
+        log.info("basePaperName: " + basePaperCacheBean.getDefaultPaper().getName());
+        //basePaperCache.remove(paperId);
+
+        QuestionCacheBean questionCacheBean = CacheHelper.getQuestion(examId, courseCode, groupCode, questionId);
+        log.info("questionId: " + questionCacheBean.getDefaultQuestion().getId());
+        //questionCache.remove(questionId);
+
+        return "ok";
+    }
+
+}