Skip to content

soxft/rate-limit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rate Limit

基于Redis 的 PHP 速率限制

Installation

composer require soxft/rate-limit

Usage

Terminating rate limiter

use RateLimit\Rate;
use RateLimit\RateLimiter;
use RateLimit\Exception\LimitExceededException;

$rateLimiter = new RateLimiter(Rate::perMinute(100), new \Redis());

$apiKey = 'abc123'; // 用户标识

try {
    $rateLimiter->limit($apiKey);
    
    //on success
} catch (LimitExceededException $exception) {
   //on limit exceeded
}

Silent rate limiter

use RateLimit\Rate;
use RateLimit\RateLimiter;

$rateLimiter = new RateLimiter(Rate::perMinute(100), new \Redis());

$ipAddress = '192.168.1.2';
$status = $rateLimiter->limitSilently($ipAddress);

echo $status->left(); //99

License

Released under MIT License - see the License File for details.

Secondary development based on nikolaposa/rate-limie

About

General purpose rate limiter implementation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages