From eaf6f8ce3e216af51b6d87e82a19c74e1b6cbf0d Mon Sep 17 00:00:00 2001 From: mfettig Date: Fri, 24 Mar 2023 15:07:33 -0400 Subject: [PATCH] [Cache] Support Redis cluster connections with predis/predis:^2.0 --- composer.json | 2 +- src/Symfony/Component/Cache/CHANGELOG.md | 1 + src/Symfony/Component/Cache/Traits/RedisTrait.php | 10 ++++++---- src/Symfony/Component/Cache/composer.json | 2 +- .../Handler/PredisClusterSessionHandlerTest.php | 5 ++++- src/Symfony/Component/HttpFoundation/composer.json | 2 +- src/Symfony/Component/Lock/composer.json | 2 +- src/Symfony/Component/Semaphore/composer.json | 2 +- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 25b14db4bc587..38dc3d5d70bf4 100644 --- a/composer.json +++ b/composer.json @@ -143,7 +143,7 @@ "php-http/httplug": "^1.0|^2.0", "phpdocumentor/reflection-docblock": "^5.2", "phpstan/phpdoc-parser": "^1.0", - "predis/predis": "~1.1", + "predis/predis": "^1.1|^2.0", "psr/http-client": "^1.0", "psr/simple-cache": "^1.0|^2.0|^3.0", "symfony/mercure-bundle": "^0.3", diff --git a/src/Symfony/Component/Cache/CHANGELOG.md b/src/Symfony/Component/Cache/CHANGELOG.md index 635a9408176b9..57646c2bc875f 100644 --- a/src/Symfony/Component/Cache/CHANGELOG.md +++ b/src/Symfony/Component/Cache/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add support for Relay PHP extension for Redis + * Updates to allow Redis cluster connections using predis/predis:^2.0 6.1 --- diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index 03bc5ba6242de..33ae696eac46c 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -15,6 +15,8 @@ use Predis\Connection\Aggregate\ClusterInterface; use Predis\Connection\Aggregate\RedisCluster; use Predis\Connection\Aggregate\ReplicationInterface; +use Predis\Connection\Cluster\ClusterInterface as Predis2ClusterInterface; +use Predis\Connection\Cluster\RedisCluster as Predis2RedisCluster; use Predis\Response\ErrorInterface; use Predis\Response\Status; use Relay\Relay; @@ -376,7 +378,7 @@ protected function doFetch(array $ids): iterable $result = []; - if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) { + if ($this->redis instanceof \Predis\ClientInterface && ($this->redis->getConnection() instanceof ClusterInterface || $this->redis->getConnection() instanceof Predis2ClusterInterface)) { $values = $this->pipeline(function () use ($ids) { foreach ($ids as $id) { yield 'get' => [$id]; @@ -476,7 +478,7 @@ protected function doDelete(array $ids): bool return true; } - if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) { + if ($this->redis instanceof \Predis\ClientInterface && ($this->redis->getConnection() instanceof ClusterInterface || $this->redis->getConnection() instanceof Predis2ClusterInterface)) { static $del; $del ??= (class_exists(UNLINK::class) ? 'unlink' : 'del'); @@ -534,7 +536,7 @@ private function pipeline(\Closure $generator, object $redis = null): \Generator $ids = []; $redis ??= $this->redis; - if ($redis instanceof \RedisCluster || ($redis instanceof \Predis\ClientInterface && $redis->getConnection() instanceof RedisCluster)) { + if ($redis instanceof \RedisCluster || ($redis instanceof \Predis\ClientInterface && ($redis->getConnection() instanceof RedisCluster || $redis->getConnection() instanceof Predis2RedisCluster))) { // phpredis & predis don't support pipelining with RedisCluster // see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining // see https://github.com/nrk/predis/issues/267#issuecomment-123781423 @@ -596,7 +598,7 @@ private function getHosts(): array $hosts = [$this->redis]; if ($this->redis instanceof \Predis\ClientInterface) { $connection = $this->redis->getConnection(); - if ($connection instanceof ClusterInterface && $connection instanceof \Traversable) { + if (($connection instanceof ClusterInterface || $connection instanceof Predis2ClusterInterface) && $connection instanceof \Traversable) { $hosts = []; foreach ($connection as $c) { $hosts[] = new \Predis\Client($c); diff --git a/src/Symfony/Component/Cache/composer.json b/src/Symfony/Component/Cache/composer.json index 764b1c0fcea81..35eae21746ad0 100644 --- a/src/Symfony/Component/Cache/composer.json +++ b/src/Symfony/Component/Cache/composer.json @@ -31,7 +31,7 @@ "require-dev": { "cache/integration-tests": "dev-master", "doctrine/dbal": "^2.13.1|^3.0", - "predis/predis": "^1.1", + "predis/predis": "^1.1|^2.0", "psr/simple-cache": "^1.0|^2.0|^3.0", "symfony/config": "^5.4|^6.0", "symfony/dependency-injection": "^5.4|^6.0", diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php index fd4a13bef5fdf..492487766b2f5 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php @@ -20,6 +20,9 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas { protected function createRedisClient(string $host): Client { - return new Client([array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])]); + return new Client( + [array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])], + ['cluster' => 'redis'] + ); } } diff --git a/src/Symfony/Component/HttpFoundation/composer.json b/src/Symfony/Component/HttpFoundation/composer.json index 944029655a181..245cde53f4f3b 100644 --- a/src/Symfony/Component/HttpFoundation/composer.json +++ b/src/Symfony/Component/HttpFoundation/composer.json @@ -23,7 +23,7 @@ }, "require-dev": { "doctrine/dbal": "^2.13.1|^3.0", - "predis/predis": "~1.0", + "predis/predis": "^1.1|^2.0", "symfony/cache": "^5.4|^6.0", "symfony/dependency-injection": "^5.4|^6.0", "symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4", diff --git a/src/Symfony/Component/Lock/composer.json b/src/Symfony/Component/Lock/composer.json index c0034f311a685..80a76a1b00c5f 100644 --- a/src/Symfony/Component/Lock/composer.json +++ b/src/Symfony/Component/Lock/composer.json @@ -22,7 +22,7 @@ }, "require-dev": { "doctrine/dbal": "^2.13|^3.0", - "predis/predis": "~1.0" + "predis/predis": "^1.1|^2.0" }, "conflict": { "doctrine/dbal": "<2.13", diff --git a/src/Symfony/Component/Semaphore/composer.json b/src/Symfony/Component/Semaphore/composer.json index ef30494122109..8266c9fcc32bb 100644 --- a/src/Symfony/Component/Semaphore/composer.json +++ b/src/Symfony/Component/Semaphore/composer.json @@ -24,7 +24,7 @@ "psr/log": "^1|^2|^3" }, "require-dev": { - "predis/predis": "~1.0" + "predis/predis": "^1.1|^2.0" }, "conflict": { "symfony/cache": "<6.2"