8000 [Cache] Add ability to configure predis client. by jralph · Pull Request #28175 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Add ability to configure predis client. #28175

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* added sub-second expiry accuracy for backends that support it
* added support for phpredis 4 `compression` and `tcp_keepalive` options
* added automatic table creation when using Doctrine DBAL with PDO-based backends
* added support for a `predis_options` array to be passed in the Redis DSN or set in `RedisAdapter` connection options
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool
* deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead
* deprecated the `AbstractAdapter::createSystemCache()` method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Cache\Tests\Adapter;

use Predis\Connection\Aggregate\RedisCluster;
use Predis\Connection\StreamConnection;
use Symfony\Component\Cache\Adapter\RedisAdapter;

Expand Down Expand Up @@ -51,5 +52,12 @@ public function testCreateConnection()
'password' => null,
);
$this->assertSame($params, $connection->getParameters()->toArray());

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', array('class' => \Predis\Client::class, 'timeout' => 3));
$this->assertInstanceOf(\Predis\Client::class, $redis);

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?predis_options[cluster]=redis');
$this->assertInstanceOf(\Predis\Client::class, $redis);
$this->assertInstanceOf(RedisCluster::class, $redis->getOptions()->cluster);
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public function provideFailedCreateConnection()
);
}

/**
* @dataProvider provideInvalidDSNCreateConnection
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid connection option: "predis_options" provided but provided class "\SomeClass" is not an instance of \Predis\Client
*/
public function testInvalidDSNFailedConnection($dsn)
{
RedisAdapter::createConnection($dsn);
}

public function provideInvalidDSNCreateConnection()
{
return array(
array('redis://localhost?predis_options[cluster]=redis&class=\SomeClass'),
);
}

/**
* @dataProvider provideInvalidCreateConnection
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,20 @@ public static function createConnection($dsn, array $options = array())
if (null === $params['class'] && !\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
throw new CacheException(sprintf('Cannot find the "redis" extension, and "predis/predis" is not installed: %s', $dsn));
}
$class = null === $params['class'] ? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];

if (isset($params['predis_options']) && !class_exists(\Predis\Client::class)) {
throw new InvalidArgumentException('Invalid connection option: "predis_options" provided but "predis/predis" is not installed.');
}

if (isset($params['predis_options']) && null !== $params['class'] && !is_a($params['class'], \Predis\Client::class, true)) {
throw new InvalidArgumentException(sprintf('Invalid connection option: "predis_options" provided but provided class "%s" is not an instance of \Predis\Client', $params['class']));
}

if (isset($params['predis_options'])) {
$class = $params['class'] ?? \Predis\Client::class;
} else {
$class = $params['class'] ?? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class);
}

if (is_a($class, \Redis::class, true)) {
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
Expand Down Expand Up @@ -167,7 +180,10 @@ public static function createConnection($dsn, array $options = array())
$params['scheme'] = $scheme;
$params['database'] = $params['dbindex'] ?: null;
$params['password'] = $auth;
$redis = new $class((new Factory())->create($params));

$predisOptions = $params['predis_options'] ?? array();

$redis = new $class((new Factory())->create($params), $predisOptions);
} elseif (class_exists($class, false)) {
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
} else {
Expand Down
0