Skip to content

Commit 14995ae

Browse files
dorrogerayfabpot
authored andcommitted
[Cache] Add \Relay\Cluster support
1 parent dd89076 commit 14995ae

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

Store/RedisStore.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Predis\Response\Error;
1515
use Predis\Response\ServerException;
1616
use Relay\Relay;
17+
use Relay\Cluster as RelayCluster;
1718
use Symfony\Component\Lock\Exception\InvalidTtlException;
1819
use Symfony\Component\Lock\Exception\LockConflictedException;
1920
use Symfony\Component\Lock\Exception\LockStorageException;
@@ -38,7 +39,7 @@ class RedisStore implements SharedLockStoreInterface
3839
* @param float $initialTtl The expiration delay of locks in seconds
3940
*/
4041
public function __construct(
41-
private \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
42+
private \Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
4243
private float $initialTtl = 300.0,
4344
) {
4445
if ($initialTtl <= 0) {
@@ -231,14 +232,14 @@ private function evaluate(string $script, string $resource, array $args): mixed
231232
{
232233
$scriptSha = sha1($script);
233234

234-
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
235+
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof RelayCluster || $this->redis instanceof \RedisCluster) {
235236
$this->redis->clearLastError();
236237

237238
$result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1);
238239
if (null !== ($err = $this->redis->getLastError()) && str_starts_with($err, self::NO_SCRIPT_ERROR_MESSAGE_PREFIX)) {
239240
$this->redis->clearLastError();
240241

241-
if ($this->redis instanceof \RedisCluster) {
242+
if ($this->redis instanceof \RedisCluster || $this->redis instanceof RelayCluster) {
242243
foreach ($this->redis->_masters() as $master) {
243244
$this->redis->script($master, 'LOAD', $script);
244245
}

Tests/Store/AbstractRedisStoreTestCase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Lock\Tests\Store;
1313

1414
use Relay\Relay;
15+
use Relay\Cluster as RelayCluster;
1516
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1617
use Symfony\Component\Lock\Exception\LockConflictedException;
1718
use Symfony\Component\Lock\Key;
@@ -30,7 +31,7 @@ protected function getClockDelay(): int
3031
return 250000;
3132
}
3233

33-
abstract protected function getRedisConnection(): \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface;
34+
abstract protected function getRedisConnection(): \Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predis\ClientInterface;
3435

3536
public function getStore(): PersistingStoreInterface
3637
{
@@ -55,7 +56,7 @@ public function testBackwardCompatibility()
5556

5657
class Symfony51Store
5758
{
58-
private \Redis|Relay|\RedisCluster|\RedisArray|\Predis\ClientInterface $redis;
59+
private \Redis|Relay|RelayCluster|\RedisCluster|\RedisArray|\Predis\ClientInterface $redis;
5960

6061
public function __construct($redis)
6162
{
@@ -85,7 +86,7 @@ public function exists(Key $key)
8586

8687
private function evaluate(string $script, string $resource, array $args)
8788
{
88-
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
89+
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof RelayCluster || $this->redis instanceof \RedisCluster) {
8990
return $this->redis->eval($script, array_merge([$resource], $args), 1);
9091
}
9192

Tests/Store/RelayClusterStoreTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 Store;
13+
14+
use Relay\Cluster as RelayCluster;
15+
use Symfony\Component\Lock\Tests\Store\AbstractRedisStoreTestCase;
16+
17+
/**
18+
* @requires extension relay
19+
*
20+
* @group integration
21+
*/
22+
class RelayClusterStoreTest extends AbstractRedisStoreTestCase
23+
{
24+
protected function setUp(): void
25+
{
26+
$relayCluster = $this->getRedisConnection();
27+
28+
foreach ($relayCluster->_masters() as $hostAndPort) {
29+
$relayCluster->flushdb($hostAndPort);
30+
}
31+
}
32+
33+
public static function setUpBeforeClass(): void
34+
{
35+
if (!class_exists(RelayCluster::class)) {
36+
self::markTestSkipped('The Relay\Cluster class is required.');
37+
}
38+
39+
if (getenv('REDIS_CLUSTER_HOSTS') === false) {
40+
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
41+
}
42+
}
43+
44+
protected function getRedisConnection(): RelayCluster
45+
{
46+
return new RelayCluster('', explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
47+
}
48+
}

0 commit comments

Comments
 (0)