'localhost', 'port' => 11211, 'timeout' => 3600, 'prefix' => '', ); // Use old constants if needed to ensure backward compatibility if (defined('CIVICRM_MEMCACHE_HOST')) { $defaults['host'] = CIVICRM_MEMCACHE_HOST; } if (defined('CIVICRM_MEMCACHE_PORT')) { $defaults['port'] = CIVICRM_MEMCACHE_PORT; } if (defined('CIVICRM_MEMCACHE_TIMEOUT')) { $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT; } if (defined('CIVICRM_MEMCACHE_PREFIX')) { $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX; } // Use new constants if possible if (defined('CIVICRM_DB_CACHE_HOST')) { $defaults['host'] = CIVICRM_DB_CACHE_HOST; } if (defined('CIVICRM_DB_CACHE_PORT')) { $defaults['port'] = CIVICRM_DB_CACHE_PORT; } if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; } if (defined('CIVICRM_DB_CACHE_PREFIX')) { $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; } break; case 'APCcache': $defaults = array(); if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; } if (defined('CIVICRM_DB_CACHE_PREFIX')) { $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; } break; } return $defaults; } /** * Create a new, named, limited-use cache. * * This is a factory function. Generally, you should use Civi::cache($name) * to locate managed cached instance. * * @param array $params * Array with keys: * - name: string, unique symbolic name. * - type: array|string, list of acceptable cache types, in order of preference. * - prefetch: bool, whether to prefetch all data in cache (if possible). * @return CRM_Utils_Cache_Interface * @throws CRM_Core_Exception * @see Civi::cache() */ public static function create($params = array()) { $types = (array) $params['type']; foreach ($types as $type) { switch ($type) { case '*memory*': if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('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; return new $dbCacheClass($settings); } break; case 'SqlGroup': if (defined('CIVICRM_DSN') && CIVICRM_DSN) { return new CRM_Utils_Cache_SqlGroup(array( 'group' => $params['name'], 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE), )); } break; case 'Arraycache': case 'ArrayCache': return new CRM_Utils_Cache_ArrayCache(array()); } } throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1)); } /** * Assert that a key is well-formed. * * @param string $key * @return string * Same $key, if it's valid. * @throws \CRM_Utils_Cache_InvalidArgumentException */ public static function assertValidKey($key) { $strict = CRM_Utils_Constant::value('CIVICRM_PSR16_STRICT', FALSE) || defined('CIVICRM_TEST'); if (!is_string($key)) { throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string"); } if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) { throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters"); } if ($strict && strlen($key) > 255) { throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long"); } return $key; } }