8000 [Cache] Add Redis Relay support by ostrolucky · Pull Request #48930 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Add Redis Relay support #48930

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

Merged
merged 6 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Lock] Accept Relay connection
  • Loading branch information
ostrolucky committed Jan 24, 2023
commit a4c2ca83bc47a6811b501fc203e31dd4a83d16ef
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Create migration for lock table when DoctrineDbalStore is used
* Add support for Relay PHP extension for Redis

6.0
---
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Lock\Store;

use Predis\Response\ServerException;
use Relay\Relay;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Exception\LockStorageException;
Expand All @@ -35,7 +36,7 @@ class RedisStore implements SharedLockStoreInterface
* @param float $initialTtl The expiration delay of locks in seconds
*/
public function __construct(
private \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
private \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
private float $initialTtl = 300.0,
) {
if ($initialTtl <= 0) {
Expand Down Expand Up @@ -226,7 +227,7 @@ public function exists(Key $key): bool

private function evaluate(string $script, string $resource, array $args): mixed
{
if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster) {
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
$this->redis->clearLastError();
$result = $this->redis->eval($script, array_merge([$resource], $args), 1);
if (null !== $err = $this->redis->getLastError()) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Lock\Store;

use Doctrine\DBAL\Connection;
use Relay\Relay;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\PersistingStoreInterface;
Expand All @@ -27,6 +28,7 @@ public static function createStore(#[\SensitiveParameter] object|string $connect
{
switch (true) {
case $connection instanceof \Redis:
case $connection instanceof Relay:
case $connection instanceof \RedisArray:
case $connection instanceof \RedisCluster:
case $connection instanceof \Predis\ClientInterface:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Lock\Tests\Store;

use Relay\Relay;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
Expand All @@ -29,7 +30,7 @@ protected function getClockDelay()
return 250000;
}

abstract protected function getRedisConnection(): \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface;
abstract protected function getRedisConnection(): \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface;

public function getStore(): PersistingStoreInterface
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public function exists(Key $key)

private function evaluate(string $script, string $resource, array $args)
{
if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster) {
if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
return $this->redis->eval($script, array_merge([$resource], $args), 1);
}

Expand All @@ -97,7 +98,7 @@ private function evaluate(string $script, string $resource, array $args)
return $this->redis->eval(...array_merge([$script, 1, $resource], $args));
}

throw new InvalidArgumentException(sprintf('"%s()" expects being initialized with a Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($this->redis)));
throw new InvalidArgumentException(sprintf('"%s()" expects being initialized with a Redis, Relay, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($this->redis)));
}

private function getUniqueToken(Key $key): string
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/RelayStoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 Store;

use PHPUnit\Framework\SkippedTestSuiteError;
use Relay\Relay;
use Symfony\Component\Lock\Tests\Store\AbstractRedisStoreTest;
use Symfony\Component\Lock\Tests\Store\SharedLockStoreTestTrait;

/**
* @requires extension relay
*
* @group integration
*/
class RelayStoreTest extends AbstractRedisStoreTest
{
use SharedLockStoreTestTrait;

public static function setUpBeforeClass(): void
{
try {
new Relay(...explode(':', getenv('REDIS_HOST')));
} catch (\Relay\Exception $e) {
throw new SkippedTestSuiteError($e->getMessage());
}
}

protected function getRedisConnection(): Relay
{
return new Relay(...explode(':', getenv('REDIS_HOST')));
}
}
0