Skip to content

[Cache] handle prefixed redis connections when clearing pools #41793

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

Merged
merged 1 commit into from
Jun 23, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractRedisAdapterTest extends AdapterTestCase

protected static $redis;

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
Expand All @@ -45,4 +45,18 @@ public static function tearDownAfterClass(): void
{
self::$redis = null;
}

/**
* @runInSeparateProcess
*/
public function testClearWithPrefix()
{
$cache = $this->createCachePool(0, __FUNCTION__);

$cache->save($cache->getItem('foo')->set('bar'));
$this->assertTrue($cache->hasItem('foo'));

$cache->clear();
$this->assertFalse($cache->hasItem('foo'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PredisAdapterTest extends AbstractRedisAdapterTest
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')]);
self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')], ['prefix' => 'prefix_']);
}

public function testCreateConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PredisClusterAdapterTest extends AbstractRedisAdapterTest
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]]);
self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]], ['prefix' => 'prefix_']);
}

public static function tearDownAfterClass(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function setUpBeforeClass(): void
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
}

self::$redis = RedisAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['class' => \Predis\Client::class, 'redis_cluster' => true]);
self::$redis = RedisAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['class' => \Predis\Client::class, 'redis_cluster' => true, 'prefix' => 'prefix_']);
}

public static function tearDownAfterClass(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function setUpBeforeClass(): void
self::markTestSkipped('REDIS_SENTINEL_SERVICE env var is not defined.');
}

self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => $service]);
self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => $service, 'prefix' => 'prefix_']);
}

public function testInvalidDSNHasBothClusterAndSentinel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public static function setUpBeforeClass(): void
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), ['lazy' => true]);
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
$adapter = parent::createCachePool($defaultLifetime);
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$adapter = parent::createCachePool($defaultLifetime, $testMethod);
$this->assertInstanceOf(RedisProxy::class, self::$redis);

return $adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public static function setUpBeforeClass(): void
self::markTestSkipped('The RedisArray class is required.');
}
self::$redis = new \RedisArray([getenv('REDIS_HOST')], ['lazy_connect' => true]);
self::$redis->setOption(\Redis::OPT_PREFIX, 'prefix_');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public static function setUpBeforeClass(): void
}

self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['lazy' => true, 'redis_cluster' => true]);
self::$redis->setOption(\Redis::OPT_PREFIX, 'prefix_');
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
$adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$this->assertInstanceOf(RedisProxy::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$this->assertInstanceOf(\RedisArray::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Cache/Traits/RedisClusterNodeProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function scan(&$iIterator, $strPattern = null, $iCount = null)
{
return $this->redis->scan($iIterator, $this->host, $strPattern, $iCount);
}

public function getOption($name)
{
return $this->redis->getOption($name);
}
}
16 changes: 15 additions & 1 deletion src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ protected function doHave($id)
*/
protected function doClear($namespace)
{
if ($this->redis instanceof \Predis\ClientInterface) {
$prefix = $this->redis->getOptions()->prefix ? $this->redis->getOptions()->prefix->getPrefix() : '';
$prefixLen = \strlen($prefix);
}

$cleared = true;
$hosts = $this->getHosts();
$host = reset($hosts);
Expand All @@ -379,7 +384,11 @@ protected function doClear($namespace)
$info = $host->info('Server');
$info = $info['Server'] ?? $info;

$pattern = $namespace.'*';
if (!$host instanceof \Predis\ClientInterface) {
$prefix = \defined('Redis::SCAN_PREFIX') && (\Redis::SCAN_PREFIX & $host->getOption(\Redis::OPT_SCAN)) ? '' : $host->getOption(\Redis::OPT_PREFIX);
$prefixLen = \strlen($host->getOption(\Redis::OPT_PREFIX) ?? '');
}
$pattern = $prefix.$namespace.'*';

if (!version_compare($info['redis_version'], '2.8', '>=')) {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
Expand All @@ -398,6 +407,11 @@ protected function doClear($namespace)
$keys = $keys[1];
}
if ($keys) {
if ($prefixLen) {
foreach ($keys as $i => $key) {
$keys[$i] = substr($key, $prefixLen);
}
}
$this->doDelete($keys);
}
} while ($cursor = (int) $cursor);
Expand Down