Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sparkinzy/redis-limiter
基于redis的限流器
https://github.com/sparkinzy/redis-limiter
Last synced: 9 days ago
JSON representation
基于redis的限流器
- Host: GitHub
- URL: https://github.com/sparkinzy/redis-limiter
- Owner: Sparkinzy
- Created: 2019-08-08T07:18:58.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-08T07:40:16.000Z (over 5 years ago)
- Last Synced: 2024-10-30T10:04:22.323Z (19 days ago)
- Language: PHP
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# 使用方式
## 添加redis配置
```php
$redisConf = [
'host' => '127.0.0.1',
'port' => 16379,
'auth' => ''
];
Limit::setRedisConf($redisConf);
```## 配置限流规则
```php
Limit::getInstance()->addItem('default')->setMax('1r/s');
```
addItem() 配置的是限流的keysetMax() 配置的是限流规则,类似nginx限流规则
- 1r/s 表示每秒限一次
- 1r/m 每分钟一次
- 1r/h 每小时一次
- 1r/d 每天一次## 3.实际项目中检查是否符合限流
```php
if (Limit::isAllow('default')){
echo '成功',PHP_EOL;
}else{
echo '失败',PHP_EOL;
}
```# 完整demo
```bash
require_once __DIR__.'/vendor/autoload.php';
use Mu\Juyuan\Limit;
$redisConf = [
'host' => '127.0.0.1',
'port' => 16379,
'auth' => ''
];
Limit::setRedisConf($redisConf);
Limit::getInstance()->addItem('default')->setMax('1r/s');
for ($i=0;$i<100;$i++){
usleep(500000);
echo '['.date('Y-m-d H:i:s').']';
if (Limit::isAllow('default')){
echo '成功',PHP_EOL;
}else{
echo '失败',PHP_EOL;
}
}
```