Merge pull request #15843 from totten/master-simplehead
[civicrm-core.git] / CRM / Utils / Cache.php
index 23975cae4d2adb302dbc3e40e9c024f967bf1fe7..3ac1130ce9438601ef17441ce6f0069089c11db2 100644 (file)
@@ -1,34 +1,18 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
 /**
@@ -55,9 +39,10 @@ class CRM_Utils_Cache {
    *   An array of configuration params.
    *
    * @return \CRM_Utils_Cache
+   * @throws \CRM_Core_Exception
    */
   public function __construct(&$config) {
-    CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
+    throw new CRM_Core_Exception(ts('this is just an interface and should not be called directly'));
   }
 
   /**
@@ -171,7 +156,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.
@@ -183,7 +168,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 +205,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.
    *