From 84413eca436d776b6e8f29dca7a9b1f055c2e8d1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 21 Jan 2019 02:26:23 -0800 Subject: [PATCH] CRM_Utils_Cache::create() - Accept new option `withArray` This adds and documents a new config option which can be passed into the cache factory. The option, `withArray`, indicates that we prefer to have a thread-local array acting as an extra cache-tier. --- CRM/Utils/Cache.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/CRM/Utils/Cache.php b/CRM/Utils/Cache.php index a1cffded14..d147eb1f00 100644 --- a/CRM/Utils/Cache.php +++ b/CRM/Utils/Cache.php @@ -165,6 +165,16 @@ class CRM_Utils_Cache { * should function correctly, but it can be harder to inspect/debug. * - type: array|string, list of acceptable cache types, in order of preference. * - prefetch: bool, whether to prefetch all data in cache (if possible). + * - withArray: bool|null|'fast', whether to setup a thread-local array-cache in front of the cache driver. + * Note that cache-values may be passed to the underlying driver with extra metadata, + * so this will slightly change/enlarge the on-disk format. + * 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. + * 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. * @return CRM_Utils_Cache_Interface * @throws CRM_Core_Exception * @see Civi::cache() @@ -183,7 +193,11 @@ class CRM_Utils_Cache { $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); + $cache = new $dbCacheClass($settings); + if (!empty($params['withArray'])) { + $cache = $params['withArray'] === 'fast' ? new CRM_Utils_Cache_FastArrayDecorator($cache) : new CRM_Utils_Cache_ArrayDecorator($cache); + } + return $cache; } break; -- 2.25.1