symfony/symfony · error · Symfony\Component\Cache\Exception\InvalidArgumentException

Cache key length must be greater than zero.

Error message

Cache key length must be greater than zero.

What it means

CacheItem::validateKey() rejects empty-string keys. PSR-6 mandates that keys must be non-empty because an empty key is ambiguous and many storage backends cannot distinguish between 'no key' and 'empty key'. This InvalidArgumentException is thrown when the key is a string but equals ''.

Source

Thrown at src/Symfony/Component/Cache/CacheItem.php:137

    public function getMetadata(): array
    {
        return $this->metadata;
    }

    /**
     * Validates a cache key according to PSR-6.
     *
     * @param mixed $key The key to validate
     *
     * @throws InvalidArgumentException When $key is not valid
     */
    public static function validateKey($key): string
    {
        if (!\is_string($key)) {
            throw new InvalidArgumentException(\sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
        }
        if ('' === $key) {
            throw new InvalidArgumentException('Cache key length must be greater than zero.');
        }
        if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) {
            throw new InvalidArgumentException(\sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS));
        }

        return $key;
    }

    /**
     * Internal logging helper.
     *
     * @internal
     */
    public static function log(?LoggerInterface $logger, string $message, array $context = []): void
    {
        if ($logger) {
            $logger->warning($message, $context);
        } else {

View on GitHub (pinned to 698e28026c)

Solutions

  1. Guard against empty keys: if ('' !== $key) { $pool->getItem($key); }.
  2. Provide a fallback default key or skip caching when the key is empty.
  3. Validate key inputs at the entry point of your caching layer.

Example fix

// before
$key = trim($input) ?? '';
$item = $pool->getItem($key); // throws if empty

// after
$key = trim($input);
if ($key === '') {
    return $default;
}
$item = $pool->getItem($key);
Defensive patterns

Strategy: validation

Validate before calling

if ('' === $key) {
    return $default; // or throw your own exception
}
$item = $pool->getItem($key);

Type guard

function isNonEmptyKey(string $key): bool {
    return '' !== $key;
}

Try / catch

try {
    $item = $pool->getItem($key);
} catch (\Symfony\Component\Cache\Exception\InvalidArgumentException $e) {
    // skip cache for empty keys
    return $default;
}

Prevention

When it happens

Trigger: Calling $pool->getItem('') with an explicitly empty string, or passing a variable that resolves to an empty string (e.g. a trimmed blank input, or an empty environment variable used as a cache key).

Common situations: Dynamic key construction where a component is missing: $pool->getItem($prefix . '_' . $suffix) when both are empty. Or fetching a cache key from configuration that hasn't been set.

Related errors


AI-assisted analysis of symfony/symfony@698e28026c (2026-08-06). Data as JSON: /api/errors/04f7e7cf9093ae8f. Report an issue: GitHub.