Closed
Description
Symfony version(s) affected
6.2.7
Description
When injecting a Redis (PHPRedis) instance, with lazy: true
, the behaviour of the scan
command is broken.
The iterator reference is not set properly and will not allow a loop to retrieve all the results from the command.
It looks like the changes introduced here causes this issue.
How to reproduce
PHP Unit test to reproduce
class ScanTest extends KernelTestCase
{
private \Redis $redis;
protected function setUp(): void
{
parent::setUp();
/* @var \Redis $redis */
$this->redis = $this->getService('redis_provider');
$this->redis->flushDB();
}
public function testScanRetrieveAllResults(): void
{
$nbFixtures = 12; // default SCAN count is 10
for ($i = 0; $i < $nbFixtures; ++$i) {
$this->redis->set((string) $i, (string) $i);
}
$allKeys = [];
$iterator = null;
do {
// SF 6.2 will loop infinitely
$allKeys = array_merge($allKeys, $this->redis->scan($iterator, '*', 2));
} while (0 !== $iterator);
// SF 6.1 succeed, retrieves 12 elements
$this->assertEquals($nbFixtures, count($allKeys));
}
}
Service declaration
services:
redis_provider:
class: \Redis
lazy: true # issue here
factory: ['App\Cache\Redis\Factory\RedisConnectionFactory', 'createConnection']
arguments:
- '%env(REDIS_DSN)%' # redis://local_redis/0 given you have a Redis locally
Factory
namespace App\Cache\Redis\Factory;
use Symfony\Component\Cache\Adapter\RedisAdapter;
class RedisConnectionFactory
{
public static function createConnection(string $dsn, array $options = []): \Redis
{
return RedisAdapter::createConnection($dsn, $options);
}
}
Possible Solution
Not using lazy-loading. lazy: false
But this does not fix the original issue.
Additional Context
Symfony version 6.2.7
PHP version 8.1
Redis version: 6.2.2
PHPRedis