Skip to content

Commit 705ed23

Browse files
committed
Support Redis 6.0 username + password
1 parent 8af05ef commit 705ed23

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/config/think-cache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
'type' => 'redis',
1111
// 服务器地址
1212
'host' => '127.0.0.1',
13+
// 无 ACL 用户请留空 username(Redis 6.0 起支持用户名 + 密码认证)
14+
'username' => '',
15+
// redis密码
16+
'password' => '',
1317
// 缓存前缀
1418
'prefix' => 'cache:',
1519
// 默认缓存有效期 0表示永久缓存

src/driver/Redis.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Redis extends Driver
3232
protected $options = [
3333
'host' => '127.0.0.1',
3434
'port' => 6379,
35+
'username' => '',
3536
'password' => '',
3637
'select' => 0,
3738
'timeout' => 0,
@@ -58,7 +59,11 @@ public function __construct(array $options = [])
5859
$this->handler = new \Redis;
5960
$this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
6061
if ('' != $this->options['password']) {
61-
$this->handler->auth($this->options['password']);
62+
if ('' != $this->options['username']) {
63+
$this->handler->auth([$this->options['username'], $this->options['password']]);
64+
} else {
65+
$this->handler->auth($this->options['password']);
66+
}
6267
}
6368
} elseif (class_exists('\Predis\Client')) {
6469
$params = [];
@@ -71,6 +76,9 @@ public function __construct(array $options = [])
7176
if ('' == $this->options['password']) {
7277
unset($this->options['password']);
7378
}
79+
if ('' == $this->options['username']) {
80+
unset($this->options['username']);
81+
}
7482
$this->handler = new \Predis\Client($this->options, $params);
7583
$this->options['prefix'] = '';
7684
} else {

0 commit comments

Comments
 (0)