Fix recent item deduping
[civicrm-core.git] / CRM / Utils / Cache.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
35 * Cache is an empty base object, we'll modify the scheme when we have different caching schemes
6a488035
TO
36 */
37class CRM_Utils_Cache {
118eb830
TO
38
39 const DELIMITER = '/';
40
6a488035 41 /**
64c2ecd4
TO
42 * (Quasi-Private) Treat this as private. It is marked public to facilitate testing.
43 *
6a488035
TO
44 * We only need one instance of this object. So we use the singleton
45 * pattern and cache the instance in this variable
46 *
47 * @var object
6a488035 48 */
64c2ecd4 49 public static $_singleton = NULL;
6a488035
TO
50
51 /**
fe482240 52 * Constructor.
6a488035 53 *
77855840
TO
54 * @param array $config
55 * An array of configuration params.
6a488035 56 *
f4aaa82a 57 * @return \CRM_Utils_Cache
6a488035 58 */
00be9182 59 public function __construct(&$config) {
6a488035
TO
60 CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
61 }
62
63 /**
fe482240 64 * Singleton function used to manage this object.
6a488035 65 *
c039f658 66 * @return CRM_Utils_Cache_Interface
6a488035 67 */
00be9182 68 public static function &singleton() {
6a488035 69 if (self::$_singleton === NULL) {
81487911 70 $className = self::getCacheDriver();
6a488035
TO
71 // a generic method for utilizing any of the available db caches.
72 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
6a488035 73 $settings = self::getCacheSettings($className);
118eb830 74 $settings['prefix'] = CRM_Utils_Array::value('prefix', $settings, '') . self::DELIMITER . 'default' . self::DELIMITER;
6a488035 75 self::$_singleton = new $dbCacheClass($settings);
ee1d325f 76 }
6a488035
TO
77 return self::$_singleton;
78 }
79
80 /**
fe482240 81 * Get cache relevant settings.
6a488035 82 *
77b97be7
EM
83 * @param $cachePlugin
84 *
6a488035
TO
85 * @return array
86 * associative array of settings for the cache
6a488035 87 */
00be9182 88 public static function getCacheSettings($cachePlugin) {
6a488035
TO
89 switch ($cachePlugin) {
90 case 'ArrayCache':
91 case 'NoCache':
92 $defaults = array();
93 break;
838fc623 94
59e56021 95 case 'Redis':
6a488035
TO
96 case 'Memcache':
97 case 'Memcached':
98 $defaults = array(
99 'host' => 'localhost',
100 'port' => 11211,
101 'timeout' => 3600,
102 'prefix' => '',
103 );
104
b44e3f84 105 // Use old constants if needed to ensure backward compatibility
6a488035
TO
106 if (defined('CIVICRM_MEMCACHE_HOST')) {
107 $defaults['host'] = CIVICRM_MEMCACHE_HOST;
108 }
109
110 if (defined('CIVICRM_MEMCACHE_PORT')) {
111 $defaults['port'] = CIVICRM_MEMCACHE_PORT;
112 }
113
114 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
115 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT;
116 }
117
118 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
119 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX;
120 }
121
122 // Use new constants if possible
123 if (defined('CIVICRM_DB_CACHE_HOST')) {
124 $defaults['host'] = CIVICRM_DB_CACHE_HOST;
125 }
126
127 if (defined('CIVICRM_DB_CACHE_PORT')) {
128 $defaults['port'] = CIVICRM_DB_CACHE_PORT;
129 }
130
131 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
132 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
133 }
134
135 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
136 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
137 }
138
139 break;
140
141 case 'APCcache':
142 $defaults = array();
143 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
144 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
145 }
146 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
147 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
148 }
149 break;
150 }
151 return $defaults;
152 }
96025800 153
a4704404
TO
154 /**
155 * Create a new, named, limited-use cache.
156 *
157 * This is a factory function. Generally, you should use Civi::cache($name)
158 * to locate managed cached instance.
159 *
160 * @param array $params
161 * Array with keys:
162 * - name: string, unique symbolic name.
7c34772b
TO
163 * For a naming convention, use `snake_case` or `CamelCase` to maximize
164 * portability/cleanliness. Any other punctuation or whitespace
165 * should function correctly, but it can be harder to inspect/debug.
a4704404
TO
166 * - type: array|string, list of acceptable cache types, in order of preference.
167 * - prefetch: bool, whether to prefetch all data in cache (if possible).
168 * @return CRM_Utils_Cache_Interface
169 * @throws CRM_Core_Exception
170 * @see Civi::cache()
171 */
172 public static function create($params = array()) {
173 $types = (array) $params['type'];
174
76e697a9
CB
175 if (!empty($params['name'])) {
176 $params['name'] = CRM_Core_BAO_Cache::cleanKey($params['name']);
177 }
178
a4704404
TO
179 foreach ($types as $type) {
180 switch ($type) {
a944a143 181 case '*memory*':
eac3023b 182 if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('Memcache', 'Memcached', 'Redis'))) {
a944a143
TO
183 $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS;
184 $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS);
118eb830 185 $settings['prefix'] = CRM_Utils_Array::value('prefix', $settings, '') . self::DELIMITER . $params['name'] . self::DELIMITER;
a944a143
TO
186 return new $dbCacheClass($settings);
187 }
188 break;
189
a4704404
TO
190 case 'SqlGroup':
191 if (defined('CIVICRM_DSN') && CIVICRM_DSN) {
192 return new CRM_Utils_Cache_SqlGroup(array(
193 'group' => $params['name'],
194 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE),
195 ));
196 }
197 break;
198
199 case 'Arraycache':
200 case 'ArrayCache':
201 return new CRM_Utils_Cache_ArrayCache(array());
202
203 }
204 }
205
206 throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
207 }
208
cdee59f7
TO
209 /**
210 * Assert that a key is well-formed.
211 *
212 * @param string $key
213 * @return string
214 * Same $key, if it's valid.
215 * @throws \CRM_Utils_Cache_InvalidArgumentException
216 */
217 public static function assertValidKey($key) {
218 $strict = CRM_Utils_Constant::value('CIVICRM_PSR16_STRICT', FALSE) || defined('CIVICRM_TEST');
219
220 if (!is_string($key)) {
221 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string");
222 }
223
224 if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) {
225 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters");
226 }
227
228 if ($strict && strlen($key) > 255) {
229 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long");
230 }
231
232 return $key;
233 }
234
81487911
TO
235 /**
236 * @return string
237 * Ex: 'ArrayCache', 'Memcache', 'Redis'.
238 */
239 public static function getCacheDriver() {
240 $className = 'ArrayCache'; // default to ArrayCache for now
241
242 // Maintain backward compatibility for now.
243 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
244 // override the CIVICRM_DB_CACHE_CLASS setting.
245 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
246 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) {
247 $className = 'Memcache';
248 return $className;
249 }
250 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) {
251 $className = 'ArrayCache';
252 return $className;
253 }
254 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) {
255 $className = CIVICRM_DB_CACHE_CLASS;
256 return $className;
257 }
258 return $className;
259 }
260
6a488035 261}