3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * Cache is an empty base object, we'll modify the scheme when we have different caching schemes
37 class CRM_Utils_Cache
{
39 const DELIMITER
= '/';
42 * (Quasi-Private) Treat this as private. It is marked public to facilitate testing.
44 * We only need one instance of this object. So we use the singleton
45 * pattern and cache the instance in this variable
49 public static $_singleton = NULL;
54 * @param array $config
55 * An array of configuration params.
57 * @return \CRM_Utils_Cache
59 public function __construct(&$config) {
60 CRM_Core_Error
::fatal(ts('this is just an interface and should not be called directly'));
64 * Singleton function used to manage this object.
66 * @return CRM_Utils_Cache_Interface
68 public static function &singleton() {
69 if (self
::$_singleton === NULL) {
70 $className = self
::getCacheDriver();
71 // a generic method for utilizing any of the available db caches.
72 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
73 $settings = self
::getCacheSettings($className);
74 $settings['prefix'] = CRM_Utils_Array
::value('prefix', $settings, '') . self
::DELIMITER
. 'default' . self
::DELIMITER
;
75 self
::$_singleton = new $dbCacheClass($settings);
77 return self
::$_singleton;
81 * Get cache relevant settings.
86 * associative array of settings for the cache
88 public static function getCacheSettings($cachePlugin) {
89 switch ($cachePlugin) {
99 'host' => 'localhost',
105 // Use old constants if needed to ensure backward compatibility
106 if (defined('CIVICRM_MEMCACHE_HOST')) {
107 $defaults['host'] = CIVICRM_MEMCACHE_HOST
;
110 if (defined('CIVICRM_MEMCACHE_PORT')) {
111 $defaults['port'] = CIVICRM_MEMCACHE_PORT
;
114 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
115 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT
;
118 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
119 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX
;
122 // Use new constants if possible
123 if (defined('CIVICRM_DB_CACHE_HOST')) {
124 $defaults['host'] = CIVICRM_DB_CACHE_HOST
;
127 if (defined('CIVICRM_DB_CACHE_PORT')) {
128 $defaults['port'] = CIVICRM_DB_CACHE_PORT
;
131 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
132 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT
;
135 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
136 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX
;
143 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
144 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT
;
146 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
147 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX
;
155 * Create a new, named, limited-use cache.
157 * This is a factory function. Generally, you should use Civi::cache($name)
158 * to locate managed cached instance.
160 * @param array $params
162 * - name: string, unique symbolic name.
163 * - type: array|string, list of acceptable cache types, in order of preference.
164 * - prefetch: bool, whether to prefetch all data in cache (if possible).
165 * @return CRM_Utils_Cache_Interface
166 * @throws CRM_Core_Exception
169 public static function create($params = array()) {
170 $types = (array) $params['type'];
172 if (!empty($params['name'])) {
173 $params['name'] = CRM_Core_BAO_Cache
::cleanKey($params['name']);
176 foreach ($types as $type) {
179 if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS
, array('Memcache', 'Memcached', 'Redis'))) {
180 $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS
;
181 $settings = self
::getCacheSettings(CIVICRM_DB_CACHE_CLASS
);
182 $settings['prefix'] = CRM_Utils_Array
::value('prefix', $settings, '') . self
::DELIMITER
. $params['name'] . self
::DELIMITER
;
183 return new $dbCacheClass($settings);
188 if (defined('CIVICRM_DSN') && CIVICRM_DSN
) {
189 return new CRM_Utils_Cache_SqlGroup(array(
190 'group' => $params['name'],
191 'prefetch' => CRM_Utils_Array
::value('prefetch', $params, FALSE),
198 return new CRM_Utils_Cache_ArrayCache(array());
203 throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
207 * Assert that a key is well-formed.
211 * Same $key, if it's valid.
212 * @throws \CRM_Utils_Cache_InvalidArgumentException
214 public static function assertValidKey($key) {
215 $strict = CRM_Utils_Constant
::value('CIVICRM_PSR16_STRICT', FALSE) ||
defined('CIVICRM_TEST');
217 if (!is_string($key)) {
218 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string");
221 if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) {
222 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters");
225 if ($strict && strlen($key) > 255) {
226 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long");
234 * Ex: 'ArrayCache', 'Memcache', 'Redis'.
236 public static function getCacheDriver() {
237 $className = 'ArrayCache'; // default to ArrayCache for now
239 // Maintain backward compatibility for now.
240 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
241 // override the CIVICRM_DB_CACHE_CLASS setting.
242 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
243 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE
) {
244 $className = 'Memcache';
247 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE
) {
248 $className = 'ArrayCache';
251 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS
) {
252 $className = CIVICRM_DB_CACHE_CLASS
;