Переглянути джерело

修改redis过期的key继续使用的bug

liu 2 місяців тому
батько
коміт
7c56797178

+ 5 - 0
src/main/java/com/sqx/common/utils/RedisUtils.java

@@ -155,4 +155,9 @@ public class RedisUtils {
     public Object rightPop(String key) {
         return redisTemplate.opsForList().rightPop(key);
     }
+
+    public long ttl(String key) {
+        Long redist = redisTemplate.getExpire(key);
+        return redist;
+    }
 }

+ 19 - 20
src/main/java/com/sqx/modules/common/service/impl/CommonInfoServiceImpl.java

@@ -77,12 +77,12 @@ public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo
 
     @Override
     public Result findByType(Integer type) {
-        return Result.success().put("data",commonInfoDao.findOne(type));
+        return Result.success().put("data", commonInfoDao.findOne(type));
     }
 
     @Override
     public Result findByTypeAndCondition(String condition) {
-        return Result.success().put("data",commonInfoDao.findByCondition(condition));
+        return Result.success().put("data", commonInfoDao.findByCondition(condition));
     }
 
     /**
@@ -95,29 +95,28 @@ public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo
     @Override
     public boolean limit(String key, int limit, int expire) {
         try {
-            log.info("1");
             // 生成Redis键
             String redisKey = "rate:limit:" + key;
-            log.info("2");
-            // 获取当前令牌数
-            Object currentValue = redisUtils.get(redisKey);
-            log.info("3");
-            int current = currentValue == null ? 0 : Integer.parseInt(currentValue.toString());
-            log.info("4");
+            long ttl = redisUtils.ttl(redisKey); // 获取剩余秒数
+            if(ttl == -1L ){
+                // 给 key = "order:123" 设置 300 秒过期
+                redisUtils.expire(redisKey,1);
+            }
             String count = commonInfoService.findOne(451).getValue();
             Integer limit2 = Integer.valueOf(count);
-            log.info("当前订单数:【{}】,总订单数:【{}】",current,limit2);
+
+            //使用Redis原子操作 令牌桶未满,允许通过,令牌数+1
+            Long current = redisUtils.increment(redisKey);// 自增并返回新值
+            // 设置过期时间(第一次设置)
+            if (current == 1) {
+                // 给 key = "order:123" 设置 300 秒过期
+                redisUtils.expire(redisKey,expire);
+                log.info("设置有效期,当前订单数:【{}】,总订单数:【{}】", current - 1, limit2);
+            }
+
+            log.info("当前订单数:【{}】,总订单数:【{}】", current - 1, limit2);
+
             if (current < limit2) {
-                log.info("5");
-                // 令牌桶未满,允许通过,令牌数+1
-                Long newValue = redisUtils.increment(redisKey);// 自增并返回新值
-                log.info("6");
-                // 设置过期时间(第一次设置)
-                if (current == 0) {
-                    log.info("7");
-                    // 给 key = "order:123" 设置 300 秒过期
-                    redisUtils.expire(redisKey,1);
-                }
                 log.info("8");
                 return true;
             } else {