日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php limit限流,php+redis 限流

發布時間:2024/9/3 php 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php limit限流,php+redis 限流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

## 思路

### 用戶請求限限流(同接口訪問時間限制)

> redis key 接品名+ user_id 每訪問一次+1 過期時間為限制訪問時間

```php

_user_id = (int)$params['user_id'];

$this->_search_key = $params['apiName'].$this->_prefix;

$this->_key = $this->_search_key.$params['user_id'];

$this->_expireat_time = 60;##配置項

$this->_user_max_limit = 100;##配置項

$this->_max_limit = 100;##配置項

}

/**

* [userLimit 是否達到單用戶接口請求峰值]

* @Author Jerry (wx621201)

* @DateTime 2019-08-02T15:35:52+0800

* @Example eg:

* @return [type] [description]

*/

public function checkUserLimit(){

if($this->_user_max_limit==0) return true;//0不限流,不存在防刷,可以購買

##如果不存在,說明超過一分鐘,給SQL攔截

$matchNum = $this->_exists();

$matchNum = (int)$matchNum?$matchNum:0;

if($matchNum>$this->_user_max_limit) return false;##達到限流條件

$this->_set($matchNum+1);

return true;##默認通過

}

/**

* [countLimit 是否達到用戶限流數]

* @Author Jerry (c84133883)

* @DateTime 2019-08-05T10:26:39+0800

* @Example eg:

* @return [type] [description]

*/

public function checkCountLimit(){

if($this->_max_limit==0) return true;//0不限流

if($this->_userCount>$this->_max_limit){

return false;

}else{

return true;

}

}

/**

* [_userCount 總用戶數]

* @Author Jerry (wx621201)

* @DateTime 2019-08-02T15:41:08+0800

* @Example eg:

* @return [type] [description]

*/

private function _userCount(){

$list = redis::scene($this->_scene)->keys($this->_search_key.'*');

return count($list);

}

/**

* [_set description]

* @Author Jerry (wx621201)

* @DateTime 2019-08-02T15:45:51+0800

* @Example eg:

* @param [type] $key [description]

* @param [type] $value [description]

* @param integer $time [description]

* @param string $scene [description]

*/

private function _set($value){

redis::scene($this->_scene)->set($this->_key,$value);

##處理過期時間

$expiretime = time()+(float)$this->_expireat_time;##1分鐘

redis::scene($this->_scene)->expireat($this->_key,$expiretime);

return ture;

}

/**

* [_exists 檢查是否存在]

* @Author Jerry (wx621201)

* @DateTime 2019-08-02T15:45:54+0800

* @Example eg:

* @param [type] $key [description]

* @param string $scene [description]

* @return [type] [description]

*/

private function _exists(){

if(!redis::scene($this->_scene)->exists($this->_key)||!redis::scene($this->_scene)->get($this->_key)){

return false;

}

return redis::scene($this->_scene)->get($this->_key);

}

}

```

### 總的用戶限流

#### 求合上面的key數,得出當前所在的用戶數量

### 所有請求限流

#### 思路1 通過IP限流

總結

以上是生活随笔為你收集整理的php limit限流,php+redis 限流的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。