0 follower

Final Class Yiisoft\Cache\CacheKeyNormalizer

InheritanceYiisoft\Cache\CacheKeyNormalizer

CacheKeyNormalizer normalizes the cache key to a string.

Public Methods

Hide inherited methods

Method Description Defined By
normalize() Normalizes the cache key from a given key. Yiisoft\Cache\CacheKeyNormalizer

Method Details

Hide inherited methods

normalize() public static method

Normalizes the cache key from a given key.

If the given key is a string that does not contain characters }()/\@: and no more than 64 characters, then the key will be returned back as it is, integers will be converted to strings. Otherwise, a normalized key is generated by serializing the given key and applying MD5 hashing.

See also https://www.php-fig.org/psr/psr-16/#12-definitions.

public static string normalize ( mixed $key )
$key mixed

The key to be normalized.

return string

The normalized cache key.

throws Yiisoft\Cache\Exception\InvalidArgumentException

For invalid key.

                public static function normalize(mixed $key): string
{
    if (is_string($key) || is_int($key)) {
        $key = (string) $key;
        $length = mb_strlen($key, '8bit');
        return (strpbrk($key, '{}()/\@:') || $length < 1 || $length > 64) ? md5($key) : $key;
    }
    if (($key = json_encode($key)) === false) {
        throw new InvalidArgumentException('Invalid key. ' . json_last_error_msg());
    }
    return md5($key);
}