## 一,系統配置
.env 中配置redis
~~~
[REDIS0]
TYPE = redis
HOST = 127.0.0.1
PORT = 6379
PASSWORD =
~~~
說明:劉宏締的架構森林是一個專注架構的博客,地址:[https://www.cnblogs.com/architectforest](https://www.cnblogs.com/architectforest)
? ? ? ? ?對應的源碼可以訪問這里獲取:?[https://github.com/liuhongdi/
](https://github.com/liuhongdi/)? ? ? ? ?或:?[https://gitee.com/liuhongdi](https://gitee.com/liuhongdi)
說明:作者:劉宏締 郵箱: 371125307@qq.com
## 二,編寫php代碼
1,controller/Goods.php
[](javascript:void(0); "復制代碼")
~~~
class Goods extends BaseController
{
/**
* 商品詳情
*
* @return \think\Response
*/
public function Detail(){
$rate = new RedisLuaRate();
$res = $rate->setRate();
$log = new BusinessLog("admin");
$log->log("rate res:".$res.":");
if ($res == "1") {
return Result::Success("成功");
} else {
return Result::Error("1","超出訪問頻率限制");
}
}
}
~~~
[](javascript:void(0); "復制代碼")
說明:此處僅做演示,實際應用時應該把這個攔截限流的功能放到middleware中
2,lib/util/RedisLuaRate.php
[](javascript:void(0); "復制代碼")
~~~
<?php
namespace app\lib\util;
class RedisLuaRate {
function setRate() {
$lua = <<<SCRIPT
local key = KEYS[1];
local limit = tonumber(KEYS[2])
local length = tonumber(KEYS[3])
--redis.log(redis.LOG_NOTICE,' length: '..length)
local current = redis.call('GET', key)
if current == false then
--redis.log(redis.LOG_NOTICE,key..' is nil ')
redis.call('SET', key,1)
redis.call('EXPIRE',key,length)
--redis.log(redis.LOG_NOTICE,' set expire end')
return '1'
else
--redis.log(redis.LOG_NOTICE,key..' value: '..current)
local num_current = tonumber(current)
if num_current+1 > limit then
return '0'
else
redis.call('INCRBY',key,1)
return '1'
end
end
SCRIPT;
$redis = new \Redis();
$redis->connect(env('redis0.host', '127.0.0.1'),env('redis0.port', '6379'));
$redis->select(0);
$key = "ip".request()->ip();
$sequence = $redis->eval($lua, [$key,3,1], 3);
$luaError = $redis->getLastError();
if (isset($luaError)) {
// print_r($luaError);
}
return $sequence;
}
}
~~~
[](javascript:void(0); "復制代碼")
說明:?eval函數的第3個參數為KEYS個數。 phpredis依據此值將KEYS和ARGV做區分
## 三,測試效果
1,用ab做并發測試:
[](javascript:void(0); "復制代碼")
~~~
liuhongdi@lhdpc:/data/php/admapi/app$ ab -c 10 -n 10 http://192.168.219.6:8000/goods/detail?goodsid=12323
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.219.6 (be patient).....done
Server Software: nginx/1.18.0
Server Hostname: 192.168.219.6
Server Port: 8000
Document Path: /goods/detail?goodsid=12323
Document Length: 37 bytes
Concurrency Level: 10
Time taken for tests: 0.059 seconds
Complete requests: 10
Failed requests: 7
(Connect: 0, Receive: 0, Length: 7, Exceptions: 0)
Total transferred: 2276 bytes
HTML transferred: 496 bytes
Requests per second: 169.79 [#/sec] (mean)
Time per request: 58.897 [ms] (mean)
Time per request: 5.890 [ms] (mean, across all concurrent requests)
Transfer rate: 37.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.5 1 2
Processing: 12 26 11.0 25 44
Waiting: 11 26 11.2 25 44
Total: 13 27 10.9 26 45
Percentage of the requests served within a certain time (ms)
50% 26
66% 29
75% 37
80% 42
90% 45
95% 45
98% 45
99% 45
100% 45 (longest request)
~~~
[](javascript:void(0); "復制代碼")
2,查看代碼在日志中寫入的結果:
[](javascript:void(0); "復制代碼")
~~~
2022-01-24 16:37:50--192.168.219.6--rate res:1:
2022-01-24 16:37:50--192.168.219.6--rate res:1:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:1:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
2022-01-24 16:37:50--192.168.219.6--rate res:0:
~~~
[](javascript:void(0); "復制代碼")
可以看到在同一秒鐘內,有三次返回成功,其余是返回失敗
## 四,查看php和thinkphp的版本?
php:
~~~
liuhongdi@lhdpc:/data/php/admapi$ php --version
PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies?
~~~
thinkphp:
~~~
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/
liuhongdi@lhdpc:/data/php/admapi$ php think version
v6.0.10LTS
~~~
- thinkphp6執行流程(一)
- php中use關鍵字用法詳解
- Thinkphp6使用騰訊云發送短信步驟
- 路由配置
- Thinkphp6,static靜態資源訪問路徑問題
- ThinkPHP6.0+ 使用Redis 原始用法
- smarty在thinkphp6.0中的最佳實踐
- Thinkphp6.0 搜索器使用方法
- 從已有安裝包(vendor)恢復 composer.json
- tp6with的用法,表間關聯查詢
- thinkphp6.x多對多如何添加中間表限制條件
- thinkphp6 安裝JWT
- 緩存類型
- 請求信息和HTTP頭信息
- 模型事件用法
- 助手函數匯總
- tp6集成Alipay 手機和電腦端支付的方法
- thinkphp6使用jwt
- 6.0session cookie cache
- tp6筆記
- TP6(thinkphp6)隊列與延時隊列
- thinkphp6 command(自定義指令)
- command(自定義指令)
- 本地文件上傳
- 緩存
- 響應
- 公共函數配置
- 七牛云+文件上傳
- thinkphp6:訪問多個redis數據源(thinkphp6.0.5 / php 7.4.9)
- 富文本編輯器wangEditor3
- IP黑名單
- 增刪改查 +文件上傳
- workerman 定時器操作控制器的方法
- 上傳文件到阿里云oss
- 短信或者郵箱驗證碼防刷代碼
- thinkphp6:訪問redis6(thinkphp 6.0.9/php 8.0.14)
- 實現關聯多個id以逗號分開查詢數據
- thinkphp6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 用mpdf生成pdf文件(php 8.1.1 / thinkphp v6.0.10LTS )
- 生成帶logo的二維碼(php 8.1.1 / thinkphp v6.0.10LTS )
- mysql數據庫使用事務(php 8.1.1 / thinkphp v6.0.10LTS)
- 一,創建過濾IP的中間件
- 源碼解析請求流程
- 驗證碼生成
- 權限管理
- 自定義異常類
- 事件監聽event-listene
- 安裝與使用think-addons
- 事件與多應用
- Workerman 基本使用
- 查詢用戶列表按拼音字母排序
- 擴展包合集
- 查詢用戶數據,但是可以通過輸入用戶昵稱來搜索用戶同時還要統計用戶的文章和粉絲數
- 根據圖片的minetype類型獲取文件真實拓展名思路
- 到處excel
- 用imagemagick庫生成縮略圖
- 生成zip壓縮包并下載
- API 多版本控制
- 用redis+lua做限流(php 8.1.1 / thinkphp v6.0.10LTS )
- 【thinkphp6源碼分析三】 APP類之父, 容器Container類
- thinkphp6表單重復提交解決辦法
- 小程序授權
- 最簡單的thinkphp6導出Excel
- 根據訪問設備不同訪問不同模塊
- 服務系統
- 前置/后置中間件
- 給接口api做簽名驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- 6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 使用前后端分離的驗證碼(thinkphp 6.0.9/php 8.0.14/vue 3.2.26)
- 前后端分離:用jwt+middleware做用戶登錄驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- vue前后端分離多圖上傳
- thinkphp 分組、頁面跳轉與ajax
- thinkphp6 常用方法文檔
- 手冊里沒有的一些用法
- Swagger 3 API 注釋
- PHP 秒級定時任務
- thinkphp6集成gatewayWorker(workerman)實現實時監聽
- thinkphp6按月新增數據表
- 使用redis 實現消息隊列
- api接口 統一結果返回處理類
- 使用swoole+thinkphp6.0+redis 結合開發的登錄模塊
- 給接口api做簽名驗證
- ThinkPHP6.0 + UniApp 實現小程序的 微信登錄
- ThinkPHP6.0 + Vue + ElementUI + axios 的環境安裝到實現 CURD 操作!
- 異常$e
- 參數請求驗證自定義和異常錯誤自定義