Skip to content

Commit

Permalink
bug #46401 [Cache] Throw when "redis_sentinel" is used with a non-Pre…
Browse files Browse the repository at this point in the history
…dis "class" option (buffcode)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] Throw when "redis_sentinel" is used with a non-Predis "class" option

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| License       | MIT

Enforce usage of `\Predis\Client` when the `redis_sentinel` option is enabled.

Otherwise it is possible to give the option `class: Redis`, which will setup the connection without any error but fail to work, because the Redis commands will be issued against the Sentinel instances instead of the Redis instances.

Commits
-------

cdc9414 [Cache] Throw when "redis_sentinel" is used with a non-Predis "class" option
  • Loading branch information
nicolas-grekas committed May 21, 2022
2 parents 5b265a0 + cdc9414 commit 3a47e5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\SkippedTestSuiteError;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
Expand Down Expand Up @@ -43,4 +44,12 @@ public function testInvalidDSNHasBothClusterAndSentinel()
$dsn = 'redis:?host[redis1]&host[redis2]&host[redis3]&redis_cluster=1&redis_sentinel=mymaster';
RedisAdapter::createConnection($dsn);
}

public function testSentinelRequiresPredis()
{
$this->expectException(CacheException::class);
$this->expectExceptionMessage('Cannot use Redis Sentinel: class "Redis" does not extend "Predis\Client":');
$dsn = 'redis:?host[redis1]&host[redis2]&host[redis3]&redis_cluster=1&redis_sentinel=mymaster';
RedisAdapter::createConnection($dsn, ['class' => \Redis::class]);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Expand Up @@ -178,6 +178,10 @@ public static function createConnection($dsn, array $options = [])
$class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) ? \RedisArray::class : \Redis::class);
} else {
$class = $params['class'] ?? \Predis\Client::class;

if (isset($params['redis_sentinel']) && !is_a($class, \Predis\Client::class, true)) {
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client": "%s".', $class, $dsn));
}
}

if (is_a($class, \Redis::class, true)) {
Expand Down

0 comments on commit 3a47e5f

Please sign in to comment.