phpfastcache icon indicating copy to clipboard operation
phpfastcache copied to clipboard

Redis will only scan 9999 keys in getAllItems

Open mapcentia opened this issue 8 months ago • 3 comments

What type of issue is this?

Incorrect/unexpected/unexplainable behavior

Operating system + version

All

PHP version

All

Connector/Database version (if applicable)

Redis

Phpfastcache version

9.2.0 ✅

Describe the issue you're facing

I think, that the method driverReadAllKeys in the Redis driver is implemented wrong.

MAX_ALL_KEYS_COUNT is set to 9999 , which I believe is meant to prevent the return of more keys than the limit. But that's no good when using a pattern, because Redis will only scan the first 9999 keys in the db. If the pattern should match keys after 9999 they will not be found.

https://github.com/PHPSocialNetwork/phpfastcache/blob/master/lib/Phpfastcache/Drivers/Redis/Driver.php#L129

Expected behavior

When using a pattern all keys should be scanned

Code sample (optional)

No response

Suggestion to fix the issue (optional)

Use the cursor in scan and break when number of keys crosses the limit. The count defaults to 10 in scan, so the number of keys could exceed MAX_ALL_KEYS_COUNT:

    protected function driverReadAllKeys(string $pattern = '*'): iterable
    {
        $i = null;
        $keys = [];
        do {
            $tmp = $this->instance->scan($i, $pattern === '' ? '*' : $pattern);
            $keys = array_merge($keys, $tmp);
            if (sizeof($keys) > ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) {
                break;
            }
        } while ($i > 0);
        return $keys;
    }

References (optional)

No response

Do you have anything more you want to share? (optional)

No response

Have you searched in our Wiki before posting ?

  • [X] I have searched over the Wiki

mapcentia avatar Jun 06 '24 13:06 mapcentia