Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
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 | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
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 | */ |
37 | class 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 |
309310bf | 58 | * @throws \CRM_Core_Exception |
6a488035 | 59 | */ |
00be9182 | 60 | public function __construct(&$config) { |
309310bf | 61 | throw new CRM_Core_Exception(ts('this is just an interface and should not be called directly')); |
6a488035 TO |
62 | } |
63 | ||
64 | /** | |
fe482240 | 65 | * Singleton function used to manage this object. |
6a488035 | 66 | * |
c039f658 | 67 | * @return CRM_Utils_Cache_Interface |
6a488035 | 68 | */ |
00be9182 | 69 | public static function &singleton() { |
6a488035 | 70 | if (self::$_singleton === NULL) { |
81487911 | 71 | $className = self::getCacheDriver(); |
6a488035 TO |
72 | // a generic method for utilizing any of the available db caches. |
73 | $dbCacheClass = 'CRM_Utils_Cache_' . $className; | |
6a488035 | 74 | $settings = self::getCacheSettings($className); |
118eb830 | 75 | $settings['prefix'] = CRM_Utils_Array::value('prefix', $settings, '') . self::DELIMITER . 'default' . self::DELIMITER; |
6a488035 | 76 | self::$_singleton = new $dbCacheClass($settings); |
ee1d325f | 77 | } |
6a488035 TO |
78 | return self::$_singleton; |
79 | } | |
80 | ||
81 | /** | |
fe482240 | 82 | * Get cache relevant settings. |
6a488035 | 83 | * |
77b97be7 EM |
84 | * @param $cachePlugin |
85 | * | |
6a488035 TO |
86 | * @return array |
87 | * associative array of settings for the cache | |
6a488035 | 88 | */ |
00be9182 | 89 | public static function getCacheSettings($cachePlugin) { |
6a488035 TO |
90 | switch ($cachePlugin) { |
91 | case 'ArrayCache': | |
92 | case 'NoCache': | |
be2fb01f | 93 | $defaults = []; |
6a488035 | 94 | break; |
838fc623 | 95 | |
59e56021 | 96 | case 'Redis': |
6a488035 TO |
97 | case 'Memcache': |
98 | case 'Memcached': | |
be2fb01f | 99 | $defaults = [ |
6a488035 TO |
100 | 'host' => 'localhost', |
101 | 'port' => 11211, | |
102 | 'timeout' => 3600, | |
103 | 'prefix' => '', | |
be2fb01f | 104 | ]; |
6a488035 | 105 | |
b44e3f84 | 106 | // Use old constants if needed to ensure backward compatibility |
6a488035 TO |
107 | if (defined('CIVICRM_MEMCACHE_HOST')) { |
108 | $defaults['host'] = CIVICRM_MEMCACHE_HOST; | |
109 | } | |
110 | ||
111 | if (defined('CIVICRM_MEMCACHE_PORT')) { | |
112 | $defaults['port'] = CIVICRM_MEMCACHE_PORT; | |
113 | } | |
114 | ||
115 | if (defined('CIVICRM_MEMCACHE_TIMEOUT')) { | |
116 | $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT; | |
117 | } | |
118 | ||
119 | if (defined('CIVICRM_MEMCACHE_PREFIX')) { | |
120 | $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX; | |
121 | } | |
122 | ||
123 | // Use new constants if possible | |
124 | if (defined('CIVICRM_DB_CACHE_HOST')) { | |
125 | $defaults['host'] = CIVICRM_DB_CACHE_HOST; | |
126 | } | |
127 | ||
128 | if (defined('CIVICRM_DB_CACHE_PORT')) { | |
129 | $defaults['port'] = CIVICRM_DB_CACHE_PORT; | |
130 | } | |
131 | ||
132 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { | |
133 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
134 | } | |
135 | ||
136 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
137 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
138 | } | |
139 | ||
140 | break; | |
141 | ||
142 | case 'APCcache': | |
be2fb01f | 143 | $defaults = []; |
6a488035 TO |
144 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { |
145 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
146 | } | |
147 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
148 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
149 | } | |
150 | break; | |
151 | } | |
152 | return $defaults; | |
153 | } | |
96025800 | 154 | |
a4704404 TO |
155 | /** |
156 | * Create a new, named, limited-use cache. | |
157 | * | |
158 | * This is a factory function. Generally, you should use Civi::cache($name) | |
159 | * to locate managed cached instance. | |
160 | * | |
161 | * @param array $params | |
162 | * Array with keys: | |
163 | * - name: string, unique symbolic name. | |
7c34772b TO |
164 | * For a naming convention, use `snake_case` or `CamelCase` to maximize |
165 | * portability/cleanliness. Any other punctuation or whitespace | |
166 | * should function correctly, but it can be harder to inspect/debug. | |
a4704404 TO |
167 | * - type: array|string, list of acceptable cache types, in order of preference. |
168 | * - prefetch: bool, whether to prefetch all data in cache (if possible). | |
84413eca TO |
169 | * - withArray: bool|null|'fast', whether to setup a thread-local array-cache in front of the cache driver. |
170 | * Note that cache-values may be passed to the underlying driver with extra metadata, | |
171 | * so this will slightly change/enlarge the on-disk format. | |
172 | * Support varies by driver: | |
173 | * - For most memory backed caches, this option is meaningful. | |
174 | * - For SqlGroup, this option is ignored. SqlGroup has equivalent behavior built-in. | |
c33f1df1 | 175 | * - For ArrayCache, this option is ignored. It's redundant. |
84413eca TO |
176 | * If this is a short-lived process in which TTL's don't matter, you might |
177 | * use 'fast' mode. It sacrifices some PSR-16 compliance and cache-coherency | |
178 | * protections to improve performance. | |
a4704404 TO |
179 | * @return CRM_Utils_Cache_Interface |
180 | * @throws CRM_Core_Exception | |
181 | * @see Civi::cache() | |
182 | */ | |
be2fb01f | 183 | public static function create($params = []) { |
a4704404 TO |
184 | $types = (array) $params['type']; |
185 | ||
76e697a9 | 186 | if (!empty($params['name'])) { |
77f080cb | 187 | $params['name'] = self::cleanKey($params['name']); |
76e697a9 CB |
188 | } |
189 | ||
a4704404 TO |
190 | foreach ($types as $type) { |
191 | switch ($type) { | |
a944a143 | 192 | case '*memory*': |
be2fb01f | 193 | if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, ['Memcache', 'Memcached', 'Redis'])) { |
a944a143 TO |
194 | $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS; |
195 | $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS); | |
118eb830 | 196 | $settings['prefix'] = CRM_Utils_Array::value('prefix', $settings, '') . self::DELIMITER . $params['name'] . self::DELIMITER; |
84413eca TO |
197 | $cache = new $dbCacheClass($settings); |
198 | if (!empty($params['withArray'])) { | |
199 | $cache = $params['withArray'] === 'fast' ? new CRM_Utils_Cache_FastArrayDecorator($cache) : new CRM_Utils_Cache_ArrayDecorator($cache); | |
200 | } | |
201 | return $cache; | |
a944a143 TO |
202 | } |
203 | break; | |
204 | ||
a4704404 TO |
205 | case 'SqlGroup': |
206 | if (defined('CIVICRM_DSN') && CIVICRM_DSN) { | |
be2fb01f | 207 | return new CRM_Utils_Cache_SqlGroup([ |
a4704404 TO |
208 | 'group' => $params['name'], |
209 | 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE), | |
be2fb01f | 210 | ]); |
a4704404 TO |
211 | } |
212 | break; | |
213 | ||
214 | case 'Arraycache': | |
215 | case 'ArrayCache': | |
be2fb01f | 216 | return new CRM_Utils_Cache_ArrayCache([]); |
a4704404 TO |
217 | |
218 | } | |
219 | } | |
220 | ||
221 | throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1)); | |
222 | } | |
223 | ||
77f080cb TO |
224 | /** |
225 | * Normalize a cache key. | |
226 | * | |
227 | * This bridges an impedance mismatch between our traditional caching | |
228 | * and PSR-16 -- PSR-16 accepts a narrower range of cache keys. | |
229 | * | |
230 | * @param string $key | |
231 | * Ex: 'ab/cd:ef' | |
232 | * @return string | |
233 | * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'. | |
234 | * A similar key, but suitable for use with PSR-16-compliant cache providers. | |
235 | */ | |
236 | public static function cleanKey($key) { | |
237 | if (!is_string($key) && !is_int($key)) { | |
238 | throw new \RuntimeException("Malformed cache key"); | |
239 | } | |
240 | ||
241 | $maxLen = 64; | |
242 | $escape = '-'; | |
243 | ||
244 | if (strlen($key) >= $maxLen) { | |
245 | return $escape . md5($key); | |
246 | } | |
247 | ||
248 | $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) { | |
249 | return $escape . dechex(ord($m[0])); | |
250 | }, $key); | |
251 | ||
252 | return strlen($r) >= $maxLen ? $escape . md5($key) : $r; | |
253 | } | |
254 | ||
cdee59f7 TO |
255 | /** |
256 | * Assert that a key is well-formed. | |
257 | * | |
258 | * @param string $key | |
259 | * @return string | |
260 | * Same $key, if it's valid. | |
261 | * @throws \CRM_Utils_Cache_InvalidArgumentException | |
262 | */ | |
263 | public static function assertValidKey($key) { | |
264 | $strict = CRM_Utils_Constant::value('CIVICRM_PSR16_STRICT', FALSE) || defined('CIVICRM_TEST'); | |
265 | ||
266 | if (!is_string($key)) { | |
267 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string"); | |
268 | } | |
269 | ||
270 | if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) { | |
271 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters"); | |
272 | } | |
273 | ||
274 | if ($strict && strlen($key) > 255) { | |
275 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long"); | |
276 | } | |
277 | ||
278 | return $key; | |
279 | } | |
280 | ||
81487911 TO |
281 | /** |
282 | * @return string | |
283 | * Ex: 'ArrayCache', 'Memcache', 'Redis'. | |
284 | */ | |
285 | public static function getCacheDriver() { | |
6714d8d2 SL |
286 | // default to ArrayCache for now |
287 | $className = 'ArrayCache'; | |
81487911 TO |
288 | |
289 | // Maintain backward compatibility for now. | |
290 | // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will | |
291 | // override the CIVICRM_DB_CACHE_CLASS setting. | |
292 | // Going forward, CIVICRM_USE_xxxCACHE should be deprecated. | |
293 | if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) { | |
294 | $className = 'Memcache'; | |
295 | return $className; | |
296 | } | |
297 | elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) { | |
298 | $className = 'ArrayCache'; | |
299 | return $className; | |
300 | } | |
301 | elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) { | |
302 | $className = CIVICRM_DB_CACHE_CLASS; | |
303 | return $className; | |
304 | } | |
305 | return $className; | |
306 | } | |
307 | ||
e0ba77f3 TO |
308 | /** |
309 | * Generate a unique negative-acknowledgement token (NACK). | |
310 | * | |
311 | * When using PSR-16 to read a value, the `$cahce->get()` will a return a default | |
312 | * value on cache-miss, so it's hard to know if you've gotten a geniune value | |
313 | * from the cache or just a default. If you're in an edge-case where it matters | |
314 | * (and you want to do has()+get() in a single roundtrip), use the nack() as | |
315 | * the default: | |
316 | * | |
317 | * $nack = CRM_Utils_Cache::nack(); | |
318 | * $value = $cache->get('foo', $nack); | |
319 | * echo ($value === $nack) ? "Cache has a value, and we got it" : "Cache has no value". | |
320 | * | |
89861474 | 321 | * The value should be unique to avoid accidental matches. |
e0ba77f3 TO |
322 | * |
323 | * @return string | |
324 | * Unique nonce value indicating a "negative acknowledgement" (failed read). | |
325 | * If we need to accurately perform has($key)+get($key), we can | |
326 | * use `get($key,$nack)`. | |
327 | */ | |
328 | public static function nack() { | |
89861474 TO |
329 | $st =& Civi::$statics[__CLASS__]; |
330 | if (!isset($st['nack-c'])) { | |
331 | $st['nack-c'] = md5(CRM_Utils_Request::id() . CIVICRM_SITE_KEY . CIVICRM_DSN . mt_rand(0, 10000)); | |
332 | $st['nack-i'] = 0; | |
e0ba77f3 | 333 | } |
89861474 | 334 | return 'NACK:' . $st['nack-c'] . $st['nack-i']++; |
e0ba77f3 TO |
335 | } |
336 | ||
6a488035 | 337 | } |