-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Redis.php
163 lines (132 loc) · 3.7 KB
/
Redis.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Ackintosh\Ganesha\Storage\Adapter;
use Ackintosh\Ganesha;
use Ackintosh\Ganesha\Configuration;
use Ackintosh\Ganesha\Exception\StorageException;
use Ackintosh\Ganesha\Storage\AdapterInterface;
class Redis implements AdapterInterface, SlidingTimeWindowInterface
{
/**
* @var \Ackintosh\Ganesha\Storage\Adapter\RedisStore
*/
private $redis;
/**
* @var Configuration
*/
private $configuration;
/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Ackintosh\Ganesha\Storage\Adapter\RedisStore $redis
*/
public function __construct($redis)
{
if (!($redis instanceof RedisStore)) {
$redis = new RedisStore($redis);
}
$this->redis = $redis;
}
public function supportCountStrategy(): bool
{
return false;
}
public function supportRateStrategy(): bool
{
return true;
}
public function setContext(Ganesha\Context $context): void
{
$this->configuration = $context->configuration();
}
/**
* @inheritdoc
*/
public function setConfiguration(Configuration $configuration): void
{
// nop
}
/**
* @throws StorageException
*/
public function load(string $service): int
{
$this->removeExpiredElements($service);
$r = $this->redis->zCard($service);
if ($r === false) {
throw new StorageException('Failed to load cardinality. service: ' . $service);
}
return $r;
}
public function save(string $service, int $count): void
{
// Redis adapter does not support Count strategy
}
/**
* @throws StorageException
*/
public function increment(string $service): void
{
$t = microtime(true);
$this->removeExpiredElements($service);
$this->redis->zAdd($service, $t, $t);
}
public function decrement(string $service): void
{
// Redis adapter does not support Count strategy
}
public function saveLastFailureTime(string $service, int $lastFailureTime): void
{
// nop
}
/**
* @throws StorageException
*/
public function loadLastFailureTime(string $service): ?int
{
$lastFailure = $this->redis->zRange($service, -1, -1);
if (!$lastFailure) {
return null;
}
return (int)$lastFailure[0];
}
/**
* @throws StorageException
*/
public function saveStatus(string $service, int $status): void
{
$r = $this->redis->set($service, $status);
if ($r === false) {
throw new StorageException(sprintf(
'Failed to save status. service: %s, status: %d',
$service,
$status
));
}
}
/**
* @throws StorageException
*/
public function loadStatus(string $service): int
{
$r = $this->redis->get($service);
// \Redis::get() returns FALSE if key didn't exist.
// @see https://github.com/phpredis/phpredis#get
if ($r === false) {
$this->saveStatus($service, Ganesha::STATUS_CALMED_DOWN);
return Ganesha::STATUS_CALMED_DOWN;
}
return (int)$r;
}
public function reset(): void
{
// TODO: Implement reset() method.
}
/**
* @throws StorageException
*/
private function removeExpiredElements(string $service): void
{
$expires = microtime(true) - $this->configuration->timeWindow();
if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
throw new StorageException('Failed to remove expired elements. service: ' . $service);
}
}
}