Skip to content

Commit

Permalink
convenient methods for rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 18, 2021
1 parent b253470 commit 2f93c49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function register()
return new MemcachedConnector;
});

$this->app->singleton(RateLimiter::class);
$this->app->singleton(RateLimiter::class, function ($app) {
return new RateLimiter($app->make('cache')->driver(
$app['config']->get('cache.limiter')
));
});
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,25 @@ public function resetAttempts($key)
* @param int $maxAttempts
* @return int
*/
public function retriesLeft($key, $maxAttempts)
public function remaining($key, $maxAttempts)
{
$attempts = $this->attempts($key);

return $maxAttempts - $attempts;
}

/**
* Get the number of retries left for the given key.
*
* @param string $key
* @param int $maxAttempts
* @return int
*/
public function retriesLeft($key, $maxAttempts)
{
return $this->remaining($key, $maxAttempts);
}

/**
* Clear the hits and lockout timer for the given key.
*
Expand Down

0 comments on commit 2f93c49

Please sign in to comment.