当前位置:网站首页>laravel8更新之速率限制改进
laravel8更新之速率限制改进
2020-11-08 13:07:00 【神兵小将】
原文地址:https://www.wjcms.net/archives/laravel8%E6%9B%B4%E6%96%B0%E4%B9%8B%E9%80%9F%E7%8E%87%E9%99%90%E5%88%B6%E6%94%B9%E8%BF%9B
Laravel的请求速率限制器功能已增强,具有更大的灵活性和功能,同时仍保持与先前版本的throttle中间件API的向后兼容性。
速率限制器是使用RateLimiter立面的for方法定义的。该for方法接受一个速率限制器名称和一个Closure,该Closure返回应应用于分配了该速率限制器的路由的限制配置:
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(1000);
});
由于速率限制器回调接收传入的HTTP请求实例,因此您可以根据传入的请求或经过身份验证的用户动态构建适当的速率限制:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100);
});
有时您可能希望将速率限制按任意值进行细分。例如,您可能希望允许用户每个IP地址每分钟100次访问给定路由。为此,您可以by在建立速率限制时使用以下方法:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100)->by($request->ip());
});
可以使用throttle 中间件将速率限制器附加到路由或路由组。油门中间件接受您希望分配给路线的速率限制器的名称:
Route::middleware(['throttle:uploads'])->group(function () {
Route::post('/audio', function () {
//
});
Route::post('/video', function () {
//
});
});
版权声明
本文为[神兵小将]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3380391/blog/4707811
边栏推荐
- Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
- On the confirmation of original data assets
- 笔试面试题目:求丢失的猪
- 阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
- C language I blog assignment 03
- Adobe media encoder /Me 2021软件安装包(附安装教程)
- Analysis of istio access control
- Google's AI model, which can translate 101 languages, is only one more than Facebook
猜你喜欢

Ali! Visual computing developer's series of manuals (with internet disk link)

Tight supply! Apple's iPhone 12 power chip capacity exposed

新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务

【Python 1-6】Python教程之——数字

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

Written interview questions: find the smallest positive integer missing

Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)

为 Docsify 自动生成 RSS 订阅

蘑菇街电商交易平台服务架构及改造优化历程(含PPT)

Python basic syntax
随机推荐
我们做了一个医疗版MNIST数据集,发现常见AutoML算法没那么好用
10个常见的软件架构模式
C language I blog assignment 03
笔试面试题目:判断单链表是否有环
小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果
Automatically generate RSS feeds for docsify
Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
阿里教你深入浅出玩转物联网平台!(附网盘链接)
Understanding design patterns
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
STM32CubeIDE下载安装-GPIO基本配置操作-Debug调试(基于CMSIS DAP Debug)
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
谷歌开源能翻译101种语言的AI模型,只比Facebook多一种
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Suitable for C / C + + novice learning some projects, do not give me to miss!

