日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
可用于PHPHyperf的計數器限流組件(安裝配置)

本篇文章給大家介紹關于適用于 Hyperf 的計數器限流組件。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

網站建設哪家好,找創(chuàng)新互聯!專注于網頁設計、網站建設、微信開發(fā)、微信小程序、集團企業(yè)網站建設等服務項目。為回饋新老客戶創(chuàng)新互聯還提供了禹州免費建站歡迎大家使用!

說明

BETA

并對 \Psr\SimpleCache\CacheInterface 進行了補充. 增加了以下方法:

  • increment
  • decrement
  • add
  • put

安裝

composer require wilbur-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' => [
    'max_attempts' => 5,  // 最大允許次數
    'decay_minutes' => 1, // 限流單位時間
    'prefix' => 'counter-rate-limit:', // key 前綴
    'for' => [
        'common' => static function (\Hyperf\HttpServer\Contract\RequestInterface $request) {
            return Limit::perMinute(3);
        },
    ],
    'key' => ThrottleRequest::key(),
],
  • for 即對應 Laravel Facade RateLimiter::for(callable),
  • key 默認為當前請求 fullUrl + ip. 支持字符串與閉包.

2. 在exceptions配置文件中增加:

\WilburYu\HyperfCacheExt\Exception\Handler\CounterRateLimitException::class

使用

在控制器中使用計數器限速注解

#[CounterRateLimitWithRedis(maxAttempts: 5, decayMinutes: 1)]or#[CounterRateLimit(for: "common")]

如果你的緩存驅動不是 redis, 可以使用 CounterRateLimit 注解,反之則直接使用 CounterRateLimitWithRedis 注解即可.

在其他地方使用限速時, 可以使用輔助函數 counter_limiter(), 使用方法同 laravel中的 RateLimiter Facade, 可參考 Laravel 限流文檔

$executed = counter_limiter()->attempt('send-sms:'.$user->id,2,function(){
    // send sms logic
});
if (!$executed) {
    return 'Too many messages sent!';
}

本文題目:可用于PHPHyperf的計數器限流組件(安裝配置)
URL標題:http://www.5511xx.com/article/dhjogds.html