Skip to content

Redis v6 clear() bug when using lazy:true #51679

@JoniJnm

Description

@JoniJnm

Symfony version(s) affected

6.3.0

Description

Redis doesn't clear the namespace when using lazy=true (on redis connection or symfony container service)

How to reproduce

$redisConnection = \Symfony\Component\Cache\Adapter\RedisAdapter::createConnection(
    'redis:?host[my-host]',
    [
        'lazy' => true
    ]
);

$redisAdapter = new \Symfony\Component\Cache\Adapter\RedisAdapter($redisConnection, 'testing');
$redis = new \Symfony\Component\Cache\Psr16Cache($redisAdapter);

$redis->set('my-key', 'my-value');
$redis->clear();
$value = $redis->get('my-key');
// here, $value is "my-value". With lazy=false $value is null

Possible Solution

I don't know if this is a bug in redis but symfony can fix it chaning the RedisProxy::scan function:

public function scan(&$iterator, $pattern = null, $count = 0, $type = null): array|false
{
    $this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)();

    if (\func_num_args() > 3) {
        return $this->lazyObjectState->realInstance->scan($iterator, $pattern, $count, $type, ...\array_slice(\func_get_args(), 4));
    } else {
        return $this->lazyObjectState->realInstance->scan($iterator, $pattern, $count);
    }
}

Note: this works only for lazy=true on redis conection but the error still exists for lazy=true on symfony container service

Additional Context

$version = phpversion("redis"); // 6.0.0

The RedisTrait::doClear function scan all the keys in the namespace:

$keys = $host instanceof \Predis\ClientInterface ? $host->scan($cursor, 'MATCH', $pattern, 'COUNT', 1000) : $host->scan($cursor, $pattern, 1000);

When using lazy: true parameter (in symfony container service or redis connection), the RedisProxy propagates all the function arguments to the redis::scan function:

return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->scan($iterator, $pattern, $count, $type, ...\array_slice(\func_get_args(), 4));

The problem is: redis doesn't return keys if we send $type=null as parameter

Seems to be related with: phpredis/phpredis#2362

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy