wilbur-yu/hyperf-cache-ext
Composer 安装命令:
composer require wilbur-yu/hyperf-cache-ext
包简介
hyperf cache counter rate limiter
关键字:
README 文档
README
BETA
移植了 Laravel Cache 组件的 rate-limiter.
并对 \Psr\SimpleCache\CacheInterface 进行了补充. 增加了以下方法:
- increment
- decrement
- add
- put
安装
composer require wenber-yu/hyperf-cache-ext
配置
- 修改cache配置文件:
'default' => [ 'driver' => WilburYu\HyperfCacheExt\Driver\RedisDriver::class, 'packer' => WilburYu\HyperfCacheExt\Utils\Packer\PhpSerializerPacker::class, 'prefix' => env('APP_NAME', 'skeleton').':cache:', ], 'limiter' => [ 'prefix' => 'rate-limit:', // key前缀 'max_attempts' => 20, // 最大允许数 'decay_minutes' => 60, // 限流单位时间 'wait' => 250, // 并发时, 获取锁最大等待毫秒数 'timeout' => 1, // 并发时, 获取锁超时秒数 'for' => [ 'common' => static function (\Hyperf\HttpServer\Contract\RequestInterface $request) { return Limit::perMinute(3); }, ], 'key' => ThrottleRequest::key(), ],
for即对应Laravel FacadeRateLimiter::for(callable),
在服务启动时, 监听器会收集该命名限制器数组, 供在注解中使用
for参数引用. 在注解切面执行时, 会将当前请求\Hyperf\HttpServer\Contract\RequestInterface实例注入到该命名闭包.
key默认为当前请求fullUrl+ip的sha1. 支持字符串与闭包.
- 可捕获异常
- 适用于计数器限流
WilburYu\HyperfCacheExt\Exception\CounterRateLimiterException::class
该异常自带一个
getHeaders方法, 值为: array('X-RateLimit-Limit', 'X-RateLimit-Remaining', 'Retry-After', ' X-RateLimit-Reset')
- 适用于漏斗与时间窗口限流
WilburYu\HyperfCacheExt\Exception\LimiterTimeoutException::class
使用
在控制器中使用
- 计数器限流注解
#[CounterRateLimiterWithRedis(maxAttempts: 5, decayMinutes: 1)] or #[CounterRateLimiter(for: "common")]
- 漏斗限流注解
use WilburYu\HyperfCacheExt\Annotation\ConcurrencyRateLimiter; // 1秒内最高支持并发请求5次 #[ConcurrencyRateLimiter(key: 'get.posts', maxAttempts: 5, decayMinutes: 1, timeout: 1, wait: 250)]
- 时间窗口限流注解
use WilburYu\HyperfCacheExt\Annotation\DurationRateLimiter; // 每 10 秒最多支持 100 个请求 #[DurationRateLimiter(key: 'get.posts', maxAttempts: 100, decayMinutes: 10, timeout: 1, wait: 750)] - ``` > 注解参数同配置文件, 优先级为注解>配置>默认. > > 1. 在计数器注解中使用 `for` 时, `max_attempts` 和 `decay_minutes` 不起作用. > 2. `timeout` 和 `wait` 参数, 适用于`漏斗`和`时间窗口` 如果你的缓存驱动不是 `redis`, 只可以使用 `CounterRateLimit` 注解. 反之则可以使用 `CounterRateLimitWithRedis/ConcurrencyRateLimiter/DurationRateLimiter` 注解. 在其他地方使用限速时, 可以使用辅助函数 1. counter_limiter() 使用方法同 `Laravel`中的 `RateLimiter Facade`, 可参考 [Laravel 限流文档](https://learnku.com/docs/laravel/8.5/current-limiting/11453) ```php $executed = counter_limiter()->attempt('send-sms:'.$user->id,2,function(){ // send sms logic }); if (!$executed) { return 'Too many messages sent!'; }
- concurrency_limiter()
使用方法同 Laravel 中的 Redis::funnel 门面代理方法
可参考高级限流器: 限定并发请求访问上限
$executed = concurrency_limiter('key')->limit(100)->then(function(){ // send sms logic }, function(){ // 异常上时调用 abort(429); });
- duration_limiter()
使用方法同 Laravel 中的 Redis::throttle 门面代理方法
可参考高级限流器: 限定单位时间访问上限
$executed = duration_limiter('key')->allow(100)->every(10)->then(function(){ // send sms logic }, function(){ // 异常上时调用 abort(429); });
感谢
wilbur-yu/hyperf-cache-ext 适用场景与选型建议
wilbur-yu/hyperf-cache-ext 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.68k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「throttle」 「hyperf」 「php rate limiter」 「php counter rate limiter」 「hyperf rate limiter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wilbur-yu/hyperf-cache-ext 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wilbur-yu/hyperf-cache-ext 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wilbur-yu/hyperf-cache-ext 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An async event for hyperf.
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
php hyperf xxljob
SForum
Alfabank REST API integration
统计信息
- 总下载量: 2.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-30