1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache\Redis; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
use Predis\Client; |
10
|
|
|
use Predis\ClientInterface; |
11
|
|
|
use Predis\Connection\ConnectionInterface; |
12
|
|
|
use Predis\Connection\StreamConnection; |
13
|
|
|
use Predis\Response\Status; |
14
|
|
|
use Psr\SimpleCache\CacheInterface; |
15
|
|
|
use Traversable; |
16
|
|
|
|
17
|
|
|
use function array_fill_keys; |
18
|
|
|
use function array_keys; |
19
|
|
|
use function array_map; |
20
|
|
|
use function count; |
21
|
|
|
use function iterator_to_array; |
22
|
|
|
use function serialize; |
23
|
|
|
use function strpbrk; |
24
|
|
|
use function unserialize; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RedisCache stores cache data in a Redis. |
28
|
|
|
* |
29
|
|
|
* Please refer to {@see CacheInterface} for common cache operations that are supported by RedisCache. |
30
|
|
|
*/ |
31
|
|
|
final class RedisCache implements CacheInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var ClientInterface $client Predis client instance to use. |
35
|
|
|
*/ |
36
|
|
|
private ClientInterface $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array<ConnectionInterface>|ConnectionInterface $connections Predis connections instance to use |
40
|
|
|
*/ |
41
|
|
|
private ConnectionInterface|array $connections; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ClientInterface $client Predis client instance to use. |
45
|
|
|
*/ |
46
|
223 |
|
public function __construct(ClientInterface $client) |
47
|
|
|
{ |
48
|
223 |
|
$this->client = $client; |
49
|
223 |
|
$this->connections = $this->client->getConnection(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checking Predis cluster usage |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
223 |
|
public function isCluster(): bool |
58
|
|
|
{ |
59
|
|
|
/** @psalm-suppress MixedAssignment, PossibleRawObjectIteration */ |
60
|
223 |
|
foreach ($this->connections as $connection) { |
61
|
|
|
/** @var StreamConnection $connection */ |
62
|
105 |
|
$cluster = (new Client($connection->getParameters()))->info('Cluster'); |
63
|
105 |
|
if (isset($cluster['Cluster']['cluster_enabled']) && 1 === (int)$cluster['Cluster']['cluster_enabled']) { |
64
|
105 |
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
118 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $key |
73
|
|
|
* @param mixed|null $default |
74
|
|
|
* |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
130 |
|
public function get(string $key, mixed $default = null): mixed |
80
|
|
|
{ |
81
|
130 |
|
$this->validateKey($key); |
82
|
126 |
|
$value = $this->client->get($key); |
83
|
126 |
|
return $value === null ? $default : unserialize($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @param DateInterval|int|null $ttl |
90
|
|
|
* |
91
|
|
|
* @throws InvalidArgumentException |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
171 |
|
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool |
96
|
|
|
{ |
97
|
171 |
|
$ttl = $this->normalizeTtl($ttl); |
98
|
|
|
|
99
|
171 |
|
if ($this->isExpiredTtl($ttl)) { |
100
|
1 |
|
return $this->delete($key); |
101
|
|
|
} |
102
|
|
|
|
103
|
170 |
|
$this->validateKey($key); |
104
|
|
|
|
105
|
|
|
/** @var Status|null $result */ |
106
|
168 |
|
$result = $this->isInfinityTtl($ttl) |
107
|
144 |
|
? $this->client->set($key, serialize($value)) |
108
|
24 |
|
: $this->client->set($key, serialize($value), 'EX', $ttl); |
109
|
|
|
|
110
|
168 |
|
return $result !== null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $key |
115
|
|
|
* |
116
|
|
|
* @throws InvalidArgumentException |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
31 |
|
public function delete(string $key): bool |
121
|
|
|
{ |
122
|
31 |
|
return !$this->has($key) || $this->client->del($key) === 1; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* If a cluster is used, all nodes will be cleared |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
223 |
|
public function clear(): bool |
131
|
|
|
{ |
132
|
223 |
|
if ($this->isCluster()) { |
133
|
|
|
/** @psalm-suppress MixedAssignment, PossibleRawObjectIteration */ |
134
|
105 |
|
foreach ($this->connections as $connection) { |
135
|
|
|
/** @var StreamConnection $connection */ |
136
|
105 |
|
$client = new Client($connection->getParameters()); |
137
|
105 |
|
$client->flushdb(); |
138
|
|
|
} |
139
|
105 |
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
118 |
|
return $this->client->flushdb() !== null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param iterable<string> $keys |
147
|
|
|
* @param mixed $default |
148
|
|
|
* |
149
|
|
|
* @throws InvalidArgumentException |
150
|
|
|
* |
151
|
|
|
* @return iterable<string, mixed> |
152
|
|
|
*/ |
153
|
14 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
154
|
|
|
{ |
155
|
|
|
/** @var string[] $keys */ |
156
|
14 |
|
$keys = $this->iterableToArray($keys); |
157
|
14 |
|
$this->validateKeys($keys); |
158
|
8 |
|
$values = array_fill_keys($keys, $default); |
159
|
|
|
/** @var null[]|string[] $valuesFromCache */ |
160
|
8 |
|
$valuesFromCache = $this->client->mget($keys); |
161
|
|
|
|
162
|
8 |
|
$i = 0; |
163
|
|
|
|
164
|
|
|
/** @psalm-suppress MixedAssignment */ |
165
|
8 |
|
foreach ($values as $key => $value) { |
166
|
8 |
|
$values[$key] = isset($valuesFromCache[$i]) ? unserialize($valuesFromCache[$i]) : $value; |
167
|
8 |
|
$i++; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
return $values; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param iterable $values |
175
|
|
|
* @param DateInterval|int|null $ttl |
176
|
|
|
* |
177
|
|
|
* @throws InvalidArgumentException |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
14 |
|
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
182
|
|
|
{ |
183
|
14 |
|
$values = $this->iterableToArray($values); |
184
|
14 |
|
$keys = array_map('\strval', array_keys($values)); |
185
|
14 |
|
$this->validateKeys($keys); |
186
|
12 |
|
$ttl = $this->normalizeTtl($ttl); |
187
|
12 |
|
$serializeValues = []; |
188
|
|
|
|
189
|
12 |
|
if ($this->isExpiredTtl($ttl)) { |
190
|
1 |
|
return $this->deleteMultiple($keys); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** @var mixed $value */ |
194
|
11 |
|
foreach ($values as $key => $value) { |
195
|
11 |
|
$serializeValues[$key] = serialize($value); |
196
|
|
|
} |
197
|
|
|
|
198
|
11 |
|
if ($this->isInfinityTtl($ttl)) { |
199
|
8 |
|
$this->client->mset($serializeValues); |
200
|
8 |
|
return true; |
201
|
|
|
} |
202
|
|
|
|
203
|
3 |
|
$this->client->multi(); |
204
|
3 |
|
$this->client->mset($serializeValues); |
205
|
|
|
|
206
|
3 |
|
foreach ($keys as $key) { |
207
|
3 |
|
$this->client->expire($key, (int)$ttl); |
208
|
|
|
} |
209
|
|
|
|
210
|
3 |
|
$results = $this->client->exec(); |
211
|
|
|
|
212
|
3 |
|
return !in_array(null, (array)$results, true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param iterable $keys |
217
|
|
|
* |
218
|
|
|
* @throws InvalidArgumentException |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
8 |
|
public function deleteMultiple(iterable $keys): bool |
223
|
|
|
{ |
224
|
8 |
|
$keys = $this->iterableToArray($keys); |
225
|
|
|
|
226
|
|
|
/** @psalm-suppress MixedAssignment, MixedArgument */ |
227
|
8 |
|
foreach ($keys as $index => $key) { |
228
|
8 |
|
if (!$this->has($key)) { |
229
|
3 |
|
unset($keys[$index]); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
234
|
4 |
|
return empty($keys) || $this->client->del($keys) === count($keys); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $key |
239
|
|
|
* |
240
|
|
|
* @throws InvalidArgumentException |
241
|
|
|
* |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
64 |
|
public function has(string $key): bool |
245
|
|
|
{ |
246
|
64 |
|
$this->validateKey($key); |
247
|
52 |
|
$ttl = $this->client->ttl($key); |
248
|
|
|
/** "-1" - if the key exists but has no associated expire {@see https://redis.io/commands/ttl}. */ |
249
|
52 |
|
return $ttl > 0 || $ttl === -1; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Normalizes cache TTL handling `null` value, strings and {@see DateInterval} objects. |
254
|
|
|
* |
255
|
|
|
* @param DateInterval|int|string|null $ttl The raw TTL. |
256
|
|
|
* |
257
|
|
|
* @return int|null TTL value as UNIX timestamp. |
258
|
|
|
*/ |
259
|
196 |
|
private function normalizeTtl(null|int|string|DateInterval $ttl): ?int |
260
|
|
|
{ |
261
|
196 |
|
if ($ttl === null) { |
262
|
156 |
|
return null; |
263
|
|
|
} |
264
|
|
|
|
265
|
40 |
|
if ($ttl instanceof DateInterval) { |
266
|
4 |
|
return (new DateTime('@0')) |
267
|
4 |
|
->add($ttl) |
268
|
4 |
|
->getTimestamp(); |
269
|
|
|
} |
270
|
|
|
|
271
|
36 |
|
return (int) $ttl; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Converts iterable to array. |
276
|
|
|
* |
277
|
|
|
* @param iterable $iterable |
278
|
|
|
* |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
26 |
|
private function iterableToArray(iterable $iterable): array |
282
|
|
|
{ |
283
|
|
|
/** @psalm-suppress RedundantCast */ |
284
|
26 |
|
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $key |
289
|
|
|
* |
290
|
|
|
* @throws InvalidArgumentException |
291
|
|
|
*/ |
292
|
204 |
|
private function validateKey(string $key): void |
293
|
|
|
{ |
294
|
204 |
|
if ($key === '' || strpbrk($key, '{}()/\@:')) { |
295
|
22 |
|
throw new InvalidArgumentException('Invalid key value.'); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param string[] $keys |
301
|
|
|
* |
302
|
|
|
* @throws InvalidArgumentException |
303
|
|
|
*/ |
304
|
22 |
|
private function validateKeys(array $keys): void |
305
|
|
|
{ |
306
|
22 |
|
if ([] === $keys) { |
307
|
4 |
|
throw new InvalidArgumentException('Invalid key values.'); |
308
|
|
|
} |
309
|
|
|
|
310
|
18 |
|
foreach ($keys as $key) { |
311
|
18 |
|
$this->validateKey($key); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param int|null $ttl |
317
|
|
|
* |
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
182 |
|
private function isExpiredTtl(?int $ttl): bool |
321
|
|
|
{ |
322
|
182 |
|
return $ttl !== null && $ttl <= 0; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param int|null $ttl |
327
|
|
|
* |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
179 |
|
private function isInfinityTtl(?int $ttl): bool |
331
|
|
|
{ |
332
|
179 |
|
return $ttl === null; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|