Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure the counter current limiting component of PHP Hyperf

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the PHP Hyperf counter current limiting components how to install and configure the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this PHP Hyperf counter current limiting components how to install the configuration article will have a harvest, let's take a look.

Description

BETA

Transplant rate-limiter. Laravel Cache components.

And added to\ Psr\ SimpleCache\ CacheInterface. The following methods have been added:

Increment

Decrement

Add

Put

Installation

Composer require wilbur-yu/hyperf-cache-ext

Configuration

1. Modify the cache configuration file:

'default' = > [' driver' = > WilburYu\ HyperfCacheExt\ Driver\ RedisDriver::class, 'packer' = > WilburYu\ HyperfCacheExt\ Utils\ Packer\ PhpSerializerPacker::class,' prefix' = > env ('APP_NAME',' skeleton').': cache:',], 'limiter' = > [' max_attempts' = > 5, / / maximum allowable times' decay_minutes' = > 1, / / current limiting unit time 'prefix' = >' counter-rate-limit:' / / key prefix 'for' = > [' common' = > static function (\ Hyperf\ HttpServer\ Contract\ RequestInterface $request) {return Limit::perMinute (3) },], 'key' = > ThrottleRequest::key (),]

For is the corresponding Laravel Facade RateLimiter::for (callable)

When the service starts, the listener collects the array of naming limiters for reference using the for parameter in the annotation. When the annotation section is executed, the current request instance\ Hyperf\ HttpServer\ Contract\ RequestInterface is injected into the named closure.

Key defaults to the current request fullUrl + ip. Supports strings and closures.

two。 Add to the exceptions configuration file:

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

Optional or can be caught by yourself. The exception comes with a getHeaders method with a value of: array ('XmurRateLimitMutual,' XmurRateLimitlyRemaining, 'Retry-After',' XRateLimitMuthReset`)

Use

Use counter speed limit annotation in controller

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

The annotation parameters are the same as the configuration file, and the priority is comment > configuration > default.

Max_attempts and decay_minutes do not work when using for.

If your cache driver is not redis, you can use CounterRateLimit annotations, otherwise use CounterRateLimitWithRedis annotations directly.

When using the speed limit in other places, you can use the auxiliary function counter_limiter (), using the same method as RateLimiter Facade in laravel. Please refer to the Laravel current limit document.

$executed = counter_limiter ()-> attempt ('send-sms:'.$user- > id,2,function () {/ / send sms logic}); if (! $executed) {return' Too many messages sentinel;} this is the end of the article on "how to install and configure the counter current limiting component of PHP Hyperf". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to install and configure the counter current limiting components of PHP Hyperf". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report