Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 444e0d9

Browse filesBrowse files
committed
Add Redis Sentinel support
1 parent 6852c84 commit 444e0d9
Copy full SHA for 444e0d9

File tree

Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed

‎src/Symfony/Component/Cache/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* removed `psr/simple-cache` dependency, run `composer require psr/simple-cache` if you need it
88
* deprecated all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead
99
* deprecated `SimpleCacheAdapter`, use `Psr16Adapter` instead
10+
* added support for connecting to Redis Sentinel clusters
1011

1112
4.2.0
1213
-----
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
15+
use Symfony\Component\Cache\Adapter\RedisAdapter;
16+
17+
class RedisAdapterSentinelTest extends AbstractRedisAdapterTest
18+
{
19+
public static function setupBeforeClass()
20+
{
21+
if (!class_exists('Predis\Client')) {
22+
self::markTestSkipped('The Predis\Client class is required.');
23+
}
24+
if (!$hosts = getenv('REDIS_SENTINEL_HOSTS')) {
25+
self::markTestSkipped('REDIS_SENTINEL_HOSTS env var is not defined.');
26+
}
27+
if (!$service = getenv('REDIS_SENTINEL_SERVICE')) {
28+
self::markTestSkipped('REDIS_SENTINEL_SERVICE env var is not defined.');
29+
}
30+
31+
self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => $service]);
32+
}
33+
}

‎src/Symfony/Component/Cache/Traits/RedisTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Traits/RedisTrait.php
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ trait RedisTrait
3838
'tcp_keepalive' => 0,
3939
'lazy' => null,
4040
'redis_cluster' => false,
41+
'redis_sentinel' => false,
4142
'dbindex' => 0,
4243
'failover' => 'none',
4344
];
@@ -146,9 +147,13 @@ public static function createConnection($dsn, array $options = [])
146147
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: %s', $dsn));
147148
}
148149

150+
if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class)) {
151+
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package: %s', $dsn));
152+
}
153+
149154
$params += $query + $options + self::$defaultConnectionOptions;
150155

151-
if (null === $params['class'] && \extension_loaded('redis')) {
156+
if (null === $params['class'] && !$params['redis_sentinel'] && \extension_loaded('redis')) {
152157
$class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) ? \RedisArray::class : \Redis::class);
153158
} else {
154159
$class = null === $params['class'] ? \Predis\Client::class : $params['class'];
@@ -246,6 +251,9 @@ public static function createConnection($dsn, array $options = [])
246251
} elseif (is_a($class, \Predis\Client::class, true)) {
247252
if ($params['redis_cluster']) {
248253
$params['cluster'] = 'redis';
254+
} elseif ($params['redis_sentinel']) {
255+
$params['replication'] = 'sentinel';
256+
$params['service'] = $params['redis_sentinel'];
249257
}
250258
$params += ['parameters' => []];
251259
$params['parameters'] += [
@@ -268,6 +276,9 @@ public static function createConnection($dsn, array $options = [])
268276
}
269277

270278
$redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
279+
if ($params['redis_sentinel']) {
280+
$redis->getConnection()->setSentinelTimeout($params['timeout']);
281+
}
271282
} elseif (class_exists($class, false)) {
272283
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis", "RedisArray", "RedisCluster" nor "Predis\Client".', $class));
273284
} else {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.