8000 [Cache] Support Redis Sentinel mode when using phpredis/phpredis extension by renan · Pull Request #39363 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Support Redis Sentinel mode when using phpredis/phpredis extension #39363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! Support Redis Sentinel mode when using phpredis/phpredis exten…
…sion
  • Loading branch information
renan committed Dec 8, 2020
commit ba6bb87402aaec6555c25cc7a79266195f72baf9
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Cache\Tests\Adapter;

use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

/**
* @group integration
Expand All @@ -33,12 +32,4 @@ public static function setUpBeforeClass(): void

self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => $service, 'class' => \Predis\Client::class]);
}

public function testInvalidDSNHasBothClusterAndSentinel()
{
$this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Cannot use both "redis_cluster" and "redis_sentinel" at the same time:');
$dsn = 'redis:?host[redis1]&host[redis2]&host[redis3]&redis_cluster=1&redis_sentinel=mymaster';
RedisAdapter::createConnection($dsn);
}
}
17 changes: 7 additions & 10 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static function createConnection($dsn, array $options = [])
$params += $query + $options + self::$defaultConnectionOptions;

if (isset($params['redis_sentinel']) && (!class_exists(\Predis\Client::class) || !class_exists(\RedisSentinel::class))) {
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension with minimum version v5.2.0: "%s".', $dsn));
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s".', $dsn));
}

if ($params['redis_cluster'] && isset($params['redis_sentinel'])) {
Expand All @@ -180,22 +180,19 @@ public static function createConnection($dsn, array $options = [])
$redis = new $class();

$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
$address = $hosts[0]['host'] ?? $hosts[0]['path'];
$host = $hosts[0]['host'] ?? $hosts[0]['path'];
$port = $hosts[0]['port'] ?? null;

if (isset($params['redis_sentinel'])) {
$sentinel = new \RedisSentinel($address, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);
$master = $sentinel->getMasterAddrByName($params['redis_sentinel']);
if (false === $master) {
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $params['redis_sentinel'], $address, $port));
}
$sentinel = new \RedisSentinel($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);

$address = $master[0];
$port = $master[1];
if (!([$host, $port] = $sentinel->getMasterAddrByName($params['redis_sentinel']))) {
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $params['redis_sentinel'], $host, $port));
}
}

try {
@$redis->{$connect}($address, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);

set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
$isConnected = $redis->isConnected();
Expand Down
0