Skip to content

Performance issues in production environments #22

@nikita-stena

Description

@nikita-stena

Hello! In the RedisCacheEngine class's clear method, line 162 uses the keys method to retrieve all keys by mask. Keys isn't performant; you should use ->scan to search by mask.
The main reasons against Keys are:

  1. It locks Redis for the entire execution time.
  2. It's O(N) complex - the more keys, the longer the lock.
  3. With large databases, it can lead to timeouts and errors.
  4. It's a production anti-pattern.
    /**
     * @throws NotFoundExceptionInterface
     * @throws InvalidArgumentException
     * @throws RedisException
     * @throws ContainerExceptionInterface
     */
    #[\Override]
    public function clear(): bool
    {
        $keys = $this->redis->keys('cache:*');
        foreach ($keys as $key) {
            if (preg_match('/^cache:(?<key>.*)/', $key, $matches)) {
                $this->delete($matches['key']);
            }
        }
        return true;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions