8000 cluster support for predis dsn · symfony/symfony@1d52b4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d52b4c

Browse files
committed
cluster support for predis dsn
1 parent a250c51 commit 1d52b4c

File tree

2 files changed

+23
-5
lines changed
  • src/Symfony/Component/Cache

2 files changed

+23
-5
lines changed

src/Symfony/Component/Cache/Tests/Adapter/PredisRedisClusterAdapterTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14+
use Symfony\Component\Cache\Adapter\RedisAdapter;
15+
1416
class PredisRedisClusterAdapterTest extends AbstractRedisAdapterTest
1517
{
1618
public static function setupBeforeClass()
1719
{
1820
if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
1921
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
2022
}
21-
self::$redis = new \Predis\Client(explode(' ', $hosts), array('cluster' => 'redis'));
23+
24+
self::$redis = RedisAdapter::createConnection('redis://'.str_replace(' ', ',', $hosts), array('class' => \Predis\Client::class, 'cluster' => 'server'));
2225
}
2326

2427
public static function tearDownAfterClass()

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,25 @@ public static function createConnection($dsn, array $options = array())
182182

183183
$redis = $params['lazy'] ? new RedisClusterProxy($initializer) : $initializer();
184184
} elseif (is_a($class, \Predis\Client::class, true)) {
185-
$params['scheme'] = $scheme;
186-
$params['database'] = $params['dbindex'] ?: null;
187-
$params['password'] = $auth;
188-
$redis = new $class((new Factory())->create($params));
185+
if ('server' === $params['cluster']) {
186+
$host = $params['host'];
187+
if (isset($params['port'])) {
188+
$host .= ':'.$params['port'];
189+
}
190+
191+
$host = array_map(function (\string $host) use ($scheme): \string {
192+
return $scheme.'://'.$host;
193+
}, explode(',', $host));
194+
195+
// Predis cluster only supports an array of hosts as first argument, otherwise
196+
// options array is ignored.
197+
$redis = new $class($host, array('cluster' => 'redis'));
198+
} else {
199+
$params['scheme'] = $scheme;
200+
$params['database'] = $params['dbindex'] ?: null;
201+
$params['password'] = $auth;
202+
$redis = new $class((new Factory())->create($params));
203+
}
189204
} elseif (class_exists($class, false)) {
190205
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
191206
} else {

0 commit comments

Comments
 (0)
0