Browse Source

apilimit优化

lizhen_gitee 1 year ago
parent
commit
6a3d345135
1 changed files with 10 additions and 9 deletions
  1. 10 9
      application/common/controller/Api.php

+ 10 - 9
application/common/controller/Api.php

@@ -428,17 +428,18 @@ class Api
         if($redisconfig['redis_selectdb'] > 0){
             $redis->select($redisconfig['redis_selectdb']);
         }
-        $check = $redis->exists($key);
-        if ($check) {
-            $redis->incr($key);
-            $count = $redis->get($key);
-            if ($count > $apiLimit) {
-                return false;
-            }
-        } else {
-            $redis->incr($key);
+        //
+        //指定键值新增+1 并获取
+        $count = $redis->incr($key);
+        if ($count > $apiLimit) {
+            return false;
+        }
+
+        //设置过期时间
+        if ($count == 1) {
             $redis->pExpire($key, $apiLimitTime);
         }
+
         return true;
     }