Skip to content

Commit dfb9ba9

Browse files
committed
Properly support RedisCluster
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent 66c1e05 commit dfb9ba9

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

config/config.sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@
12341234
'user' => '', // Optional, if not defined no password will be used.
12351235
'password' => '', // Optional, if not defined no password will be used.
12361236
'dbindex' => 0, // Optional, if undefined SELECT will not run and will use Redis Server's default DB Index.
1237-
// If redis is encrypted, provide certificates
1237+
// If redis in-transit encryption is enabled, provide certificates
12381238
// SSL context https://www.php.net/manual/en/context.ssl.php
12391239
'ssl_context' => [
12401240
'local_cert' => '/certs/redis.crt',

lib/private/RedisFactory.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,29 @@ public function __construct(SystemConfig $config) {
4646
}
4747

4848
private function create() {
49-
$isCluster = !empty($this->config->getValue('redis.cluster', []));
50-
$config = $this->config->getValue('redis', []);
49+
$isCluster = in_array('redis.cluster', $this->config->getKeys());
50+
$config = $isCluster
51+
? $this->config->getValue('redis.cluster', [])
52+
: $this->config->getValue('redis', []);
5153

52-
// Init cluster config if any
53-
if ($isCluster) {
54-
if (!class_exists('RedisCluster')) {
55-
throw new \Exception('Redis Cluster support is not available');
56-
}
57-
// Replace config with the cluster config
58-
$config = $this->config->getValue('redis.cluster', []);
54+
if (empty($config)) {
55+
throw new \Exception('Redis config is empty');
56+
}
57+
58+
if ($isCluster && !class_exists('RedisCluster')) {
59+
throw new \Exception('Redis Cluster support is not available');
5960
}
6061

6162
if (isset($config['timeout'])) {
6263
$timeout = $config['timeout'];
6364
} else {
64-
$timeout = null;
65+
$timeout = 0.0;
6566
}
6667

6768
if (isset($config['read_timeout'])) {
6869
$readTimeout = $config['read_timeout'];
6970
} else {
70-
$readTimeout = null;
71+
$readTimeout = 0.0;
7172
}
7273

7374
$auth = null;
@@ -85,11 +86,7 @@ private function create() {
8586

8687
// cluster config
8788
if ($isCluster) {
88-
if ($connectionParameters !== null) {
89-
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth, $connectionParameters);
90-
} else {
91-
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, $auth);
92-
}
89+
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth, $connectionParameters);
9390

9491
if (isset($config['failover_mode'])) {
9592
$this->instance->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover_mode']);
@@ -111,7 +108,8 @@ private function create() {
111108
$port = null;
112109
}
113110

114-
if (!empty($connectionParameters)) {
111+
if ($connectionParameters !== null) {
112+
// Non-clustered redis requires connection parameters to be wrapped inside `stream`
115113
$connectionParameters = [
116114
'stream' => $this->getSslContext($config)
117115
];
@@ -134,7 +132,7 @@ private function create() {
134132
* Get the ssl context config
135133
*
136134
* @param Array $config the current config
137-
* @return Array
135+
* @return Array|null
138136
* @throws \UnexpectedValueException
139137
*/
140138
private function getSslContext($config) {
@@ -147,7 +145,7 @@ private function getSslContext($config) {
147145
}
148146
return $config['ssl_context'];
149147
}
150-
return [];
148+
return null;
151149
}
152150

153151
public function getInstance() {

0 commit comments

Comments
 (0)