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

Skip to content

Commit 2458735

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 2458735

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-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` array to be passed in the Redis DSN or set in `RedisAdapter` connection options
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/PredisAdapterTest.php

Lines changed: 8 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 Predis\Connection\StreamConnection;
1516
use Symfony\Component\Cache\Adapter\RedisAdapter;
1617

@@ -51,5 +52,12 @@ public function testCreateConnection()
5152
'password' => null,
5253
);
5354
$this->assertSame($params, $connection->getParameters()->toArray());
55+
56+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', array('class' => \Predis\Client::class, 'timeout' => 3));
57+
$this->assertInstanceOf(\Predis\Client::class, $redis);
58+
59+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?predis_options[cluster]=redis');
60+
$this->assertInstanceOf(\Predis\Client::class, $redis);
61+
$this->assertInstanceOf(RedisCluster::class, $redis->getOptions()->cluster);
5462
}
5563
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ public function provideFailedCreateConnection()
7272
);
7373
}
7474

75+
/**
76+
* @dataProvider provideInvalidDSNCreateConnection
77+
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
78+
* @expectedExceptionMessage Invalid connection option: "predis_options" provided but provided class "\SomeClass" is not an instance of \Predis\Client
79+
*/
80+
public function testInvalidDSNFailedConnection($dsn)
81+
{
82+
RedisAdapter::createConnection($dsn);
83+
}
84+
85+
public function provideInvalidDSNCreateConnection()
86+
{
87+
return array(
88+
array('redis://localhost?predis_options[cluster]=redis&class=\SomeClass'),
89+
);
90+
}
91+
7592
/**
7693
* @dataProvider provideInvalidCreateConnection
7794
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException

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

Lines changed: 18 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,10 @@ 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+
$redis = new $class((new Factory())->create($params), $predisOptions);
171187
} elseif (class_exists($class, false)) {
172188
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
173189
} else {

0 commit comments

Comments
 (0)