soxft/rate-limit

General purpose rate limiter implementation.

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/soxft/rate-limit

1.1.5 2024-07-18 07:40 UTC

This package is auto-updated.

Last update: 2025-10-18 10:47:40 UTC


README

基于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