-
Notifications
You must be signed in to change notification settings - Fork 11
/
UserExample.php
74 lines (61 loc) · 1.51 KB
/
UserExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace ethercreative\ratelimiter;
use Yii;
/**
* Class UserExample
*
* @package ethercreative\ratelimiter
*/
class UserExample extends \yii\base\Model implements IpRateLimitInterface
{
/**
* @var string IP of the user
*/
private $ip;
/**
* @var integer maximum number of allowed requests
*/
private $rateLimit;
/**
* @var integer time period for the rates to apply to
*/
private $timePeriod;
/**
* @inheritdoc
*/
public static function findByIp($ip, $rateLimit, $timePeriod)
{
$user = new static();
$user->ip = $ip;
$user->rateLimit = $rateLimit;
$user->timePeriod = $timePeriod;
return $user;
}
/**
* @inheritdoc
*/
public function getRateLimit($request, $action)
{
return [$this->rateLimit, $this->timePeriod];
}
/**
* @inheritdoc
*/
public function loadAllowance($request, $action)
{
$cache = Yii::$app->getCache();
return [
$cache->get('user.ratelimit.ip.allowance.' . $this->ip),
$cache->get('user.ratelimit.ip.allowance_updated_at.' . $this->ip),
];
}
/**
* @inheritdoc
*/
public function saveAllowance($request, $action, $allowance, $timestamp)
{
$cache = Yii::$app->getCache();
$cache->set('user.ratelimit.ip.allowance.' . $this->ip, $allowance);
$cache->set('user.ratelimit.ip.allowance_updated_at.' . $this->ip, $timestamp);
}
}