8000 [Cache] add RedisClusterProxy to create lazy connections to Redis clusters by nicolas-grekas · Pull Request #28691 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] add RedisClusterProxy to create lazy connections to Redis clusters #28691

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 1 commit into from
Oct 2, 2018
Merged
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
63 changes: 63 additions & 0 deletions src/Symfony/Component/Cache/Traits/RedisClusterProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Traits;

/**
* @author Alessandro Chitolina <alekitto@gmail.com>
*
* @internal
*/
class RedisClusterProxy
{
private $redis;
private $initializer;

public function __construct(\Closure $initializer)
{
$this->initializer = $initializer;
}

public function __call($method, array $args)
{
$this->redis ?: $this->redis = $this->initializer->__invoke();

return $this->redis->{$method}(...$args);
}

public function hscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
{
$this->redis ?: $this->redis = $this->initializer->__invoke();

return $this->redis->hscan($strKey, $iIterator, $strPattern, $iCount);
}

public function scan(&$iIterator, $strPattern = null, $iCount = null)
{
$this->redis ?: $this->redis = $this->initializer->__invoke();

return $this->redis->scan($iIterator, $strPattern, $iCount);
}

public function sscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
{
$this->redis ?: $this->redis = $this->initializer->__invoke();

return $this->redis->sscan($strKey, $iIterator, $strPattern, $iCount);
}

public function zscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
{
$this->redis ?: $this->redis = $this->initializer->__invoke();

return $this->redis->zscan($strKey, $iIterator, $strPattern, $iCount);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private function init($redisClient, $namespace, $defaultLifetime, ?MarshallerInt
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
}
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy) {
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
throw new InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)));
}
$this->redis = $redisClient;
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function doClear($namespace)
foreach ($this->redis->_hosts() as $host) {
$hosts[] = $this->redis->_instance($host);
}
} elseif ($this->redis instanceof \RedisCluster) {
} elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
$hosts = array();
foreach ($this->redis->_masters() as $host) {
$hosts[] = $h = new \Redis();
Expand Down Expand Up @@ -330,7 +330,7 @@ private function pipeline(\Closure $generator)
{
$ids = array();

if ($this->redis instanceof \RedisCluster || ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof RedisCluster)) {
if ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster || ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof RedisCluster)) {
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

use Predis\Response\ErrorInterface;
use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;

/**
Expand Down Expand Up @@ -45,7 +46,8 @@ public function __construct($redis, array $options = array())
!$redis instanceof \RedisArray &&
!$redis instanceof \RedisCluster &&
!$redis instanceof \Predis\Client &&
!$redis instanceof RedisProxy
!$redis instanceof RedisProxy &&
!$redis instanceof RedisClusterProxy
) {
throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
}
Expand Down
8000
8 changes: 7 additions & 1 deletion src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Lock\Store;

use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\LockConflictedException;
Expand Down Expand Up @@ -130,7 +131,12 @@ public function exists(Key $key)
*/
private function evaluate(string $script, string $resource, array $args)
{
if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster || $this->redis instanceof RedisProxy) {
if (
$this->redis instanceof \Redis ||
$this->redis instanceof \RedisCluster ||
$this->redis instanceof RedisProxy ||
$this->redis instanceof RedisClusterProxy
) {
return $this->redis->eval($script, array_merge(array($resource), $args), 1);
}

Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Lock\Store;

use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Exception\InvalidArgumentException;

Expand All @@ -28,7 +29,14 @@ class StoreFactory
*/
public static function createStore($connection)
{
if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client || $connection instanceof RedisProxy) {
if (
$connection instanceof \Redis ||
$connection instanceof \RedisArray ||
$connection instanceof \RedisCluster ||
$connection instanceof \Predis\Client ||
$connection instanceof RedisProxy ||
$connection instanceof RedisClusterProxy
) {
return new RedisStore($connection);
}
if ($connection instanceof \Memcached) {
Expand Down
0