Rate Limiting
Typically, the rate limiter utilizes your default application cache as defined by the key within your application’s cache
configuration file. However, you may specify which cache driver the rate limiter should use by defining a limiter
key within your application’s cache
configuration file:
Basic Usage
The method returns false
when the callback has no remaining attempts available; otherwise, the attempt
method will return the callback’s result or true
. The first argument accepted by the attempt
method is a rate limiter “key”, which may be any string of your choosing that represents the action being rate limited:
use Illuminate\Support\Facades\RateLimiter; $executed = RateLimiter::attempt( 'send-message:'.$user->id, $perMinute = 5, function() { // Send message... }); if (! $executed) { return 'Too many messages sent!';}
If you would like to manually interact with the rate limiter, a variety of other methods are available. For example, you may invoke the method to determine if a given rate limiter key has exceeded its maximum number of allowed attempts per minute:
use Illuminate\Support\Facades\RateLimiter; if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) { RateLimiter::hit('send-message:'.$user->id); // Send message...}
Determining Limiter Availability
When a key has no more attempts left, the availableIn
method returns the number of seconds remaining until more attempts will be available:
You may reset the number of attempts for a given rate limiter key using the clear
method. For example, you may reset the number of attempts when a given message is read by the receiver: