当前位置:网站首页>Thinkphp6.0 middleware with limited access frequency think throttle
Thinkphp6.0 middleware with limited access frequency think throttle
2022-07-04 05:29:00 【Yuan rises and falls】
effect
The middleware can limit the number of visits of users in a period of time , It can be used to protect the interface from climbing and explosion .
install
composer require topthink/think-throttle After installation, the project will be automatically generated conf/throttle.php The configuration file , Components are not automatically enabled after installation , Manual setting required .
Turn on
Components work as middleware , Therefore, its startup is the same as other middleware , For example, in global middleware app/middleware.php :
<?php
return [
\think\middleware\Throttle::class,
];Configuration instructions
stay config/throttle.php configuration option :
<?php
// Middleware configuration
return [
// Cache key prefix , Prevent key values from conflicting with other applications
'prefix' => 'throttle_',
// The cache keys ,true Indicates the source of use ip
'key' => true,
// The type of request to be restricted , eg: GET POST PUT DELETE HEAD
'visit_method' => ['GET'],
// Set access frequency , for example '10/m' It means allowing requests per minute 10 Time . value null Means unrestricted , eg: null 10/m 20/h 300/d 200/300
'visit_rate' => '100/m',
// Response returned when access is restricted
'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds) {
return Response::create('Too many requests, try again after ' . $wait_seconds . ' seconds.')->code(429);
},
];When the configuration item meets any of the following conditions , Access frequency will not be limited :
keyThe value isfalseornull;visit_rateThe value isnull.
among key The key used to set the cache ; and visit_rate Used to set the access frequency , The unit can be seconds , branch , when , God , for example :1/s, 10/m, 98/h, 100/d , It can also be 100/600 (600 Maximum in seconds 100 Requests ).
Flexible customization
Example 1 : Make restrictions on individual users , key The value of can be set as a function , This function returns the new cache key value ( need Session Support ), for example :
'key' => function($throttle, $request) {
$user_id = $request->session->get('user_id');
return $user_id ;
},Example 2 : You can also customize the generation for different controllers and methods in the callback function key, The middleware will transform :
'key' => function($throttle, $request) {
return '__CONTROLLER__/__ACTION__/__IP__';
},Or set it directly :
'key' => '__CONTROLLER__/__ACTION__/__IP__',PS: This example requires the middleware to be enabled after routing the middleware , In this way, the preset replacement function will take effect .
Example 3 : It is allowed to modify the current access frequency or temporarily change the current limiting policy within the closure :
'key' => function($throttle, $request) {
$throttle->setRate('5/m'); // Set the frequency
$throttle->setDriverClass(CounterSlider::class);// Set current limiting policy
return true;
},Example 4 : Allow independent configuration in route definition (1.3.x Versioning support )
Route::group(function() {
// Route registration
})->middleware(\think\middleware\Throttle::class, [
'visit_rate' => '20/m',
'key' => '__CONTROLLER__/__ACTION__/__IP__',
]);边栏推荐
- 力扣 第 300 场周赛
- Simulink与Arduino串口通信
- The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
- RSA加密应用常见缺陷的原理与实践
- Automated testing selenium foundation -- webdriverapi
- [matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
- [matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
- Thread pool: use thread pool to optimize query speed
- PostgreSQL has officially surpassed mysql. Is this guy too strong!
猜你喜欢
随机推荐
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (class I part response waveform)
力扣 第 300 场周赛
[matlab] matlab simulation modulation system - DSB system
2022 question bank and answers for safety management personnel of hazardous chemical business units
拓扑排序和关键路径的图形化显示
transformer坑了多少算力
Integer type of C language
Flink1.13 SQL basic syntax (I) DDL, DML
Flink1.13 basic SQL syntax (II) join operation
Appearance of LabVIEW error dialog box
[matlab] matlab simulation - low pass Gaussian white noise
Unity2d -- character moves and turns
总线的基本概念
VB.net 简单的处理图片,黑白(类库——7)
Roles of rollup components
Nodejs learning document
Void convolution, deformable convolution, deformable ROI pooling
2022 R2 mobile pressure vessel filling retraining question bank and answers
[matlab] matlab simulation - narrow band Gaussian white noise
Analysis of classical pointer and array written test questions in C language


![[QT] create mycombobox click event](/img/5a/ed17567a71f6737891fc7a8273df0a.png)






