deason 1 year ago
parent
commit
9de1ac4a0f

+ 3 - 2
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -52,8 +52,9 @@ public interface CacheConstants {
     String LOCK_AUTO_APPLY = "auto_apply";
 
     /**
-     * 自动排考前缀:{yyyyMMdd}
+     * 自动排考
+     * $lock:arrange_exam_{yyyyMMdd}
      */
-    String LOCK_ARRANGE_EXAM = "LOCK:ARRANGE_EXAM:";
+    String LOCK_ARRANGE_EXAM = "arrange_exam_%s";
 
 }

+ 12 - 8
src/main/java/com/qmth/exam/reserve/cache/RedisClient.java

@@ -1,14 +1,14 @@
 package com.qmth.exam.reserve.cache;
 
-import java.util.concurrent.TimeUnit;
-
-import javax.annotation.Resource;
-
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
+
 @Component
 public class RedisClient {
 
@@ -17,8 +17,8 @@ public class RedisClient {
     @Resource
     private RedisTemplate<String, Object> redisTemplate;
 
-    // @Resource
-    // private RedissonClient redissonClient;
+    @Resource
+    private RedissonClient redissonClient;
 
     public void set(String key, Object value) {
         redisTemplate.opsForValue().set(key, value);
@@ -50,8 +50,12 @@ public class RedisClient {
         return redisTemplate.hasKey(key);
     }
 
-    public boolean tryLock(String key, Object value, long timeout, TimeUnit timeUnit) {
-        return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit).booleanValue();
+    public RedisTemplate<String, Object> getRedisTemplate() {
+        return redisTemplate;
+    }
+
+    public RedissonClient getRedissonClient() {
+        return redissonClient;
     }
 
 }

+ 6 - 3
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -132,6 +132,9 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 throw new StatusException("当前预约时段已约满");
             }
 
+            // 某考点某时段的“已预约数量” 累加1(优先执行抢占1个数量)
+            applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
+
             LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
             wrapper.eq(StudentApplyEntity::getStudentId, student.getId());
             wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
@@ -139,6 +142,9 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             StudentApplyEntity existEntity = studentApplyService.getOne(wrapper);
             if (existEntity != null) {
                 if (!existEntity.getCancel()) {
+                    // 某考点某时段的“已预约数量” 累减1(释放1个数量)
+                    applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
+
                     log.warn("预约失败,当前时段已预约,请勿重复预约!applyId:{}", existEntity.getId());
                     throw new StatusException("当前时段已预约,请勿重复预约");
                 }
@@ -156,9 +162,6 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 studentApplyService.save(entity);
                 log.warn("新增考生预约记录!applyId:{} examSiteId:{} timePeriodId:{}", entity.getId(), examSiteId, timePeriodId);
             }
-
-            // 某考点某时段的“已预约数量” 累加1
-            applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
         } finally {
             studentApplyLock.unlock();
         }