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

Skip to content

Commit 1ac9b3e

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 1ac9b3e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14+
use Predis\Connection\Aggregate\RedisCluster;
1415
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1516
use Symfony\Component\Cache\Adapter\RedisAdapter;
1617
use Symfony\Component\Cache\Traits\RedisProxy;
@@ -51,6 +52,11 @@ public function testCreateConnection()
5152

5253
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
5354
$this->assertEquals(5, $redis->getReadTimeout());
55+
56+
/** @var \Predis\Client $redis */
57+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?predis_options[cluster]=redis');
58+
$this->assertInstanceOf(\Predis\Client::class, $redis);
59+
$this->assertInstanceOf(RedisCluster::class, $redis->getOptions()->cluster);
5460
}
5561

5662
/**
@@ -72,6 +78,23 @@ public function provideFailedCreateConnection()
7278
);
7379
}
7480

81+
/**
82+
* @dataProvider provideInvalidDSNCreateConnection
83+
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
84+
* @expectedExceptionMessage Invalid connection option: "predis_options" provided but provided class "\SomeClass" is not an instance of \Predis\Client
85+
*/
86+
public function testInvalidDSNFailedConnection($dsn)
87+
{
88+
RedisAdapter::createConnection($dsn);
89+
}
90+
91+
public function provideInvalidDSNCreateConnection()
92+
{
93+
return array(
94+
array('redis://localhost?predis_options[cluster]=redis&class=\SomeClass'),
95+
);
96+
}
97+
7598
/**
7699
* @dataProvider provideInvalidCreateConnection
77100
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,20 @@ public static function createConnection($dsn, array $options = array())
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));
121121
}
122-
$class = null === $params['class'] ? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];
122+
123+
if (isset($params['predis_options']) && !class_exists(\Predis\Client::class)) {
124+
throw new InvalidArgumentException('Invalid connection option: "predis_options" provided but "predis/predis" is not installed.');
125+
}
126+
127+
if (isset($params['predis_options']) && null !== $params['class'] && !is_a($params['class'], \Predis\Client::class, true)) {
128+
throw new InvalidArgumentException(sprintf('Invalid connection option: "predis_options" provided but provided class "%s" is not an instance of \Predis\Client', $params['class']));
129+
}
130+
131+
if (isset($params['predis_options'])) {
132+
$class = $params['class'] ?? \Predis\Client::class;
133+
} else {
134+
$class = $params['class'] ?? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class);
135+
}
123136

124137
if (is_a($class, \Redis::class, true)) {
125138
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
@@ -167,7 +180,14 @@ public static function createConnection($dsn, array $options = array())
167180
$params['scheme'] = $scheme;
168181
$params['database'] = $params['dbindex'] ?: null;
169182
$params['password'] = $auth;
170-
$redis = new $class((new Factory())->create($params));
183+
184+
$predisOptions = $params['predis_options'] ?? array();
185+
186+
if (!\is_array($predisOptions)) {
187+
throw new InvalidArgumentException('Invalid connection option: "predis_options" provided but is not of the type "array".');
188+
}
189+
190+
$redis = new $class((new Factory())->create($params), $predisOptions);
171191
} elseif (class_exists($class, false)) {
172192
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
173193
} else {

0 commit comments

Comments
 (0)