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

Skip to content

Commit fa55b12

Browse files
[Cache] handle prefixed redis connections when clearing pools
1 parent 8391e0b commit fa55b12

14 files changed

+71
-13
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class AbstractRedisAdapterTest extends AdapterTestCase
2424

2525
protected static $redis;
2626

27-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
27+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
2828
{
2929
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
3030
}
@@ -45,4 +45,18 @@ public static function tearDownAfterClass(): void
4545
{
4646
self::$redis = null;
4747
}
48+
49+
/**
50+
* @runInSeparateProcess
51+
*/
52+
public function testClearWithPrefix()
53+
{
54+
$cache = $this->createCachePool(0, __FUNCTION__);
55+
56+
$cache->save($cache->getItem('foo')->set('bar'));
57+
$this->assertTrue($cache->hasItem('foo'));
58+
59+
$cache->clear();
60+
$this->assertFalse($cache->hasItem('foo'));
61+
}
4862
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PredisAdapterTest extends AbstractRedisAdapterTest
2222
public static function setUpBeforeClass(): void
2323
{
2424
parent::setUpBeforeClass();
25-
self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')]);
25+
self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')], ['prefix' => 'prefix_']);
2626
}
2727

2828
public function testCreateConnection()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PredisClusterAdapterTest extends AbstractRedisAdapterTest
1919
public static function setUpBeforeClass(): void
2020
{
2121
parent::setUpBeforeClass();
22-
self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]]);
22+
self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]], ['prefix' => 'prefix_']);
2323
}
2424

2525
public static function tearDownAfterClass(): void

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function setUpBeforeClass(): void
2424
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
2525
}
2626

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

3030
public static function tearDownAfterClass(): void

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function setUp(): void
2727
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
2828
}
2929

30-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
30+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3131
{
3232
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
3333
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function setUp(): void
2727
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
2828
}
2929

30-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
30+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3131
{
3232
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
3333
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static function setUpBeforeClass(): void
3333
}
3434

3535
self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => $service]);
36+
self::$redis->setOption(\Redis::OPT_PREFIX, 'prefix_');
3637
}
3738

3839
public function testInvalidDSNHasBothClusterAndSentinel()

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ public static function setUpBeforeClass(): void
2828
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), ['lazy' => true]);
2929
}
3030

31-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
31+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3232
{
33-
$adapter = parent::createCachePool($defaultLifetime);
33+
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
34+
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
35+
}
36+
37+
$adapter = parent::createCachePool($defaultLifetime, $testMethod);
3438
$this->assertInstanceOf(RedisProxy::class, self::$redis);
3539

3640
return $adapter;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ public static function setUpBeforeClass(): void
2323
self::markTestSkipped('The RedisArray class is required.');
2424
}
2525
self::$redis = new \RedisArray([getenv('REDIS_HOST')], ['lazy_connect' => true]);
26+
self::$redis->setOption(\Redis::OPT_PREFIX, 'prefix_');
2627
}
2728
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ public static function setUpBeforeClass(): void
3232
}
3333

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

37-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
38+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3839
{
40+
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
41+
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
42+
}
43+
3944
$this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
4045
$adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4146

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ protected function setUp(): void
2828
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
2929
}
3030

31-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
31+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3232
{
33+
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
34+
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
35+
}
36+
3337
$this->assertInstanceOf(RedisProxy::class, self::$redis);
3438
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
3539

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ protected function setUp(): void
2727
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
2828
}
2929

30-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
30+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3131
{
32+
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
33+
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
34+
}
35+
3236
$this->assertInstanceOf(\RedisArray::class, self::$redis);
3337
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
3438

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ protected function setUp(): void
2828
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
2929
}
3030

31-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
31+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
3232
{
33+
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
34+
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
35+
}
36+
3337
$this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
3438
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
3539

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ protected function doClear($namespace)
369369
$evalArgs = [[$namespace], 0];
370370
}
371371

372+
if ($this->redis instanceof \Predis\ClientInterface) {
373+
$scanPrefix = $this->redis->getOptions()->prefix ? $this->redis->getOptions()->prefix->getPrefix() : '';
374+
$prefixLen = \strlen($scanPrefix);
375+
}
376+
372377
$hosts = $this->getHosts();
373378
$host = reset($hosts);
374379
if ($host instanceof \Predis\Client && $host->getConnection() instanceof ReplicationInterface) {
@@ -394,13 +399,29 @@ protected function doClear($namespace)
394399
}
395400

396401
$cursor = null;
402+
403+
if (!$host instanceof \Predis\ClientInterface) {
404+
$scanPrefix = \defined('Redis::SCAN_PREFIX') && (\Redis::SCAN_PREFIX & $host->getOption(\Redis::OPT_SCAN)) ? '' : $host->getOption(\Redis::OPT_PREFIX);
405+
$prefixLen = \strlen($host->getOption(\Redis::OPT_PREFIX) ?? '');
406+
}
407+
397408
do {
398-
$keys = $host instanceof \Predis\ClientInterface ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 1000) : $host->scan($cursor, $namespace.'*', 1000);
409+
if ($host instanceof \Predis\ClientInterface) {
410+
$keys = $host->scan($cursor, 'MATCH', $scanPrefix.$namespace.'*', 'COUNT', 1000);
411+
} else {
412+
$keys = $host->scan($cursor, $scanPrefix.$namespace.'*', 1000);
413+
}
414+
399415
if (isset($keys[1]) && \is_array($keys[1])) {
400416
$cursor = $keys[0];
401417
$keys = $keys[1];
402418
}
403419
if ($keys) {
420+
if ($prefixLen) {
421+
foreach ($keys as $i => $key) {
422+
$keys[$i] = substr($key, $prefixLen);
423+
}
424+
}
404425
$this->doDelete($keys);
405426
}
406427
} while ($cursor = (int) $cursor);

0 commit comments

Comments
 (0)