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 45c26a6

Browse filesBrowse files
committed
[Cache] Add support for \Relay\Cluster in Semaphore and Lock components
1 parent 9557876 commit 45c26a6
Copy full SHA for 45c26a6

File tree

8 files changed

+138
-10
lines changed
Filter options

8 files changed

+138
-10
lines changed

‎src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
6060
private string $redisEvictionPolicy;
6161

6262
public function __construct(
63-
\Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
63+
\Redis|Relay|\Relay\Cluster|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
6464
private string $namespace = '',
6565
int $defaultLifetime = 0,
6666
?MarshallerInterface $marshaller = null,
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
16+
use Symfony\Component\Cache\Traits\RelayClusterProxy;
17+
18+
/**
19+
* @requires extension relay
20+
*
21+
* @group integration
22+
*/
23+
class RedisTagAwareRelayClusterAdapterTest extends RelayClusterAdapterTest
24+
{
25+
use TagAwareTestTrait;
26+
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
31+
}
32+
33+
public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
34+
{
35+
$this->assertInstanceOf(RelayClusterProxy::class, self::$redis);
36+
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
37+
38+
return $adapter;
39+
}
40+
}

‎src/Symfony/Component/Lock/Store/RedisStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/RedisStore.php
+4-3Lines 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
}

‎src/Symfony/Component/Lock/Tests/Store/AbstractRedisStoreTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Tests/Store/AbstractRedisStoreTestCase.php
+4-3Lines 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

+48Lines 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+
}

‎src/Symfony/Component/Semaphore/Store/RedisStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Semaphore/Store/RedisStore.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Semaphore\Store;
1313

1414
use Relay\Relay;
15+
use Relay\Cluster as RelayCluster;
1516
use Symfony\Component\Semaphore\Exception\InvalidArgumentException;
1617
use Symfony\Component\Semaphore\Exception\SemaphoreAcquiringException;
1718
use Symfony\Component\Semaphore\Exception\SemaphoreExpiredException;
@@ -27,7 +28,7 @@
2728
class RedisStore implements PersistingStoreInterface
2829
{
2930
public function __construct(
30-
private \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
31+
private \Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
3132
) {
3233
}
3334

@@ -158,7 +159,7 @@ public function exists(Key $key): bool
158159

159160
private function evaluate(string $script, string $resource, array $args): mixed
160161
{
161-
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
162+
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof RelayCluster || $this->redis instanceof \RedisCluster) {
162163
return $this->redis->eval($script, array_merge([$resource], $args), 1);
163164
}
164165

‎src/Symfony/Component/Semaphore/Tests/Store/AbstractRedisStoreTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Semaphore/Tests/Store/AbstractRedisStoreTestCase.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Semaphore\Tests\Store;
1313

1414
use Relay\Relay;
15+
use Relay\Cluster as RelayCluster;
1516
use Symfony\Component\Semaphore\PersistingStoreInterface;
1617
use Symfony\Component\Semaphore\Store\RedisStore;
1718

@@ -20,7 +21,7 @@
2021
*/
2122
abstract class AbstractRedisStoreTestCase extends AbstractStoreTestCase
2223
{
23-
abstract protected function getRedisConnection(): \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface;
24+
abstract protected function getRedisConnection(): \Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predis\ClientInterface;
2425

2526
public function getStore(): PersistingStoreInterface
2627
{
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Semaphore\Tests\Store;
13+
14+
use Relay\Cluster as RelayCluster;
15+
16+
/**
17+
* @requires extension relay
18+
*/
19+
class RelayClusterStoreTest extends AbstractRedisStoreTestCase
20+
{
21+
public static function setUpBeforeClass(): void
22+
{
23+
if (!class_exists(RelayCluster::class)) {
24+
self::markTestSkipped('The Relay\Cluster class is required.');
25+
}
26+
27+
if (getenv('REDIS_CLUSTER_HOSTS') === false) {
28+
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
29+
}
30+
}
31+
32+
protected function getRedisConnection(): RelayCluster
33+
{
34+
return new RelayCluster('', explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
35+
}
36+
}

0 commit comments

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