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

Skip to content

Commit 2fe2f53

Browse files
committed
[Cache] Add ability to configure predis client.
When the predis driver is created, there is no current way to configure the predis client options (such as setting the cluster mode to redis). This merge allows you to add a predis_options parameter to the dsn to allow configuration of options.
1 parent 00e8b49 commit 2fe2f53

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* added sub-second expiry accuracy for backends that support it
1010
* added support for phpredis 4 `compression` and `tcp_keepalive` options
1111
* added automatic table creation when using Doctrine DBAL with PDO-based backends
12+
* added support for a `predis_options` query parameter array in the redis dsn.
1213
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool
1314
* deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead
1415
* deprecated the `AbstractAdapter::createSystemCache()` method

src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function testCreateConnection()
5151

5252
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
5353
$this->assertEquals(5, $redis->getReadTimeout());
54+
55+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?predis_options["cluster"]=redis');
56+
$this->assertInstanceOf(\Predis\Client::class, $redis);
5457
}
5558

5659
/**
@@ -72,6 +75,23 @@ public function provideFailedCreateConnection()
7275
);
7376
}
7477

78+
/**
79+
* @dataProvider provideInvalidDSNCreateConnection
80+
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
81+
* @expectedExceptionMessage Cannot find the "redis" extension and expected an instance of Predis\Client, instead got: \SomeClass
82+
*/
83+
public function testInvalidDSNFailedConnection($dsn)
84+
{
85+
RedisAdapter::createConnection($dsn);
86+
}
87+
88+
public function provideInvalidDSNCreateConnection()
89+
{
90+
return array(
91+
array('redis://localhost?predis_options[cluster]=redis&class=\SomeClass'),
92+
);
93+
}
94+
7595
/**
7696
* @dataProvider provideInvalidCreateConnection
7797
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ public static function createConnection($dsn, array $options = array())
118118
$params += $options + self::$defaultConnectionOptions;
119119
if (null === $params['class'] && !\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
120120
throw new CacheException(sprintf('Cannot find the "redis" extension, and "predis/predis" is not installed: %s', $dsn));
121+
} elseif (isset($params['predis_options']) && !class_exists(\Predis\Client::class)) {
122+
throw new CacheException(sprintf('DSN contains "predis_options" and "predis/predis" is not installed: %s', $dsn));
123+
} elseif (isset($params['predis_options']) || !\extension_loaded('redis')) {
124+
if (null !== $params['class'] && !is_a($params['class'], \Predis\Client::class, true)) {
125+
throw new InvalidArgumentException(sprintf('Cannot find the "redis" extension and expected an instance of %s, instead got: %s', \Predis\Client::class, $params['class']));
126+
}
127+
128+
$class = null === $params['class'] ? \Predis\Client::class : $params['class'];
129+
} else {
130+
$class = null === $params['class'] ? \Redis::class : $params['class'];
121131
}
122-
$class = null === $params['class'] ? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];
123132

124133
if (is_a($class, \Redis::class, true)) {
125134
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
@@ -167,7 +176,10 @@ public static function createConnection($dsn, array $options = array())
167176
$params['scheme'] = $scheme;
168177
$params['database'] = $params['dbindex'] ?: null;
169178
$params['password'] = $auth;
170-
$redis = new $class((new Factory())->create($params));
179+
180+
$predisOptions = false !== isset($params['predis_options']) && true === \is_array($params['predis_options']) ? $params['predis_options'] : array();
181+
182+
$redis = new $class((new Factory())->create($params), $predisOptions);
171183
} elseif (class_exists($class, false)) {
172184
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
173185
} else {

0 commit comments

Comments
 (0)