瀏覽代碼

apilimit改进

lizhen_gitee 1 年之前
父節點
當前提交
27861e8a1c
共有 2 個文件被更改,包括 12 次插入25 次删除
  1. 11 9
      application/common/controller/Api.php
  2. 1 16
      application/index/controller/Test.php

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

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

+ 1 - 16
application/index/controller/Test.php

@@ -25,22 +25,7 @@ class Test extends Controller
         echo $ipaddress;
     }
 
-    public function test_redis(){
-        $redis = new Redis();
-        $redisconfig = config("redis");
-        $redis->connect($redisconfig["host"], $redisconfig["port"]);
-        if ($redisconfig['redis_pwd']) {
-            $redis->auth($redisconfig['redis_pwd']);
-        }
-        if($redisconfig['redis_selectdb'] > 0){
-            $redis->select($redisconfig['redis_selectdb']);
-        }
-
-        $key = 'party_giveGiftToYou_180';
-
-        $redis->incr($key);
-        $redis->pExpire($key, 100000);
-    }
+
 
 
 }