[Cache] phpredis: Added full TLS support for RedisCluster
This commit is contained in:
parent
2dd8445198
commit
a1e0408d08
48
src/Symfony/Component/Cache/Traits/RedisClusterNodeProxy.php
Normal file
48
src/Symfony/Component/Cache/Traits/RedisClusterNodeProxy.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Cache\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file acts as a wrapper to the \RedisCluster implementation so it can accept the same type of calls as
|
||||||
|
* individual \Redis objects.
|
||||||
|
*
|
||||||
|
* Calls are made to individual nodes via: RedisCluster->{method}($host, ...args)'
|
||||||
|
* according to https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#directed-node-commands
|
||||||
|
*
|
||||||
|
* @author Jack Thomas <jack.thomas@solidalpha.com>
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class RedisClusterNodeProxy
|
||||||
|
{
|
||||||
|
private $host;
|
||||||
|
private $redis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \RedisCluster|RedisClusterProxy $redis
|
||||||
|
*/
|
||||||
|
public function __construct(array $host, $redis)
|
||||||
|
{
|
||||||
|
$this->host = $host;
|
||||||
|
$this->redis = $redis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call(string $method, array $args)
|
||||||
|
{
|
||||||
|
return $this->redis->{$method}($this->host, ...$args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scan(&$iIterator, $strPattern = null, $iCount = null)
|
||||||
|
{
|
||||||
|
return $this->redis->scan($iIterator, $this->host, $strPattern, $iCount);
|
||||||
|
}
|
||||||
|
}
|
@ -41,6 +41,7 @@ trait RedisTrait
|
|||||||
'redis_sentinel' => null,
|
'redis_sentinel' => null,
|
||||||
'dbindex' => 0,
|
'dbindex' => 0,
|
||||||
'failover' => 'none',
|
'failover' => 'none',
|
||||||
|
'ssl' => null, // see https://php.net/context.ssl
|
||||||
];
|
];
|
||||||
private $redis;
|
private $redis;
|
||||||
private $marshaller;
|
private $marshaller;
|
||||||
@ -187,7 +188,7 @@ trait RedisTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
|
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout'], ['stream' => $params['ssl'] ?? null]);
|
||||||
|
|
||||||
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
||||||
$isConnected = $redis->isConnected();
|
$isConnected = $redis->isConnected();
|
||||||
@ -250,7 +251,7 @@ trait RedisTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '');
|
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', $params['ssl'] ?? null);
|
||||||
} catch (\RedisClusterException $e) {
|
} catch (\RedisClusterException $e) {
|
||||||
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
|
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
|
||||||
}
|
}
|
||||||
@ -299,7 +300,7 @@ trait RedisTrait
|
|||||||
}
|
}
|
||||||
$params['exceptions'] = false;
|
$params['exceptions'] = false;
|
||||||
|
|
||||||
$redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
|
$redis = new $class($hosts, array_diff_key($params, array_diff_key(self::$defaultConnectionOptions, ['ssl' => null])));
|
||||||
if (isset($params['redis_sentinel'])) {
|
if (isset($params['redis_sentinel'])) {
|
||||||
$redis->getConnection()->setSentinelTimeout($params['timeout']);
|
$redis->getConnection()->setSentinelTimeout($params['timeout']);
|
||||||
}
|
}
|
||||||
@ -530,8 +531,7 @@ trait RedisTrait
|
|||||||
} elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
|
} elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
|
||||||
$hosts = [];
|
$hosts = [];
|
||||||
foreach ($this->redis->_masters() as $host) {
|
foreach ($this->redis->_masters() as $host) {
|
||||||
$hosts[] = $h = new \Redis();
|
$hosts[] = new RedisClusterNodeProxy($host, $this->redis);
|
||||||
$h->connect($host[0], $host[1]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user