Skip to content

Commit 8af05ef

Browse files
authored
Merge pull request #9 from ichynul/main
支持predis
2 parents ab7c053 + d2f70f4 commit 8af05ef

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/driver/Redis.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@
1616
use DateInterval;
1717
use DateTimeInterface;
1818
use RedisException;
19-
use Workerman\Coroutine\Pool;
2019
use Webman\ThinkCache\Driver;
2120

2221
class Redis extends Driver
2322
{
24-
25-
/**
26-
* @var Pool[]
27-
*/
28-
protected static array $pools = [];
29-
3023
/**
3124
* @var \Redis
3225
*/
@@ -61,12 +54,28 @@ public function __construct(array $options = [])
6154
$this->options = array_merge($this->options, $options);
6255
}
6356

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');
6878
}
69-
7079
if (0 != $this->options['select']) {
7180
$this->handler->select((int) $this->options['select']);
7281
}
@@ -192,6 +201,9 @@ public function clear(): bool
192201
*/
193202
public function clearTag($keys): void
194203
{
204+
if (empty($keys)) {
205+
return;
206+
}
195207
// 指定标签清除
196208
$this->handler->del($keys);
197209
}
@@ -234,7 +246,11 @@ public function getTagItems($tag): array
234246
*/
235247
public function close()
236248
{
237-
$this->handler->close();
249+
if (method_exists($this->handler, 'close')) {
250+
$this->handler->close();
251+
} else {
252+
$this->handler->quit();
253+
}
238254
$this->handler = null;
239255
}
240256
}

0 commit comments

Comments
 (0)