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
- Guard against empty keys: if ('' !== $key) { $pool->getItem($key); }.
- Provide a fallback default key or skip caching when the key is empty.
- 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
- Check for empty strings before calling getItem().
- Return early or use defaults when keys are empty.
- Validate cache keys at your service boundary.
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
- Expiration date must be an integer, a DateInterval or null,
- Cache tag must be string or object that implements __toStrin
- Cache tag length must be greater than zero.
- Cache tag "%s" contains reserved characters "%s".
- Cache key must be string, "%s" given.
AI-assisted analysis of symfony/symfony@698e28026c (2026-08-06).
Data as JSON: /api/errors/04f7e7cf9093ae8f.
Report an issue: GitHub.