1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soupmix\Cache; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
use Psr\SimpleCache\CacheInterface; |
10
|
|
|
use Redis; |
11
|
|
|
|
12
|
|
|
use function array_combine; |
13
|
|
|
use function array_fill; |
14
|
|
|
use function array_merge; |
15
|
6 |
|
use function count; |
16
|
|
|
use function defined; |
17
|
6 |
|
use function extension_loaded; |
18
|
|
|
use function time; |
19
|
|
|
|
20
|
6 |
|
class RedisCache extends Common implements CacheInterface |
21
|
6 |
|
{ |
22
|
|
|
public function __construct(private Redis $handler) |
23
|
1 |
|
{ |
24
|
|
|
if (! defined('Redis::SERIALIZER_IGBINARY') || ! extension_loaded('igbinary')) { |
25
|
1 |
|
return; |
26
|
1 |
|
} |
27
|
1 |
|
|
28
|
|
|
$handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); |
29
|
|
|
} |
30
|
5 |
|
|
31
|
|
|
public function get($key, $default = null) |
32
|
5 |
|
{ |
33
|
3 |
|
$this->checkReservedCharacters($key); |
34
|
1 |
|
$value = $this->handler->get($key); |
35
|
|
|
|
36
|
3 |
|
return $value ?? $default; |
37
|
3 |
|
} |
38
|
1 |
|
|
39
|
|
|
public function set($key, $value, $ttl = null): bool |
40
|
2 |
|
{ |
41
|
|
|
$this->checkReservedCharacters($key); |
42
|
|
|
if ($ttl instanceof DateInterval) { |
43
|
|
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
2 |
|
$setTtl = (int) $ttl; |
47
|
|
|
if ($setTtl === 0) { |
48
|
|
|
return $this->handler->set($key, $value); |
49
|
6 |
|
} |
50
|
|
|
|
51
|
6 |
|
return $this->handler->setex($key, $ttl, $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function delete($key): bool |
55
|
|
|
{ |
56
|
1 |
|
return (bool) $this->handler->del($key); |
57
|
1 |
|
} |
58
|
1 |
|
|
59
|
|
|
public function clear(): bool |
60
|
1 |
|
{ |
61
|
|
|
return $this->handler->flushDB(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getMultiple($keys, $default = null): array |
65
|
1 |
|
{ |
66
|
|
|
$defaults = array_fill(0, count($keys), $default); |
67
|
1 |
|
foreach ($keys as $key) { |
68
|
1 |
|
$this->checkReservedCharacters($key); |
69
|
|
|
} |
70
|
1 |
|
|
71
|
1 |
|
return array_merge(array_combine($keys, $this->handler->mget($keys)), $defaults); |
72
|
|
|
} |
73
|
1 |
|
|
74
|
1 |
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function setMultiple($values, $ttl = null): bool |
78
|
1 |
|
{ |
79
|
1 |
|
foreach ($values as $key => $value) { |
80
|
|
|
$this->checkReservedCharacters($key); |
81
|
|
|
} |
82
|
1 |
|
|
83
|
|
|
if ($ttl instanceof DateInterval) { |
84
|
|
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
$setTtl = (int) $ttl; |
88
|
1 |
|
if ($setTtl === 0) { |
89
|
|
|
return $this->handler->mset($values); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$return = true; |
93
|
|
|
foreach ($values as $key => $value) { |
94
|
|
|
$return = $return && $this->set($key, $value, $setTtl); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function has($key) |
101
|
|
|
{ |
102
|
|
|
$this->checkReservedCharacters($key); |
103
|
|
|
|
104
|
|
|
return (bool) $this->handler->exists($key); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|