1 | <?php |
||
2 | /** |
||
3 | * Class RedisCache |
||
4 | * |
||
5 | * @filesource RedisCache.php |
||
6 | * @created 27.05.2017 |
||
7 | * @package chillerlan\SimpleCache |
||
8 | * @author Smiley <[email protected]> |
||
9 | * @copyright 2017 Smiley |
||
10 | * @license MIT |
||
11 | * |
||
12 | * @noinspection PhpComposerExtensionStubsInspection |
||
13 | * @phan-file-suppress PhanUndeclaredClassMethod, PhanUndeclaredTypeProperty, PhanUndeclaredTypeParameter |
||
14 | */ |
||
15 | |||
16 | namespace chillerlan\SimpleCache; |
||
17 | |||
18 | use chillerlan\Settings\SettingsContainerInterface; |
||
19 | use Psr\Log\LoggerInterface; |
||
20 | use Redis; |
||
21 | |||
22 | use function array_combine, array_keys; |
||
23 | |||
24 | class RedisCache extends CacheDriverAbstract{ |
||
25 | |||
26 | protected Redis $redis; |
||
27 | |||
28 | /** |
||
29 | * RedisCache constructor. |
||
30 | */ |
||
31 | public function __construct(Redis $redis, SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
||
32 | parent::__construct($options, $logger); |
||
33 | |||
34 | $this->redis = $redis; |
||
35 | } |
||
36 | |||
37 | /** @inheritdoc */ |
||
38 | public function get($key, $default = null){ |
||
39 | $value = $this->redis->get($this->checkKey($key)); |
||
40 | |||
41 | if($value !== false){ |
||
42 | return $value; |
||
43 | } |
||
44 | |||
45 | return $default; |
||
46 | } |
||
47 | |||
48 | /** @inheritdoc */ |
||
49 | public function set($key, $value, $ttl = null):bool{ |
||
50 | $key = $this->checkKey($key); |
||
51 | $ttl = $this->getTTL($ttl); |
||
52 | |||
53 | if($ttl === null){ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
54 | return $this->redis->set($key, $value); |
||
55 | } |
||
56 | |||
57 | return $this->redis->setex($key, $ttl, $value); |
||
58 | } |
||
59 | |||
60 | /** @inheritdoc */ |
||
61 | public function delete($key):bool{ |
||
62 | return (bool)$this->redis->del($this->checkKey($key)); |
||
63 | } |
||
64 | |||
65 | /** @inheritdoc */ |
||
66 | public function clear():bool{ |
||
67 | return $this->redis->flushDB(); |
||
68 | } |
||
69 | |||
70 | /** @inheritdoc */ |
||
71 | public function getMultiple($keys, $default = null):array{ |
||
72 | $keys = $this->getData($keys); |
||
73 | |||
74 | $this->checkKeyArray($keys); |
||
75 | |||
76 | // scary |
||
77 | $values = array_combine($keys, $this->redis->mget($keys)); |
||
78 | $return = []; |
||
79 | |||
80 | foreach($keys as $key){ |
||
81 | /** @phan-suppress-next-line PhanTypeArraySuspiciousNullable */ |
||
82 | $return[$key] = $values[$key] !== false ? $values[$key] : $default; |
||
83 | } |
||
84 | |||
85 | return $return; |
||
86 | } |
||
87 | |||
88 | /** @inheritdoc */ |
||
89 | public function setMultiple($values, $ttl = null):bool{ |
||
90 | $values = $this->getData($values); |
||
91 | $ttl = $this->getTTL($ttl); |
||
92 | |||
93 | if($ttl === null){ |
||
0 ignored issues
–
show
|
|||
94 | $this->checkKeyArray(array_keys($values)); |
||
95 | |||
96 | return $this->redis->msetnx($values); |
||
97 | } |
||
98 | |||
99 | $return = []; |
||
100 | |||
101 | foreach($values as $key => $value){ |
||
102 | $return[] = $this->set($key, $value, $ttl); |
||
103 | } |
||
104 | |||
105 | return $this->checkReturn($return); |
||
106 | } |
||
107 | |||
108 | /** @inheritdoc */ |
||
109 | public function deleteMultiple($keys):bool{ |
||
110 | $keys = $this->getData($keys); |
||
111 | |||
112 | $this->checkKeyArray($keys); |
||
113 | |||
114 | return (bool)$this->redis->del($keys); |
||
115 | } |
||
116 | |||
117 | } |
||
118 |