3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Cache is an empty base object, we'll modify the scheme when we have different caching schemes
21 class CRM_Utils_Cache
{
23 const DELIMITER
= '/';
26 * (Quasi-Private) Treat this as private. It is marked public to facilitate testing.
28 * We only need one instance of this object. So we use the singleton
29 * pattern and cache the instance in this variable
33 public static $_singleton = NULL;
38 * @param array $config
39 * An array of configuration params.
41 * @return \CRM_Utils_Cache
42 * @throws \CRM_Core_Exception
44 public function __construct(&$config) {
45 throw new CRM_Core_Exception(ts('this is just an interface and should not be called directly'));
49 * Singleton function used to manage this object.
51 * @return CRM_Utils_Cache_Interface
53 public static function &singleton() {
54 if (self
::$_singleton === NULL) {
55 $className = self
::getCacheDriver();
56 // a generic method for utilizing any of the available db caches.
57 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
58 $settings = self
::getCacheSettings($className);
59 $settings['prefix'] = CRM_Utils_Array
::value('prefix', $settings, '') . self
::DELIMITER
. 'default' . self
::DELIMITER
;
60 self
::$_singleton = new $dbCacheClass($settings);
62 return self
::$_singleton;
66 * Get cache relevant settings.
71 * associative array of settings for the cache
73 public static function getCacheSettings($cachePlugin) {
74 switch ($cachePlugin) {
84 'host' => 'localhost',
90 // Use old constants if needed to ensure backward compatibility
91 if (defined('CIVICRM_MEMCACHE_HOST')) {
92 $defaults['host'] = CIVICRM_MEMCACHE_HOST
;
95 if (defined('CIVICRM_MEMCACHE_PORT')) {
96 $defaults['port'] = CIVICRM_MEMCACHE_PORT
;
99 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
100 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT
;
103 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
104 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX
;
107 // Use new constants if possible
108 if (defined('CIVICRM_DB_CACHE_HOST')) {
109 $defaults['host'] = CIVICRM_DB_CACHE_HOST
;
112 if (defined('CIVICRM_DB_CACHE_PORT')) {
113 $defaults['port'] = CIVICRM_DB_CACHE_PORT
;
116 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
117 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT
;
120 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
121 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX
;
128 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
129 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT
;
131 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
132 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX
;
140 * Create a new, named, limited-use cache.
142 * This is a factory function. Generally, you should use Civi::cache($name)
143 * to locate managed cached instance.
145 * @param array $params
147 * - name: string, unique symbolic name.
148 * For a naming convention, use `snake_case` or `CamelCase` to maximize
149 * portability/cleanliness. Any other punctuation or whitespace
150 * should function correctly, but it can be harder to inspect/debug.
151 * - type: array|string, list of acceptable cache types, in order of preference.
152 * - prefetch: bool, whether to prefetch all data in cache (if possible).
153 * - withArray: bool|null|'fast', whether to setup a thread-local array-cache in front of the cache driver.
154 * Note that cache-values may be passed to the underlying driver with extra metadata,
155 * so this will slightly change/enlarge the on-disk format.
156 * Support varies by driver:
157 * - For most memory backed caches, this option is meaningful.
158 * - For SqlGroup, this option is ignored. SqlGroup has equivalent behavior built-in.
159 * - For ArrayCache, this option is ignored. It's redundant.
160 * If this is a short-lived process in which TTL's don't matter, you might
161 * use 'fast' mode. It sacrifices some PSR-16 compliance and cache-coherency
162 * protections to improve performance.
163 * @return CRM_Utils_Cache_Interface
164 * @throws CRM_Core_Exception
167 public static function create($params = []) {
168 $types = (array) $params['type'];
170 if (!empty($params['name'])) {
171 $params['name'] = self
::cleanKey($params['name']);
174 foreach ($types as $type) {
177 if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS
, ['Memcache', 'Memcached', 'Redis'])) {
178 $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS
;
179 $settings = self
::getCacheSettings(CIVICRM_DB_CACHE_CLASS
);
180 $settings['prefix'] = CRM_Utils_Array
::value('prefix', $settings, '') . self
::DELIMITER
. $params['name'] . self
::DELIMITER
;
181 $cache = new $dbCacheClass($settings);
182 if (!empty($params['withArray'])) {
183 $cache = $params['withArray'] === 'fast' ?
new CRM_Utils_Cache_FastArrayDecorator($cache) : new CRM_Utils_Cache_ArrayDecorator($cache);
190 if (defined('CIVICRM_DSN') && CIVICRM_DSN
) {
191 return new CRM_Utils_Cache_SqlGroup([
192 'group' => $params['name'],
193 'prefetch' => $params['prefetch'] ??
FALSE,
200 return new CRM_Utils_Cache_ArrayCache([]);
205 throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
209 * Normalize a cache key.
211 * This bridges an impedance mismatch between our traditional caching
212 * and PSR-16 -- PSR-16 accepts a narrower range of cache keys.
217 * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'.
218 * A similar key, but suitable for use with PSR-16-compliant cache providers.
220 public static function cleanKey($key) {
221 if (!is_string($key) && !is_int($key)) {
222 throw new \
RuntimeException("Malformed cache key");
228 if (strlen($key) >= $maxLen) {
229 return $escape . md5($key);
232 $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) {
233 return $escape . dechex(ord($m[0]));
236 return strlen($r) >= $maxLen ?
$escape . md5($key) : $r;
240 * Assert that a key is well-formed.
244 * Same $key, if it's valid.
245 * @throws \CRM_Utils_Cache_InvalidArgumentException
247 public static function assertValidKey($key) {
248 $strict = CRM_Utils_Constant
::value('CIVICRM_PSR16_STRICT', FALSE) ||
defined('CIVICRM_TEST');
250 if (!is_string($key)) {
251 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string");
254 if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) {
255 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters");
258 if ($strict && strlen($key) > 255) {
259 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long");
267 * Ex: 'ArrayCache', 'Memcache', 'Redis'.
269 public static function getCacheDriver() {
270 // default to ArrayCache for now
271 $className = 'ArrayCache';
273 // Maintain backward compatibility for now.
274 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
275 // override the CIVICRM_DB_CACHE_CLASS setting.
276 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
277 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE
) {
278 $className = 'Memcache';
281 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE
) {
282 $className = 'ArrayCache';
285 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS
) {
286 $className = CIVICRM_DB_CACHE_CLASS
;
293 * Generate a unique negative-acknowledgement token (NACK).
295 * When using PSR-16 to read a value, the `$cahce->get()` will a return a default
296 * value on cache-miss, so it's hard to know if you've gotten a geniune value
297 * from the cache or just a default. If you're in an edge-case where it matters
298 * (and you want to do has()+get() in a single roundtrip), use the nack() as
301 * $nack = CRM_Utils_Cache::nack();
302 * $value = $cache->get('foo', $nack);
303 * echo ($value === $nack) ? "Cache has a value, and we got it" : "Cache has no value".
305 * The value should be unique to avoid accidental matches.
308 * Unique nonce value indicating a "negative acknowledgement" (failed read).
309 * If we need to accurately perform has($key)+get($key), we can
310 * use `get($key,$nack)`.
312 public static function nack() {
313 $st =& Civi
::$statics[__CLASS__
];
314 if (!isset($st['nack-c'])) {
315 $st['nack-c'] = md5(CRM_Utils_Request
::id() . CIVICRM_SITE_KEY
. CIVICRM_DSN
. mt_rand(0, 10000));
318 return 'NACK:' . $st['nack-c'] . $st['nack-i']++
;