Răsfoiți Sursa

支持刷新某考点某时段的“剩余可约数量”缓存

deason 1 lună în urmă
părinte
comite
98aaad3d60

+ 41 - 1
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -177,7 +177,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 刷新 某考点时段的“剩余可约数量”缓存
+     * 刷新 某考点下所有时段的“剩余可约数量”缓存
      */
     public void refreshApplyAvailableCountCache(Long examSiteId, int oldCapacity, int newCapacity) {
         String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSiteId);
@@ -224,6 +224,46 @@ public class ApplyTaskCacheService implements CacheConstants {
         }
     }
 
+    /**
+     * 刷新 某考点某时段的“剩余可约数量”缓存
+     */
+    public void refreshApplyAvailableCountCache(Long examSiteId, Long timePeriodId, int oldCapacity, int newCapacity) {
+        String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSiteId);
+        Lock lock = concurrentService.getLock(lockKey);
+
+        try {
+            if (!lock.tryLock()) {
+                throw new RuntimeException("获取锁失败,不允许同时操作!" + lockKey);
+            }
+
+            // 新、旧考点容量差额
+            int diffCapacity = newCapacity - oldCapacity;
+            String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
+            if (redisClient.exist(cacheKey)) {
+                if (diffCapacity == 0) {
+                    // 考点总容量未变化,不用更新缓存
+                    return;
+                }
+
+                // 总容量变化时,则更新
+                RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
+                long availableCount = Math.max(atomic.get() + diffCapacity, 0);
+                atomic.set(availableCount);
+                log.warn("更新考点时段剩余可约数量:{} {}", availableCount, cacheKey);
+            } else {
+                RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
+                atomic.set(newCapacity);
+                log.warn("初始考点时段剩余可约数量:{} {}", newCapacity, cacheKey);
+            }
+        } finally {
+            try {
+                lock.unlock();
+            } catch (Exception e) {
+                log.warn("解锁失败!lockKey:{} err:{}", lockKey, e.getMessage());
+            }
+        }
+    }
+
     /**
      * 初始或重置计算 所有考点所有时段的“剩余可约数量”缓存
      */