PHP code example of yunwuxin / think-throttle

1. Go to this page and download the library: Download yunwuxin/think-throttle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yunwuxin / think-throttle example snippets


//...
use yunwuxin\throttle\facade\RateLimiter;

//...

$key = 'xxxx';

if(RateLimiter::tooManyAttempts($key, 10))
{
    //超出频率限制了
    throw new \Exception('....');
}

RateLimiter::hit($key, 60);

//....其他操作

use yunwuxin\throttle\RateLimiter;

class SomeClass
{

    public function index(Ratelimiter $limiter)
    {
        $key = 'xxxx';
        
        if($limiter->tooManyAttempts($key, 10))
        {
            //超出频率限制了
            throw new \Exception('....');
        }
        
        $limiter->hit($key, 60);

        //....其他操作
    }
}


use yunwuxin\throttle\middleware\ThrottleRequests;
use think\facade\Route;

Route::group(function(){
    //路由注册

})->middleware(ThrottleRequests::class, 10);