https://github.com/soxft/rate-limit
General purpose rate limiter implementation.
https://github.com/soxft/rate-limit
Last synced: about 2 months ago
JSON representation
General purpose rate limiter implementation.
- Host: GitHub
- URL: https://github.com/soxft/rate-limit
- Owner: soxft
- License: mit
- Created: 2022-02-19T06:12:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-18T07:41:01.000Z (10 months ago)
- Last Synced: 2025-02-11T01:33:30.455Z (3 months ago)
- Language: PHP
- Size: 31.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rate Limit
> 基于Redis 的 PHP 速率限制
## Installation```bash
composer require soxft/rate-limit
```## Usage
**Terminating rate limiter**
```php
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**
```php
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](LICENSE) for details.
Secondary development based on [nikolaposa/rate-limie](https://github.com/nikolaposa/rate-limit)