瀏覽代碼

update cache

deason 5 年之前
父節點
當前提交
a16c28a96f

+ 7 - 16
examcloud-core-questions-api-provider/src/main/java/cn/com/qmth/examcloud/core/questions/api/PaperDetailUnitController.java

@@ -1,24 +1,16 @@
 package cn.com.qmth.examcloud.core.questions.api;
 
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailUnitExp;
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
-import cn.com.qmth.examcloud.core.questions.service.PaperDetailUnitService;
 import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
-import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.core.questions.service.PaperDetailUnitService;
+import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailUnitExp;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
 
 /**
  * Created by songyue on 16/12/28.
@@ -86,7 +78,6 @@ public class PaperDetailUnitController extends ControllerSupport {
     /**
      * 删除小题
      *
-     * @param unit_id
      * @return
      */
     @ApiOperation(value = "删除小题", notes = "删除小题")

+ 35 - 5
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/PaperDetailUnitServiceImpl.java

@@ -14,6 +14,7 @@ import cn.com.qmth.examcloud.core.questions.service.PaperService;
 import cn.com.qmth.examcloud.core.questions.service.QuesService;
 import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailUnitExp;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
+import org.apache.commons.collections.CollectionUtils;
 import org.bson.types.ObjectId;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -23,11 +24,15 @@ import org.springframework.data.domain.Sort.Order;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+
+import static cn.com.qmth.examcloud.core.questions.service.cache.Constants.CACHE_PREFIX_QUESTION;
 
 /**
  * @author chenken
@@ -50,6 +55,9 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
     @Autowired
     MongoTemplate mongoTemplate;
 
+    @Autowired
+    RedisTemplate<String, Object> redisTemplate;
+
     @Autowired
     RedisClient redisClient;
 
@@ -87,7 +95,8 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
         if (baseUnit.getQuestionType() == QuesStructType.NESTED_ANSWER_QUESTION) {
             if (updateQuestion.getId().equals(baseQuestion.getId())) {
                 // 更新对象为套题本身
-                baseUnit.setQuestion(quesService.saveQues(updateQuestion));
+                quesService.saveQues(updateQuestion);
+                baseUnit.setQuestion(updateQuestion);
             } else {
                 List<Double> subScoreList = baseUnit.getSubScoreList();
                 int size = baseQuestion.getSubQuestions().size();
@@ -134,14 +143,16 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
                 }
 
                 if (match) {
-                    baseUnit.setQuestion(quesService.saveQues(baseQuestion));
+                    quesService.saveQues(baseQuestion);
+                    baseUnit.setQuestion(baseQuestion);
                 }
 
                 // 更新套题unit当前总分与子题分数
                 baseUnit.setSubScoreList(subScoreList);
             }
         } else {
-            baseUnit.setQuestion(quesService.saveQues(updateQuestion));
+            quesService.saveQues(updateQuestion);
+            baseUnit.setQuestion(updateQuestion);
             baseUnit.setScore(updateUnit.getScore());
         }
 
@@ -151,6 +162,11 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
         Paper paper = baseUnit.getPaper();
         paperService.formatPaper(paper, user);
 
+        if (baseUnit.getQuestion() != null) {
+            //清除缓存
+            this.clearQuestionCache(baseUnit.getQuestion().getId());
+        }
+
         return baseUnit;
     }
 
@@ -161,7 +177,8 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
      * @return
      */
     public void deletePaperDetailUnit(String id, User user) {
-        Paper paper = Model.of(paperDetailUnitRepo.findById(id)).getPaper();
+        PaperDetailUnit detailUnit = Model.of(paperDetailUnitRepo.findById(id));
+        Paper paper = detailUnit.getPaper();
         paperDetailUnitRepo.deleteById(id);
 
         //清除缓存
@@ -169,6 +186,10 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
             redisClient.delete(redisPaperPrefix + ":" + paper.getId());
         }
 
+        if (detailUnit.getQuestion() != null) {
+            this.clearQuestionCache(detailUnit.getQuestion().getId());
+        }
+
         paperService.formatPaper(paper, user);
     }
 
@@ -320,5 +341,14 @@ public class PaperDetailUnitServiceImpl implements PaperDetailUnitService {
         List<PaperDetailUnit> paperDetailUnits = mongoTemplate.find(query, PaperDetailUnit.class);
         return paperDetailUnits;
     }
-}
 
+    private void clearQuestionCache(String questionId) {
+        //清理与当前试题相关的缓存
+        final String patternKey = CACHE_PREFIX_QUESTION + "*" + questionId;
+        Set<String> keys = redisTemplate.keys(patternKey);
+        if (CollectionUtils.isNotEmpty(keys)) {
+            redisTemplate.delete(keys);
+        }
+    }
+
+}