久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      可用于PHP Hyperf的計數(shù)器限流組件(安裝配置)

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

      說明

      BETA

      移植了 Laravel Cache 組件的 rate-limiter.

      并對 PsrSimpleCacheCacheInterface 進行了補充. 增加了以下方法:

      • increment
      • decrement
      • add
      • put

      安裝

      composer require wilbur-yu/hyperf-cache-ext

      配置

      1. 修改cache配置文件:

      'default' => [     'driver' => WilburYuHyperfCacheExtDriverRedisDriver::class,     'packer' => WilburYuHyperfCacheExtUtilsPackerPhpSerializerPacker::class,     'prefix' => env('APP_NAME', 'skeleton').':cache:', ], 'limiter' => [     'max_attempts' => 5,  // 最大允許次數(shù)     'decay_minutes' => 1, // 限流單位時間     'prefix' => 'counter-rate-limit:', // key 前綴     'for' => [         'common' => static function (HyperfHttpServerContractRequestInterface $request) {             return Limit::perMinute(3);         },     ],     'key' => ThrottleRequest::key(), ],
      • for 即對應 Laravel Facade RateLimiter::for(callable),

        在服務啟動時, 監(jiān)聽器會收集該命名限制器數(shù)組, 供在注解中使用 for 參數(shù)引用. 在注解切面執(zhí)行時, 會將當前請求 HyperfHttpServerContractRequestInterface 實例注入到該命名閉包.

      • key 默認為當前請求 fullUrl + ip. 支持字符串與閉包.

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

      WilburYuHyperfCacheExtExceptionHandlerCounterRateLimitException::class

      可選, 也可自行捕獲, 該異常自帶一個 getHeaders 方法, 值為: array(‘X-RateLimit-Limit’, ‘X-RateLimit-Remaining’, ‘Retry-After’, ‘X-RateLimit-Reset’)

      使用

      在控制器中使用計數(shù)器限速注解

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

      注解參數(shù)同配置文件, 優(yōu)先級為注解>配置>默認.
      使用 for 時, max_attemptsdecay_minutes 不起作用.

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

      在其他地方使用限速時, 可以使用輔助函數(shù) 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!'; }

      推薦學習:《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號