X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FCache.php;h=93e292127f13af13e1301286579f8c39fd326a1e;hb=075af2f09c32084fb6729648b306ba40bdf07220;hp=26bdc6c3b7dd76e68fea0cf846868309ea66f36a;hpb=b280082d51641c500c3d2613197ba2f9446fce76;p=civicrm-core.git diff --git a/CRM/Utils/Cache.php b/CRM/Utils/Cache.php index 26bdc6c3b7..93e292127f 100644 --- a/CRM/Utils/Cache.php +++ b/CRM/Utils/Cache.php @@ -183,7 +183,7 @@ class CRM_Utils_Cache { $types = (array) $params['type']; if (!empty($params['name'])) { - $params['name'] = CRM_Core_BAO_Cache::cleanKey($params['name']); + $params['name'] = self::cleanKey($params['name']); } foreach ($types as $type) { @@ -220,6 +220,37 @@ class CRM_Utils_Cache { throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1)); } + /** + * Normalize a cache key. + * + * This bridges an impedance mismatch between our traditional caching + * and PSR-16 -- PSR-16 accepts a narrower range of cache keys. + * + * @param string $key + * Ex: 'ab/cd:ef' + * @return string + * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'. + * A similar key, but suitable for use with PSR-16-compliant cache providers. + */ + public static function cleanKey($key) { + if (!is_string($key) && !is_int($key)) { + throw new \RuntimeException("Malformed cache key"); + } + + $maxLen = 64; + $escape = '-'; + + if (strlen($key) >= $maxLen) { + return $escape . md5($key); + } + + $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) { + return $escape . dechex(ord($m[0])); + }, $key); + + return strlen($r) >= $maxLen ? $escape . md5($key) : $r; + } + /** * Assert that a key is well-formed. *