Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/driver/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@
use DateInterval;
use DateTimeInterface;
use RedisException;
use Workerman\Coroutine\Pool;
use Webman\ThinkCache\Driver;

class Redis extends Driver
{

/**
* @var Pool[]
*/
protected static array $pools = [];

/**
* @var \Redis
*/
Expand Down Expand Up @@ -61,12 +54,28 @@ public function __construct(array $options = [])
$this->options = array_merge($this->options, $options);
}

$this->handler = new \Redis;
$this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
if ('' != $this->options['password']) {
$this->handler->auth($this->options['password']);
if (extension_loaded('redis')) {
$this->handler = new \Redis;
$this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
if ('' != $this->options['password']) {
$this->handler->auth($this->options['password']);
}
} elseif (class_exists('\Predis\Client')) {
$params = [];
foreach ($this->options as $key => $val) {
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
$params[$key] = $val;
unset($this->options[$key]);
}
}
if ('' == $this->options['password']) {
unset($this->options['password']);
}
$this->handler = new \Predis\Client($this->options, $params);
$this->options['prefix'] = '';
} else {
throw new BadFunctionCallException('not support: redis');
}

if (0 != $this->options['select']) {
$this->handler->select((int) $this->options['select']);
}
Expand Down Expand Up @@ -234,7 +243,11 @@ public function getTagItems($tag): array
*/
public function close()
{
$this->handler->close();
if (method_exists($this->handler, 'close')) {
$this->handler->close();
} else {
$this->handler->quit();
}
$this->handler = null;
}
}