定制 wilbur-yu/hyperf-cache-ext 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

配置

  1. 修改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 Facade RateLimiter::for(callable),

在服务启动时, 监听器会收集该命名限制器数组, 供在注解中使用 for 参数引用. 在注解切面执行时, 会将当前请求 \Hyperf\HttpServer\Contract\RequestInterface 实例注入到该命名闭包.

  • key 默认为当前请求 fullUrl + ipsha1. 支持字符串与闭包.
  1. 可捕获异常
  • 适用于计数器限流

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!';
}
  1. concurrency_limiter()

使用方法同 Laravel 中的 Redis::funnel 门面代理方法 可参考高级限流器: 限定并发请求访问上限

$executed = concurrency_limiter('key')->limit(100)->then(function(){
    // send sms logic
}, function(){
    // 异常上时调用
    abort(429);
});
  1. duration_limiter()

使用方法同 Laravel 中的 Redis::throttle 门面代理方法 可参考高级限流器: 限定单位时间访问上限

$executed = duration_limiter('key')->allow(100)->every(10)->then(function(){
    // send sms logic
}, function(){
    // 异常上时调用
    abort(429);
});

感谢

Laravel Cache

Hyperf Rate-Limit

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 wilbur-yu/hyperf-cache-ext 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-30