Skip to content

Commit 79e5ccb

Browse files
[Cache] handle prefixed redis connections when clearing pools
1 parent 5f2d5e0 commit 79e5ccb

15 files changed

+69
-14
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function setUpBeforeClass(): void
3232
self::markTestSkipped('REDIS_SENTINEL_SERVICE env var is not defined.');
3333
}
3434

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

3838
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

0 commit comments

Comments
 (0)