Skip to content

Commit 2f93c49

Browse files
committed
convenient methods for rate limiting
1 parent b253470 commit 2f93c49

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Illuminate/Cache/CacheServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function register()
3131
return new MemcachedConnector;
3232
});
3333

34-
$this->app->singleton(RateLimiter::class);
34+
$this->app->singleton(RateLimiter::class, function ($app) {
35+
return new RateLimiter($app->make('cache')->driver(
36+
$app['config']->get('cache.limiter')
37+
));
38+
});
3539
}
3640

3741
/**

src/Illuminate/Cache/RateLimiter.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,25 @@ public function resetAttempts($key)
153153
* @param int $maxAttempts
154154
* @return int
155155
*/
156-
public function retriesLeft($key, $maxAttempts)
156+
public function remaining($key, $maxAttempts)
157157
{
158158
$attempts = $this->attempts($key);
159159

160160
return $maxAttempts - $attempts;
161161
}
162162

163+
/**
164+
* Get the number of retries left for the given key.
165+
*
166+
* @param string $key
167+
* @param int $maxAttempts
168+
* @return int
169+
*/
170+
public function retriesLeft($key, $maxAttempts)
171+
{
172+
return $this->remaining($key, $maxAttempts);
173+
}
174+
163175
/**
164176
* Clear the hits and lockout timer for the given key.
165177
*

0 commit comments

Comments
 (0)