doctrine/cache · error · InvalidArgument

Cache key length must be greater than zero.

Error message

Cache key length must be greater than zero.

What it means

PSR-6 and Doctrine's adapter forbid empty-string cache keys. validKey() throws InvalidArgument when a '' string is passed to getItem, hasItem, deleteItem, deleteItems or validKeys, because an empty key cannot be stored or looked up meaningfully.

Solutions

  1. Ensure a non-empty fallback or prefix is used, e.g. $key !== '' ?: 'default'
  2. Validate/trim and reject empty keys at the application boundary before caching
  3. Log or skip caching when the computed key is empty instead of calling the pool

Example fix

// before
$item = $pool->getItem(trim($userInput));
// after
$key = trim($userInput);
if ($key === '') { throw new InvalidArgumentException('Cache key cannot be empty'); }
$item = $pool->getItem($key);
Defensive patterns

Strategy: validation

Validate before calling

if (!is_string($key) || $key === '') { throw new InvalidArgumentException('Cache key must be a non-empty string'); }

Type guard

function isValidCacheKey(mixed $key): bool { return is_string($key) && $key !== ''; }

Try / catch

try { return $pool->getItem($key); } catch (\Doctrine\Common\Cache\Psr6\InvalidArgument $e) { return null; }

Prevention

When it happens

Trigger: Calling any pool method with a key that is the empty string, typically a variable produced by concatenating missing parts or trimming a value down to nothing.

Common situations: Configuration field left blank ('') used directly as a cache key prefix; sprintf/implode producing '' when inputs are absent; trim() of whitespace-only input.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of doctrine/cache@e0a9919443 (2026-09-13). Data as JSON: /api/errors/5e1e144fa120de9f. Report an issue: GitHub.

Appendix: source

Thrown at lib/Doctrine/Common/Cache/Psr6/CacheAdapter.php:261

        return $success;
    }

    public function __destruct()
    {
        $this->commit();
    }

    /**
     * @param mixed $key
     */
    private static function validKey($key): bool
    {
        if (! is_string($key)) {
            throw new InvalidArgument(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
        }

        if ($key === '') {
            throw new InvalidArgument('Cache key length must be greater than zero.');
        }

        if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) {
            throw new InvalidArgument(sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS));
        }

        return true;
    }

    /**
     * @param mixed[] $keys
     */
    private static function validKeys(array $keys): bool
    {
        foreach ($keys as $key) {
            self::validKey($key);
        }

View on GitHub (pinned to e0a9919443)