Navigation Menu

Skip to content

Commit

Permalink
feature #30605 [Cache] added DSN support for rediss in AbstractAdapte…
Browse files Browse the repository at this point in the history
…r and RedisTrait (alex-vasilchenko-md)

This PR was squashed before being merged into the 4.3-dev branch (closes #30605).

Discussion
----------

[Cache] added DSN support for rediss in AbstractAdapter and RedisTrait

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30573
| License       | MIT

A fix for this issue: #30573
Support for "rediss:" in DSN added.

Commits
-------

7e2852d [Cache] added DSN support for rediss in AbstractAdapter and RedisTrait
  • Loading branch information
fabpot committed Mar 20, 2019
2 parents 2278d4c + 7e2852d commit 81bf2ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Expand Up @@ -132,7 +132,7 @@ public static function createConnection($dsn, array $options = [])
if (!\is_string($dsn)) {
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, \gettype($dsn)));
}
if (0 === strpos($dsn, 'redis:')) {
if (0 === strpos($dsn, 'redis:') || 0 === strpos($dsn, 'rediss:')) {
return RedisAdapter::createConnection($dsn, $options);
}
if (0 === strpos($dsn, 'memcached:')) {
Expand Down
25 changes: 18 additions & 7 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Expand Up @@ -31,30 +31,33 @@ public function createCachePool($defaultLifetime = 0)
return $adapter;
}

public function testCreateConnection()
/**
* @dataProvider provideValidSchemes
*/
public function testCreateConnection($dsnScheme)
{
$redis = RedisAdapter::createConnection('redis:?host[h1]&host[h2]&host[/foo:]');
$redis = RedisAdapter::createConnection($dsnScheme.':?host[h1]&host[h2]&host[/foo:]');
$this->assertInstanceOf(\RedisArray::class, $redis);
$this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts());
@$redis = null; // some versions of phpredis connect on destruct, let's silence the warning

$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection('redis://'.$redisHost);
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost);
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'/2');
$this->assertSame(2, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['timeout' => 3]);
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['timeout' => 3]);
$this->assertEquals(3, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'?timeout=4');
$this->assertEquals(4, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['read_timeout' => 5]);
$this->assertEquals(5, $redis->getReadTimeout());
}

Expand Down Expand Up @@ -87,6 +90,14 @@ public function testInvalidCreateConnection($dsn)
RedisAdapter::createConnection($dsn);
}

public function provideValidSchemes()
{
return [
['redis'],
['rediss'],
];
}

public function provideInvalidCreateConnection()
{
return [
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Expand Up @@ -80,15 +80,19 @@ private function init($redisClient, $namespace, $defaultLifetime, ?MarshallerInt
*/
public static function createConnection($dsn, array $options = [])
{
if (0 !== strpos($dsn, 'redis:')) {
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: %s does not start with "redis:".', $dsn));
if (0 === strpos($dsn, 'redis:')) {
$scheme = 'redis';
} elseif (0 === strpos($dsn, 'rediss:')) {
$scheme = 'rediss';
} else {
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: %s does not start with "redis:" or "rediss".', $dsn));
}

if (!\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: %s', $dsn));
}

$params = preg_replace_callback('#^redis:(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
if (isset($m[2])) {
$auth = $m[2];
}
Expand Down

0 comments on commit 81bf2ab

Please sign in to comment.