PHP code example of skyjerry / rate-limiter

1. Go to this page and download the library: Download skyjerry/rate-limiter 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/ */

    

skyjerry / rate-limiter example snippets


    $storageConfig = [
        'filePath' => sys_get_temp_dir() . '/test_rate_limiter.data',
    ];
    $rateLimiter = \RateLimiter\RateLimiterFactory::createRateLimiter(
         'tokenBucket',
         'file',
         $storageConfig,
         10,
         1
    );
    
    if (!$rateLimiter->acquire('user_1', 10)) {
        echo "限流了!";
    }
    

    $storageConfig = [
        'filePath' => sys_get_temp_dir() . '/test_rate_limiter.data',
    ];
    $rateLimiter = \RateLimiter\RateLimiterFactory::createRateLimiter(
         'tokenBucket',
         'file',
         $storageConfig,
         10,
         1
    );
    
    if (!$rateLimiter->acquire('user_1', 10)) {
        echo "Rate limited!";
    }