Browse Source

apilimit优化

lizhen_gitee 1 year ago
parent
commit
0dc9c4a4be
2 changed files with 20 additions and 18 deletions
  1. 10 9
      application/common/controller/Api.php
  2. 10 9
      application/common/controller/Apic.php

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

@@ -470,17 +470,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;
     }
 

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

@@ -444,17 +444,18 @@ class Apic
         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;
     }