Skip to content

[Cache] Add \Relay\Cluster support #59857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RedisAdapter extends AbstractAdapter
{
use RedisTrait;

public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|\Relay\Relay $redis, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|\Relay\Relay|\Relay\Cluster $redis, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->init($redis, $namespace, $defaultLifetime, $marshaller);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
private string $redisEvictionPolicy;

public function __construct(
\Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
\Redis|Relay|\Relay\Cluster|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
private string $namespace = '',
int $defaultLifetime = 0,
?MarshallerInterface $marshaller = null,
Expand All @@ -69,7 +69,7 @@ public function __construct(
throw new InvalidArgumentException(\sprintf('Unsupported Predis cluster connection: only "%s" is, "%s" given.', PredisCluster::class, get_debug_type($redis->getConnection())));
}

$isRelay = $redis instanceof Relay;
$isRelay = $redis instanceof Relay || $redis instanceof \Relay\Cluster;
if ($isRelay || \defined('Redis::OPT_COMPRESSION') && \in_array($redis::class, [\Redis::class, \RedisArray::class, \RedisCluster::class], true)) {
$compression = $redis->getOption($isRelay ? Relay::OPT_COMPRESSION : \Redis::OPT_COMPRESSION);

Expand Down Expand Up @@ -225,7 +225,7 @@ protected function doInvalidate(array $tagIds): bool
$results = $this->pipeline(function () use ($tagIds, $lua) {
if ($this->redis instanceof \Predis\ClientInterface) {
$prefix = $this->redis->getOptions()->prefix ? $this->redis->getOptions()->prefix->getPrefix() : '';
} elseif (\is_array($prefix = $this->redis->getOption($this->redis instanceof Relay ? Relay::OPT_PREFIX : \Redis::OPT_PREFIX) ?? '')) {
} elseif (\is_array($prefix = $this->redis->getOption(($this->redis instanceof Relay || $this->redis instanceof \Relay\Cluster) ? Relay::OPT_PREFIX : \Redis::OPT_PREFIX) ?? '')) {
$prefix = current($prefix);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add support for `\Relay\Cluster` in `RedisAdapter`

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Cache\CacheItemPoolInterface;
use Relay\Relay;
use Relay\Cluster as RelayCluster;
use Symfony\Component\Cache\Adapter\RedisAdapter;

abstract class AbstractRedisAdapterTestCase extends AdapterTestCase
Expand All @@ -23,7 +24,7 @@ abstract class AbstractRedisAdapterTestCase extends AdapterTestCase
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
];

protected static \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis;
protected static \Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis;

public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
use Symfony\Component\Cache\Traits\RelayClusterProxy;

/**
* @requires extension relay
*
* @group integration
*/
class RedisTagAwareRelayClusterAdapterTest extends RelayClusterAdapterTest
{
use TagAwareTestTrait;

protected function setUp(): void
{
parent::setUp();
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
{
$this->assertInstanceOf(RelayClusterProxy::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

return $adapter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Relay\Relay;
use Relay\Cluster as RelayCluster;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Traits\RelayClusterProxy;

/**
* @requires extension relay
*
* @group integration
*/
class RelayClusterAdapterTest extends AbstractRedisAdapterTestCase
{
public static function setUpBeforeClass(): void
{
if (!class_exists(RelayCluster::class)) {
self::markTestSkipped('The Relay\Cluster class is required.');
}
if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
}

self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['lazy' => true, 'redis_cluster' => true, 'class' => RelayCluster::class]);
self::$redis->setOption(Relay::OPT_PREFIX, 'prefix_');
}

public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
{
$this->assertInstanceOf(RelayClusterProxy::class, self::$redis);
$adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

return $adapter;
}

/**
* @dataProvider provideFailedCreateConnection
*/
public function testFailedCreateConnection(string $dsn)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Relay cluster connection failed:');
RedisAdapter::createConnection($dsn);
}

public static function provideFailedCreateConnection(): array
{
return [
['redis://localhost:1234?redis_cluster=1&class=Relay\Cluster'],
['redis://foo@localhost?redis_cluster=1&class=Relay\Cluster'],
['redis://localhost/123?redis_cluster=1&class=Relay\Cluster'],
];
}
}
50 changes: 50 additions & 0 deletions src/Symfony/Component/Cache/Tests/Traits/RedisProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use PHPUnit\Framework\TestCase;
use Relay\Relay;
use Relay\Cluster as RelayCluster;
use Symfony\Component\Cache\Traits\RedisProxyTrait;
use Symfony\Component\Cache\Traits\RelayClusterProxy;
use Symfony\Component\Cache\Traits\RelayProxy;
use Symfony\Component\VarExporter\LazyProxyTrait;
use Symfony\Component\VarExporter\ProxyHelper;
Expand Down Expand Up @@ -121,4 +123,52 @@ public function testRelayProxy()

$this->assertEquals($expectedProxy, $proxy);
}


/**
* @requires extension relay
*/
public function testRelayClusterProxy()
{
$proxy = file_get_contents(\dirname(__DIR__, 2).'/Traits/RelayClusterProxy.php');
$proxy = substr($proxy, 0, 2 + strpos($proxy, '}'));
$expectedProxy = $proxy;
$methods = [];
$expectedMethods = [];

foreach ((new \ReflectionClass(RelayClusterProxy::class))->getMethods() as $method) {
if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name) || $method->isStatic()) {
continue;
}

$return = '__construct' === $method->name || $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
$expectedMethods[$method->name] = "\n ".ProxyHelper::exportSignature($method, false, $args)."\n".<<<EOPHP
{
{$return}\$this->initializeLazyObject()->{$method->name}({$args});
}

EOPHP;
}

foreach ((new \ReflectionClass(RelayCluster::class))->getMethods() as $method) {
if ('reset' === $method->name || method_exists(RedisProxyTrait::class, $method->name) || $method->isStatic()) {
continue;
}
$return = '__construct' === $method->name || $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
$methods[$method->name] = "\n ".ProxyHelper::exportSignature($method, false, $args)."\n".<<<EOPHP
{
{$return}\$this->initializeLazyObject()->{$method->name}({$args});
}

EOPHP;
}

uksort($methods, 'strnatcmp');
$proxy .= implode('', $methods)."}\n";

uksort($expectedMethods, 'strnatcmp');
$expectedProxy .= implode('', $expectedMethods)."}\n";

$this->assertEquals($expectedProxy, $proxy);
}
}
Loading
Loading