1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon Simple Cache package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\SimpleCache\Driver; |
14
|
|
|
|
15
|
|
|
use Shieldon\SimpleCache\CacheProvider; |
16
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
17
|
|
|
use Redis as RedisServer; |
18
|
|
|
use Exception; |
19
|
|
|
use function array_keys; |
20
|
|
|
use function extension_loaded; |
21
|
|
|
use function unserialize; |
22
|
|
|
use function serialize; |
23
|
|
|
use function is_bool; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A cache driver class provided by Redis database. |
27
|
|
|
*/ |
28
|
|
|
class Redis extends CacheProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The Redis instance. |
32
|
|
|
* |
33
|
|
|
* @var Redis|null |
34
|
|
|
*/ |
35
|
|
|
protected $redis = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param array $setting The settings. |
41
|
|
|
* |
42
|
|
|
* @throws CacheException |
43
|
|
|
*/ |
44
|
10 |
|
public function __construct(array $setting = []) |
45
|
|
|
{ |
46
|
|
|
$config = [ |
47
|
10 |
|
'host' => '127.0.0.1', |
48
|
|
|
'port' => 6379, |
49
|
|
|
'user' => null, |
50
|
|
|
'pass' => null, |
51
|
|
|
]; |
52
|
|
|
|
53
|
10 |
|
foreach (array_keys($config) as $key) { |
54
|
10 |
|
if (isset($setting[$key])) { |
55
|
10 |
|
$config[$key] = $setting[$key]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
$this->connect($config); |
60
|
10 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Connect to Redis server. |
64
|
|
|
* |
65
|
|
|
* @param array $config The settings. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
* |
69
|
|
|
* @throws CacheException |
70
|
|
|
*/ |
71
|
10 |
|
protected function connect(array $config): void |
72
|
|
|
{ |
73
|
10 |
|
if (extension_loaded('redis')) { |
74
|
|
|
try { |
75
|
10 |
|
$this->redis = new RedisServer(); |
|
|
|
|
76
|
10 |
|
$this->redis->connect($config['host'], $config['port']); |
77
|
10 |
|
$this->auth($config); |
78
|
|
|
|
79
|
|
|
// @codeCoverageIgnoreStart |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
throw new CacheException($e->getMessage()); |
82
|
|
|
} |
83
|
|
|
// @codeCoverageIgnoreEnd |
84
|
10 |
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// @codeCoverageIgnoreStart |
88
|
|
|
throw new CacheException( |
89
|
|
|
'PHP Redis extension is not installed on your system.' |
90
|
|
|
); |
91
|
|
|
// @codeCoverageIgnoreEnd |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Redis authentication. |
96
|
|
|
* @codeCoverageIgnore |
97
|
|
|
* |
98
|
|
|
* @param array $config The user / pass data. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function auth(array $config = []): void |
103
|
|
|
{ |
104
|
|
|
if ($this->getVersion() >= 6) { |
105
|
|
|
if (!empty($config['user']) && !empty($config['pass'])) { |
106
|
|
|
$this->redis->auth([ |
|
|
|
|
107
|
|
|
$config['user'], |
108
|
|
|
$config['pass'], |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!empty($config['pass'])) { |
115
|
|
|
$this->redis->auth($config['pass']); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get Redis version number. |
121
|
|
|
*/ |
122
|
10 |
|
protected function getVersion(): int |
123
|
|
|
{ |
124
|
10 |
|
$info = $this->redis->info(); |
|
|
|
|
125
|
|
|
|
126
|
10 |
|
return (int) $info['redis_version']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Fetch a cache by an extended Cache Driver. |
131
|
|
|
* |
132
|
|
|
* @param string $key The key of a cache. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
6 |
|
protected function doGet(string $key): array |
137
|
|
|
{ |
138
|
6 |
|
$content = $this->redis->get($this->getKeyName($key)); |
139
|
|
|
|
140
|
6 |
|
if (empty($content)) { |
141
|
6 |
|
return []; |
142
|
|
|
} |
143
|
6 |
|
$data = unserialize($content); |
144
|
|
|
|
145
|
6 |
|
return $data; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set a cache by an extended Cache Driver. |
150
|
|
|
* |
151
|
|
|
* @param string $key The key of a cache. |
152
|
|
|
* @param mixed $value The value of a cache. (serialized) |
153
|
|
|
* @param int $ttl The time to live for a cache. |
154
|
|
|
* @param int $timestamp The time to store a cache. |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
6 |
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
159
|
|
|
{ |
160
|
|
|
$contents = [ |
161
|
6 |
|
'timestamp' => $timestamp, |
162
|
6 |
|
'ttl' => $ttl, |
163
|
6 |
|
'value' => $value |
164
|
|
|
]; |
165
|
|
|
|
166
|
6 |
|
$result = $this->redis->set( |
167
|
6 |
|
$this->getKeyName($key), |
168
|
6 |
|
serialize($contents), |
169
|
6 |
|
$ttl |
170
|
|
|
); |
171
|
|
|
|
172
|
6 |
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Delete a cache by an extended Cache Driver. |
177
|
|
|
* |
178
|
|
|
* @param string $key The key of a cache. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
2 |
|
protected function doDelete(string $key): bool |
183
|
|
|
{ |
184
|
2 |
|
return $this->redis->del($this->getKeyName($key)) >= 0; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Delete all caches by an extended Cache Driver. |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
2 |
|
protected function doClear(): bool |
193
|
|
|
{ |
194
|
2 |
|
$keys = $this->redis->keys('simple_cache:*'); |
|
|
|
|
195
|
|
|
|
196
|
2 |
|
if (!empty($keys)) { |
197
|
2 |
|
foreach ($keys as $key) { |
198
|
2 |
|
$this->redis->del($key); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Check if the cache exists or not. |
207
|
|
|
* |
208
|
|
|
* @param string $key The key of a cache. |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
4 |
|
protected function doHas(string $key): bool |
213
|
|
|
{ |
214
|
4 |
|
$exist = $this->redis->exists($this->getKeyName($key)); |
|
|
|
|
215
|
|
|
|
216
|
|
|
// This function took a single argument and returned TRUE or FALSE in phpredis versions < 4.0.0. |
217
|
|
|
|
218
|
|
|
// @codeCoverageIgnoreStart |
219
|
|
|
if (is_bool($exist)) { |
220
|
|
|
return $exist; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $exist > 0; |
224
|
|
|
// @codeCoverageIgnoreEnd |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the key name of a cache. |
229
|
|
|
* |
230
|
|
|
* @param string $key The key of a cache. |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
6 |
|
private function getKeyName(string $key): string |
235
|
|
|
{ |
236
|
6 |
|
return 'simple_cache:' . md5($key); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..