Properly support RedisCluster

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
pull/27888/head
John Molakvoæ (skjnldsv) 3 years ago
parent 66c1e05452
commit 6e00fe8c26
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF

@ -1234,7 +1234,7 @@ $CONFIG = [
'user' => '', // Optional, if not defined no password will be used.
'password' => '', // Optional, if not defined no password will be used.
'dbindex' => 0, // Optional, if undefined SELECT will not run and will use Redis Server's default DB Index.
// If redis is encrypted, provide certificates
// If redis in-transit encryption is enabled, provide certificates
// SSL context https://www.php.net/manual/en/context.ssl.php
'ssl_context' => [
'local_cert' => '/certs/redis.crt',
@ -1278,7 +1278,7 @@ $CONFIG = [
'failover_mode' => \RedisCluster::FAILOVER_ERROR,
'user' => '', // Optional, if not defined no password will be used.
'password' => '', // Optional, if not defined no password will be used.
// If redis is encrypted, provide certificates
// If redis in-transit encryption is enabled, provide certificates
// SSL context https://www.php.net/manual/en/context.ssl.php
'ssl_context' => [
'local_cert' => '/certs/redis.crt',

@ -46,28 +46,29 @@ class RedisFactory {
}
private function create() {
$isCluster = !empty($this->config->getValue('redis.cluster', []));
$config = $this->config->getValue('redis', []);
$isCluster = in_array('redis.cluster', $this->config->getKeys());
$config = $isCluster
? $this->config->getValue('redis.cluster', [])
: $this->config->getValue('redis', []);
// Init cluster config if any
if ($isCluster) {
if (!class_exists('RedisCluster')) {
throw new \Exception('Redis Cluster support is not available');
}
// Replace config with the cluster config
$config = $this->config->getValue('redis.cluster', []);
if (empty($config)) {
throw new \Exception('Redis config is empty');
}
if ($isCluster && !class_exists('RedisCluster')) {
throw new \Exception('Redis Cluster support is not available');
}
if (isset($config['timeout'])) {
$timeout = $config['timeout'];
} else {
$timeout = null;
$timeout = 0.0;
}
if (isset($config['read_timeout'])) {
$readTimeout = $config['read_timeout'];
} else {
$readTimeout = null;
$readTimeout = 0.0;
}
$auth = null;
@ -85,10 +86,11 @@ class RedisFactory {
// cluster config
if ($isCluster) {
// Support for older phpredis versions not supporting connectionParameters
if ($connectionParameters !== null) {
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth, $connectionParameters);
} else {
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, $auth);
$this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth);
}
if (isset($config['failover_mode'])) {
@ -111,13 +113,17 @@ class RedisFactory {
$port = null;
}
if (!empty($connectionParameters)) {
// Support for older phpredis versions not supporting connectionParameters
if ($connectionParameters !== null) {
// Non-clustered redis requires connection parameters to be wrapped inside `stream`
$connectionParameters = [
'stream' => $this->getSslContext($config)
];
$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters);
} else {
$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout);
}
$this->instance->connect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters);
// Auth if configured
if ($auth !== null) {
@ -134,7 +140,7 @@ class RedisFactory {
* Get the ssl context config
*
* @param Array $config the current config
* @return Array
* @return Array|null
* @throws \UnexpectedValueException
*/
private function getSslContext($config) {
@ -147,7 +153,7 @@ class RedisFactory {
}
return $config['ssl_context'];
}
return [];
return null;
}
public function getInstance() {

Loading…
Cancel
Save