|
16 | 16 | use DateInterval; |
17 | 17 | use DateTimeInterface; |
18 | 18 | use RedisException; |
19 | | -use Workerman\Coroutine\Pool; |
20 | 19 | use Webman\ThinkCache\Driver; |
21 | 20 |
|
22 | 21 | class Redis extends Driver |
23 | 22 | { |
24 | | - |
25 | | - /** |
26 | | - * @var Pool[] |
27 | | - */ |
28 | | - protected static array $pools = []; |
29 | | - |
30 | 23 | /** |
31 | 24 | * @var \Redis |
32 | 25 | */ |
@@ -61,12 +54,28 @@ public function __construct(array $options = []) |
61 | 54 | $this->options = array_merge($this->options, $options); |
62 | 55 | } |
63 | 56 |
|
64 | | - $this->handler = new \Redis; |
65 | | - $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']); |
66 | | - if ('' != $this->options['password']) { |
67 | | - $this->handler->auth($this->options['password']); |
| 57 | + if (extension_loaded('redis')) { |
| 58 | + $this->handler = new \Redis; |
| 59 | + $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']); |
| 60 | + if ('' != $this->options['password']) { |
| 61 | + $this->handler->auth($this->options['password']); |
| 62 | + } |
| 63 | + } elseif (class_exists('\Predis\Client')) { |
| 64 | + $params = []; |
| 65 | + foreach ($this->options as $key => $val) { |
| 66 | + if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) { |
| 67 | + $params[$key] = $val; |
| 68 | + unset($this->options[$key]); |
| 69 | + } |
| 70 | + } |
| 71 | + if ('' == $this->options['password']) { |
| 72 | + unset($this->options['password']); |
| 73 | + } |
| 74 | + $this->handler = new \Predis\Client($this->options, $params); |
| 75 | + $this->options['prefix'] = ''; |
| 76 | + } else { |
| 77 | + throw new BadFunctionCallException('not support: redis'); |
68 | 78 | } |
69 | | - |
70 | 79 | if (0 != $this->options['select']) { |
71 | 80 | $this->handler->select((int) $this->options['select']); |
72 | 81 | } |
@@ -192,6 +201,9 @@ public function clear(): bool |
192 | 201 | */ |
193 | 202 | public function clearTag($keys): void |
194 | 203 | { |
| 204 | + if (empty($keys)) { |
| 205 | + return; |
| 206 | + } |
195 | 207 | // 指定标签清除 |
196 | 208 | $this->handler->del($keys); |
197 | 209 | } |
@@ -234,7 +246,11 @@ public function getTagItems($tag): array |
234 | 246 | */ |
235 | 247 | public function close() |
236 | 248 | { |
237 | | - $this->handler->close(); |
| 249 | + if (method_exists($this->handler, 'close')) { |
| 250 | + $this->handler->close(); |
| 251 | + } else { |
| 252 | + $this->handler->quit(); |
| 253 | + } |
238 | 254 | $this->handler = null; |
239 | 255 | } |
240 | 256 | } |
0 commit comments