Browse Source

update caches

deason 1 năm trước cách đây
mục cha
commit
2dddf10ebc

+ 23 - 0
src/main/java/com/qmth/exam/reserve/bean/apply/ApplyRecordCacheBean.java

@@ -0,0 +1,23 @@
+package com.qmth.exam.reserve.bean.apply;
+
+import com.qmth.exam.reserve.bean.IModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class ApplyRecordCacheBean implements IModel {
+
+    private static final long serialVersionUID = 2804383719693396546L;
+
+    @ApiModelProperty(value = "考点ID")
+    private Long examSiteId;
+
+    @ApiModelProperty(value = "预约时段ID")
+    private Long timePeriodId;
+
+    @ApiModelProperty(value = "是否已取消预约")
+    private Boolean cancel;
+
+}

+ 7 - 0
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -47,6 +47,13 @@ public interface CacheConstants {
      */
     String CACHE_STUDENT_APPLY_NUMBER = "$cache:student_apply_number:%s";
 
+    /**
+     * 某考生的“预约记录”的缓存
+     * $cache:student_apply_record:{studentId}
+     * hashKey:{examSiteId}_{timePeriodId}
+     */
+    String CACHE_STUDENT_APPLY_RECORD = "$cache:student_apply_record:%s";
+
     /**
      * 某考生预约操作锁
      * $lock:student_apply:{studentId}

+ 29 - 0
src/main/java/com/qmth/exam/reserve/cache/RedisClient.java

@@ -7,6 +7,8 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 @Component
@@ -38,6 +40,33 @@ public class RedisClient {
         return object;
     }
 
+    public <T> Map<String, T> getEntriesForHash(String key, Class<T> clazz) {
+        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
+
+        Map<String, T> result = new HashMap<>();
+        if (entries.isEmpty()) {
+            return result;
+        }
+
+        for (Map.Entry<Object, Object> entry : entries.entrySet()) {
+            result.put((String) entry.getKey(), (T) entry.getValue());
+        }
+
+        return result;
+    }
+
+    public <T> T getForHash(String key, String hashKey, Class<T> clazz) {
+        return (T) redisTemplate.opsForHash().get(key, hashKey);
+    }
+
+    public void setForHash(String key, String hashKey, Object value) {
+        redisTemplate.opsForHash().put(key, hashKey, value);
+    }
+
+    public void deleteForHash(String key, String hashKey) {
+        redisTemplate.opsForHash().delete(key, hashKey);
+    }
+
     public void expire(String key, long timeout, TimeUnit timeUnit) {
         redisTemplate.expire(key, timeout, timeUnit);
     }

+ 50 - 0
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -1,18 +1,24 @@
 package com.qmth.exam.reserve.cache.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.qmth.exam.reserve.bean.apply.ApplyRecordCacheBean;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.RedisClient;
+import com.qmth.exam.reserve.entity.StudentApplyEntity;
 import com.qmth.exam.reserve.service.ApplyTaskService;
 import com.qmth.exam.reserve.service.ExamSiteService;
 import com.qmth.exam.reserve.service.StudentApplyService;
 import com.qmth.exam.reserve.service.StudentService;
+import org.apache.commons.collections4.CollectionUtils;
 import org.redisson.api.RAtomicLong;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 @Component
@@ -145,4 +151,48 @@ public class ApplyTaskCacheService implements CacheConstants {
         atomic.decrementAndGet();
     }
 
+    /**
+     * 获取某考生的 所有的“预约记录”缓存
+     */
+    public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
+        if (!redisClient.exist(cacheKey)) {
+            LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(StudentApplyEntity::getStudentId, studentId);
+            List<StudentApplyEntity> list = studentApplyService.list(wrapper);
+
+            if (CollectionUtils.isNotEmpty(list)) {
+                for (StudentApplyEntity entity : list) {
+                    ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
+                    bean.setTimePeriodId(entity.getTimePeriodId());
+                    bean.setExamSiteId(entity.getExamSiteId());
+                    bean.setCancel(entity.getCancel());
+                    this.saveStudentApplyRecord(studentId, bean);
+                }
+            }
+            redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
+        }
+
+        return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
+    }
+
+    /**
+     * 获取某考生的 某考点某时段的“预约记录”缓存
+     */
+    public ApplyRecordCacheBean getStudentApplyRecord(Long studentId, Long examSiteId, Long timePeriodId) {
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
+        String hashKey = String.format("%s_%s", examSiteId, timePeriodId);
+        return redisClient.getForHash(cacheKey, hashKey, ApplyRecordCacheBean.class);
+    }
+
+    /**
+     * 保存某考生的 某考点某时段的“预约记录”缓存
+     */
+    public void saveStudentApplyRecord(Long studentId, ApplyRecordCacheBean value) {
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
+        String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
+        redisClient.setForHash(cacheKey, hashKey, value);
+        log.info("SET cacheKey:{} hashKey:{}", cacheKey, hashKey);
+    }
+
 }