Merge pull request #14581 from seamuslee001/convert_navigation_cache
[civicrm-core.git] / CRM / Utils / Cache.php
index a60ccfd84ca8ca376733c58a69c18a75467769ef..93e292127f13af13e1301286579f8c39fd326a1e 100644 (file)
@@ -89,18 +89,18 @@ class CRM_Utils_Cache {
     switch ($cachePlugin) {
       case 'ArrayCache':
       case 'NoCache':
-        $defaults = array();
+        $defaults = [];
         break;
 
       case 'Redis':
       case 'Memcache':
       case 'Memcached':
-        $defaults = array(
+        $defaults = [
           'host' => 'localhost',
           'port' => 11211,
           'timeout' => 3600,
           'prefix' => '',
-        );
+        ];
 
         // Use old constants if needed to ensure backward compatibility
         if (defined('CIVICRM_MEMCACHE_HOST')) {
@@ -139,7 +139,7 @@ class CRM_Utils_Cache {
         break;
 
       case 'APCcache':
-        $defaults = array();
+        $defaults = [];
         if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
           $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
         }
@@ -171,7 +171,7 @@ class CRM_Utils_Cache {
    *     Support varies by driver:
    *       - For most memory backed caches, this option is meaningful.
    *       - For SqlGroup, this option is ignored. SqlGroup has equivalent behavior built-in.
-   *       - For Arraycache, this option is ignored. It's redundant.
+   *       - For ArrayCache, this option is ignored. It's redundant.
    *      If this is a short-lived process in which TTL's don't matter, you might
    *      use 'fast' mode. It sacrifices some PSR-16 compliance and cache-coherency
    *      protections to improve performance.
@@ -179,17 +179,17 @@ class CRM_Utils_Cache {
    * @throws CRM_Core_Exception
    * @see Civi::cache()
    */
-  public static function create($params = array()) {
+  public static function create($params = []) {
     $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) {
       switch ($type) {
         case '*memory*':
-          if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('Memcache', 'Memcached', 'Redis'))) {
+          if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, ['Memcache', 'Memcached', 'Redis'])) {
             $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS;
             $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS);
             $settings['prefix'] = CRM_Utils_Array::value('prefix', $settings, '') . self::DELIMITER . $params['name'] . self::DELIMITER;
@@ -203,16 +203,16 @@ class CRM_Utils_Cache {
 
         case 'SqlGroup':
           if (defined('CIVICRM_DSN') && CIVICRM_DSN) {
-            return new CRM_Utils_Cache_SqlGroup(array(
+            return new CRM_Utils_Cache_SqlGroup([
               'group' => $params['name'],
               'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE),
-            ));
+            ]);
           }
           break;
 
         case 'Arraycache':
         case 'ArrayCache':
-          return new CRM_Utils_Cache_ArrayCache(array());
+          return new CRM_Utils_Cache_ArrayCache([]);
 
       }
     }
@@ -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.
    *
@@ -251,7 +282,8 @@ class CRM_Utils_Cache {
    *   Ex: 'ArrayCache', 'Memcache', 'Redis'.
    */
   public static function getCacheDriver() {
-    $className = 'ArrayCache';   // default to ArrayCache for now
+    // default to ArrayCache for now
+    $className = 'ArrayCache';
 
     // Maintain backward compatibility for now.
     // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will