Skip to content

Commit b93a8d2

Browse files
committed
[Cache] Support Redis cluster connections with predis/predis:^2.0
1 parent aadd302 commit b93a8d2

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"php-http/httplug": "^1.0|^2.0",
144144
"phpdocumentor/reflection-docblock": "^5.2",
145145
"phpstan/phpdoc-parser": "^1.0",
146-
"predis/predis": "~1.1",
146+
"predis/predis": "^1.1|^2.0",
147147
"psr/http-client": "^1.0",
148148
"psr/simple-cache": "^1.0|^2.0|^3.0",
149149
"symfony/mercure-bundle": "^0.3",

src/Symfony/Component/Cache/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for Relay PHP extension for Redis
8+
* Updates to allow Redis cluster connections using predis/predis:^2.0
89

910
6.1
1011
---

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Predis\Connection\Aggregate\ClusterInterface;
1616
use Predis\Connection\Aggregate\RedisCluster;
1717
use Predis\Connection\Aggregate\ReplicationInterface;
18+
use Predis\Connection\Cluster\ClusterInterface as Predis2ClusterInterface;
19+
use Predis\Connection\Cluster\RedisCluster as Predis2RedisCluster;
1820
use Predis\Response\ErrorInterface;
1921
use Predis\Response\Status;
2022
use Relay\Relay;
@@ -376,7 +378,7 @@ protected function doFetch(array $ids): iterable
376378

377379
$result = [];
378380

379-
if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
381+
if ($this->redis instanceof \Predis\ClientInterface && ($this->redis->getConnection() instanceof ClusterInterface || $this->redis->getConnection() instanceof Predis2ClusterInterface)) {
380382
$values = $this->pipeline(function () use ($ids) {
381383
foreach ($ids as $id) {
382384
yield 'get' => [$id];
@@ -476,7 +478,7 @@ protected function doDelete(array $ids): bool
476478
return true;
477479
}
478480

479-
if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
481+
if ($this->redis instanceof \Predis\ClientInterface && ($this->redis->getConnection() instanceof ClusterInterface || $this->redis->getConnection() instanceof Predis2ClusterInterface)) {
480482
static $del;
481483
$del ??= (class_exists(UNLINK::class) ? 'unlink' : 'del');
482484

@@ -534,7 +536,7 @@ private function pipeline(\Closure $generator, object $redis = null): \Generator
534536
$ids = [];
535537
$redis ??= $this->redis;
536538

537-
if ($redis instanceof \RedisCluster || ($redis instanceof \Predis\ClientInterface && $redis->getConnection() instanceof RedisCluster)) {
539+
if ($redis instanceof \RedisCluster || ($redis instanceof \Predis\ClientInterface && ($redis->getConnection() instanceof RedisCluster || $redis->getConnection() instanceof Predis2RedisCluster))) {
538540
// phpredis & predis don't support pipelining with RedisCluster
539541
// see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
540542
// see https://github.com/nrk/predis/issues/267#issuecomment-123781423
@@ -596,7 +598,7 @@ private function getHosts(): array
596598
$hosts = [$this->redis];
597599
if ($this->redis instanceof \Predis\ClientInterface) {
598600
$connection = $this->redis->getConnection();
599-
if ($connection instanceof ClusterInterface && $connection instanceof \Traversable) {
601+
if (($connection instanceof ClusterInterface || $connection instanceof Predis2ClusterInterface) && $connection instanceof \Traversable) {
600602
$hosts = [];
601603
foreach ($connection as $c) {
602604
$hosts[] = new \Predis\Client($c);

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas
2020
{
2121
protected function createRedisClient(string $host): Client
2222
{
23-
return new Client([array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])]);
23+
return new Client(
24+
[array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])],
25+
['cluster' => 'redis']
26+
);
2427
}
2528
}

0 commit comments

Comments
 (0)