Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38655bd

Browse files
minor #28691 [Cache] add RedisClusterProxy to create lazy connections to Redis clusters (alekitto)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Cache] add RedisClusterProxy to create lazy connections to Redis clusters | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Code provided by @alekitto in #28300 I'm working on an alternative to this PR, but these bits are required. Commits ------- 239a022 [Cache] add RedisClusterProxy to create lazy connections to Redis clusters
2 parents 944b06d + 239a022 commit 38655bd

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Traits;
13+
14+
/**
15+
* @author Alessandro Chitolina <[email protected]>
16+
*
17+
* @internal
18+
*/
19+
class RedisClusterProxy
20+
{
21+
private $redis;
22+
private $initializer;
23+
24+
public function __construct(\Closure $initializer)
25+
{
26+
$this->initializer = $initializer;
27+
}
28+
29+
public function __call($method, array $args)
30+
{
31+
$this->redis ?: $this->redis = $this->initializer->__invoke();
32+
33+
return $this->redis->{$method}(...$args);
34+
}
35+
36+
public function hscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
37+
{
38+
$this->redis ?: $this->redis = $this->initializer->__invoke();
39+
40+
return $this->redis->hscan($strKey, $iIterator, $strPattern, $iCount);
41+
}
42+
43+
public function scan(&$iIterator, $strPattern = null, $iCount = null)
44+
{
45+
$this->redis ?: $this->redis = $this->initializer->__invoke();
46+
47+
return $this->redis->scan($iIterator, $strPattern, $iCount);
48+
}
49+
50+
public function sscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
51+
{
52+
$this->redis ?: $this->redis = $this->initializer->__invoke();
53+
54+
return $this->redis->sscan($strKey, $iIterator, $strPattern, $iCount);
55+
}
56+
57+
public function zscan($strKey, &$iIterator, $strPattern = null, $iCount = null)
58+
{
59+
$this->redis ?: $this->redis = $this->initializer->__invoke();
60+
61+
return $this->redis->zscan($strKey, $iIterator, $strPattern, $iCount);
62+
}
63+
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function init($redisClient, $namespace, $defaultLifetime, ?MarshallerInt
5252
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
5353
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
5454
}
55-
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy) {
55+
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
5656
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)));
5757
}
5858
$this->redis = $redisClient;
@@ -237,7 +237,7 @@ protected function doClear($namespace)
237237
foreach ($this->redis->_hosts() as $host) {
238238
$hosts[] = $this->redis->_instance($host);
239239
}
240-
} elseif ($this->redis instanceof \RedisCluster) {
240+
} elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
241241
$hosts = array();
242242
foreach ($this->redis->_masters() as $host) {
243243
$hosts[] = $h = new \Redis();
@@ -330,7 +330,7 @@ private function pipeline(\Closure $generator)
330330
{
331331
$ids = array();
332332

333-
if ($this->redis instanceof \RedisCluster || ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof RedisCluster)) {
333+
if ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster || ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof RedisCluster)) {
334334
// phpredis & predis don't support pipelining with RedisCluster
335335
// see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
336336
// see https://github.com/nrk/predis/issues/267#issuecomment-123781423

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
1313

1414
use Predis\Response\ErrorInterface;
15+
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1516
use Symfony\Component\Cache\Traits\RedisProxy;
1617

1718
/**
@@ -45,7 +46,8 @@ public function __construct($redis, array $options = array())
4546
!$redis instanceof \RedisArray &&
4647
!$redis instanceof \RedisCluster &&
4748
!$redis instanceof \Predis\Client &&
48-
!$redis instanceof RedisProxy
49+
!$redis instanceof RedisProxy &&
50+
!$redis instanceof RedisClusterProxy
4951
) {
5052
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)));
5153
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1415
use Symfony\Component\Cache\Traits\RedisProxy;
1516
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1617
use Symfony\Component\Lock\Exception\LockConflictedException;
@@ -130,7 +131,12 @@ public function exists(Key $key)
130131
*/
131132
private function evaluate(string $script, string $resource, array $args)
132133
{
133-
if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster || $this->redis instanceof RedisProxy) {
134+
if (
135+
$this->redis instanceof \Redis ||
136+
$this->redis instanceof \RedisCluster ||
137+
$this->redis instanceof RedisProxy ||
138+
$this->redis instanceof RedisClusterProxy
139+
) {
134140
return $this->redis->eval($script, array_merge(array($resource), $args), 1);
135141
}
136142

src/Symfony/Component/Lock/Store/StoreFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1415
use Symfony\Component\Cache\Traits\RedisProxy;
1516
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1617

@@ -28,7 +29,14 @@ class StoreFactory
2829
*/
2930
public static function createStore($connection)
3031
{
31-
if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client || $connection instanceof RedisProxy) {
32+
if (
33+
$connection instanceof \Redis ||
34+
$connection instanceof \RedisArray ||
35+
$connection instanceof \RedisCluster ||
36+
$connection instanceof \Predis\Client ||
37+
$connection instanceof RedisProxy ||
38+
$connection instanceof RedisClusterProxy
39+
) {
3240
return new RedisStore($connection);
3341
}
3442
if ($connection instanceof \Memcached) {

0 commit comments

Comments
 (0)