Skip to content

Commit 645f57a

Browse files
authored
[11.x] Pass decay seconds or minutes like hour and day (#51054)
* Allow overwriting the default decay time * Verify that default decay times are able to be chagned
1 parent ad00a85 commit 645f57a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Illuminate/Cache/RateLimiting/Limit.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,24 @@ public function __construct($key = '', int $maxAttempts = 60, int $decaySeconds
5151
* Create a new rate limit.
5252
*
5353
* @param int $maxAttempts
54+
* @param int $decaySeconds
5455
* @return static
5556
*/
56-
public static function perSecond($maxAttempts)
57+
public static function perSecond($maxAttempts, $decaySeconds = 1)
5758
{
58-
return new static('', $maxAttempts, 1);
59+
return new static('', $maxAttempts, $decaySeconds);
5960
}
6061

6162
/**
6263
* Create a new rate limit.
6364
*
6465
* @param int $maxAttempts
66+
* @param int $decayMinutes
6567
* @return static
6668
*/
67-
public static function perMinute($maxAttempts)
69+
public static function perMinute($maxAttempts, $decayMinutes = 1)
6870
{
69-
return new static('', $maxAttempts, 60);
71+
return new static('', $maxAttempts, 60 * $decayMinutes);
7072
}
7173

7274
/**

tests/Cache/LimitTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ public function testConstructors()
1818
$this->assertSame(1, $limit->decaySeconds);
1919
$this->assertSame(3, $limit->maxAttempts);
2020

21+
$limit = Limit::perSecond(3, 5);
22+
$this->assertSame(5, $limit->decaySeconds);
23+
$this->assertSame(3, $limit->maxAttempts);
24+
2125
$limit = Limit::perMinute(3);
2226
$this->assertSame(60, $limit->decaySeconds);
2327
$this->assertSame(3, $limit->maxAttempts);
2428

29+
$limit = Limit::perMinute(3, 4);
30+
$this->assertSame(240, $limit->decaySeconds);
31+
$this->assertSame(3, $limit->maxAttempts);
32+
2533
$limit = Limit::perMinutes(2, 3);
2634
$this->assertSame(120, $limit->decaySeconds);
2735
$this->assertSame(3, $limit->maxAttempts);
@@ -30,10 +38,18 @@ public function testConstructors()
3038
$this->assertSame(3600, $limit->decaySeconds);
3139
$this->assertSame(3, $limit->maxAttempts);
3240

41+
$limit = Limit::perHour(3, 2);
42+
$this->assertSame(7200, $limit->decaySeconds);
43+
$this->assertSame(3, $limit->maxAttempts);
44+
3345
$limit = Limit::perDay(3);
3446
$this->assertSame(86400, $limit->decaySeconds);
3547
$this->assertSame(3, $limit->maxAttempts);
3648

49+
$limit = Limit::perDay(3, 5);
50+
$this->assertSame(432000, $limit->decaySeconds);
51+
$this->assertSame(3, $limit->maxAttempts);
52+
3753
$limit = new GlobalLimit(3);
3854
$this->assertSame(60, $limit->decaySeconds);
3955
$this->assertSame(3, $limit->maxAttempts);

0 commit comments

Comments
 (0)