Merge pull request #16541 from bhahumanists/subtype-issue-991
[civicrm-core.git] / CRM / Core / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * BAO object for civicrm_cache table.
14 *
15 * This is a database cache and is persisted across sessions. Typically we use
16 * this to store meta data (like profile fields, custom fields etc).
17 *
18 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
19 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
20 * a specific group if needed.
21 *
22 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
23 * the unique form name for each form (per user)
24 */
25 class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
26
27 /**
28 * When store session/form state, how long should the data be retained?
29 *
30 * Default is Two days: 2*24*60*60
31 *
32 * @var int, number of second
33 */
34 const DEFAULT_SESSION_TTL = 172800;
35
36 /**
37 * Cache.
38 *
39 * Format is ($cacheKey => $cacheValue)
40 *
41 * @var array
42 */
43 public static $_cache = NULL;
44
45 /**
46 * Retrieve an item from the DB cache.
47 *
48 * @param string $group
49 * (required) The group name of the item.
50 * @param string $path
51 * (required) The path under which this item is stored.
52 * @param int $componentID
53 * The optional component ID (so componenets can share the same name space).
54 *
55 * @return object
56 * The data if present in cache, else null
57 * @deprecated
58 */
59 public static function &getItem($group, $path, $componentID = NULL) {
60 CRM_Core_Error::deprecatedFunctionWarning(
61 'CRM_Core_BAO_Cache::getItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
62 );
63 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
64 $value = $adapter::getItem($group, $path, $componentID);
65 return $value;
66 }
67
68 if (self::$_cache === NULL) {
69 self::$_cache = [];
70 }
71
72 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
73 if (!array_key_exists($argString, self::$_cache)) {
74 $cache = CRM_Utils_Cache::singleton();
75 $cleanKey = self::cleanKey($argString);
76 self::$_cache[$argString] = $cache->get($cleanKey);
77 if (self::$_cache[$argString] === NULL) {
78 $table = self::getTableName();
79 $where = self::whereCache($group, $path, $componentID);
80 $rawData = CRM_Core_DAO::singleValueQuery("SELECT data FROM $table WHERE $where");
81 $data = $rawData ? self::decode($rawData) : NULL;
82
83 self::$_cache[$argString] = $data;
84 if ($data !== NULL) {
85 // Do not cache 'null' as that is most likely a cache miss & we shouldn't then cache it.
86 $cache->set($cleanKey, self::$_cache[$argString]);
87 }
88 }
89 }
90 return self::$_cache[$argString];
91 }
92
93 /**
94 * Retrieve all items in a group.
95 *
96 * @param string $group
97 * (required) The group name of the item.
98 * @param int $componentID
99 * The optional component ID (so componenets can share the same name space).
100 *
101 * @return object
102 * The data if present in cache, else null
103 * @deprecated
104 */
105 public static function &getItems($group, $componentID = NULL) {
106 CRM_Core_Error::deprecatedFunctionWarning(
107 'CRM_Core_BAO_Cache::getItems is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
108 );
109 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
110 return $adapter::getItems($group, $componentID);
111 }
112
113 if (self::$_cache === NULL) {
114 self::$_cache = [];
115 }
116
117 $argString = "CRM_CT_CI_{$group}_{$componentID}";
118 if (!array_key_exists($argString, self::$_cache)) {
119 $cache = CRM_Utils_Cache::singleton();
120 $cleanKey = self::cleanKey($argString);
121 self::$_cache[$argString] = $cache->get($cleanKey);
122 if (!self::$_cache[$argString]) {
123 $table = self::getTableName();
124 $where = self::whereCache($group, NULL, $componentID);
125 $dao = CRM_Core_DAO::executeQuery("SELECT path, data FROM $table WHERE $where");
126
127 $result = [];
128 while ($dao->fetch()) {
129 $result[$dao->path] = self::decode($dao->data);
130 }
131
132 self::$_cache[$argString] = $result;
133 $cache->set($cleanKey, self::$_cache[$argString]);
134 }
135 }
136
137 return self::$_cache[$argString];
138 }
139
140 /**
141 * Store an item in the DB cache.
142 *
143 * @param object $data
144 * (required) A reference to the data that will be serialized and stored.
145 * @param string $group
146 * (required) The group name of the item.
147 * @param string $path
148 * (required) The path under which this item is stored.
149 * @param int $componentID
150 * The optional component ID (so componenets can share the same name space).
151 * @deprecated
152 * @throws CRM_Core_Exception
153 */
154 public static function setItem(&$data, $group, $path, $componentID = NULL) {
155 CRM_Core_Error::deprecatedFunctionWarning(
156 'CRM_Core_BAO_Cache::setItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
157 );
158 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
159 return $adapter::setItem($data, $group, $path, $componentID);
160 }
161
162 if (self::$_cache === NULL) {
163 self::$_cache = [];
164 }
165
166 // get a lock so that multiple ajax requests on the same page
167 // dont trample on each other
168 // CRM-11234
169 $lock = Civi::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
170 if (!$lock->isAcquired()) {
171 throw new CRM_Core_Exception('Cannot acquire database lock');
172 }
173
174 $table = self::getTableName();
175 $where = self::whereCache($group, $path, $componentID);
176 $dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM $table WHERE {$where}");
177 // FIXME - Use SQL NOW() or CRM_Utils_Time?
178 $now = date('Y-m-d H:i:s');
179 $dataSerialized = self::encode($data);
180
181 // This table has a wonky index, so we cannot use REPLACE or
182 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
183 if ($dataExists) {
184 $sql = "UPDATE $table SET data = %1, created_date = %2 WHERE {$where}";
185 $args = [
186 1 => [$dataSerialized, 'String'],
187 2 => [$now, 'String'],
188 ];
189 $dao = CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
190 }
191 else {
192 $insert = CRM_Utils_SQL_Insert::into($table)
193 ->row([
194 'group_name' => $group,
195 'path' => $path,
196 'component_id' => $componentID,
197 'data' => $dataSerialized,
198 'created_date' => $now,
199 ]);
200 $dao = CRM_Core_DAO::executeQuery($insert->toSQL(), [], TRUE, NULL, FALSE, FALSE);
201 }
202
203 $lock->release();
204
205 // cache coherency - refresh or remove dependent caches
206
207 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
208 $cache = CRM_Utils_Cache::singleton();
209 $data = self::decode($dataSerialized);
210 self::$_cache[$argString] = $data;
211 $cache->set(self::cleanKey($argString), $data);
212
213 $argString = "CRM_CT_CI_{$group}_{$componentID}";
214 unset(self::$_cache[$argString]);
215 $cache->delete(self::cleanKey($argString));
216 }
217
218 /**
219 * Delete all the cache elements that belong to a group OR delete the entire cache if group is not specified.
220 *
221 * @param string $group
222 * The group name of the entries to be deleted.
223 * @param string $path
224 * Path of the item that needs to be deleted.
225 * @param bool $clearAll clear all caches
226 * @deprecated
227 */
228 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
229 CRM_Core_Error::deprecatedFunctionWarning(
230 'CRM_Core_BAO_Cache::deleteGroup is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
231 );
232 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
233 return $adapter::deleteGroup($group, $path);
234 }
235 else {
236 $table = self::getTableName();
237 $where = self::whereCache($group, $path, NULL);
238 CRM_Core_DAO::executeQuery("DELETE FROM $table WHERE $where");
239 }
240
241 if ($clearAll) {
242 self::resetCaches();
243 }
244 }
245
246 /**
247 * Cleanup ACL and System Level caches
248 */
249 public static function resetCaches() {
250 // also reset ACL Cache
251 // @todo why is this called when CRM_Utils_System::flushCache() does it as well.
252 CRM_ACL_BAO_Cache::resetCache();
253
254 // also reset memory cache if any
255 CRM_Utils_System::flushCache();
256 }
257
258 /**
259 * The next two functions are internal functions used to store and retrieve session from
260 * the database cache. This keeps the session to a limited size and allows us to
261 * create separate session scopes for each form in a tab
262 */
263
264 /**
265 * This function takes entries from the session array and stores it in the cache.
266 *
267 * It also deletes the entries from the $_SESSION object (for a smaller session size)
268 *
269 * @param array $names
270 * Array of session values that should be persisted.
271 * This is either a form name + qfKey or just a form name
272 * (in the case of profile)
273 * @param bool $resetSession
274 * Should session state be reset on completion of DB store?.
275 */
276 public static function storeSessionToCache($names, $resetSession = TRUE) {
277 foreach ($names as $key => $sessionName) {
278 if (is_array($sessionName)) {
279 $value = NULL;
280 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
281 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
282 }
283 $key = "{$sessionName[0]}_{$sessionName[1]}";
284 Civi::cache('session')->set($key, $value, self::pickSessionTtl($key));
285 if ($resetSession) {
286 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
287 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
288 }
289 }
290 else {
291 $value = NULL;
292 if (!empty($_SESSION[$sessionName])) {
293 $value = $_SESSION[$sessionName];
294 }
295 Civi::cache('session')->set($sessionName, $value, self::pickSessionTtl($sessionName));
296 if ($resetSession) {
297 $_SESSION[$sessionName] = NULL;
298 unset($_SESSION[$sessionName]);
299 }
300 }
301 }
302
303 self::cleanup();
304 }
305
306 /* Retrieve the session values from the cache and populate the $_SESSION array
307 *
308 * @param array $names
309 * Array of session values that should be persisted.
310 * This is either a form name + qfKey or just a form name
311 * (in the case of profile)
312 */
313
314 /**
315 * Restore session from cache.
316 *
317 * @param string $names
318 */
319 public static function restoreSessionFromCache($names) {
320 foreach ($names as $key => $sessionName) {
321 if (is_array($sessionName)) {
322 $value = Civi::cache('session')->get("{$sessionName[0]}_{$sessionName[1]}");
323 if ($value) {
324 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
325 }
326 }
327 else {
328 $value = Civi::cache('session')->get($sessionName);
329 if ($value) {
330 $_SESSION[$sessionName] = $value;
331 }
332 }
333 }
334 }
335
336 /**
337 * Determine how long session-state should be retained.
338 *
339 * @param string $sessionKey
340 * Ex: '_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654_container'
341 * Ex: 'CiviCRM_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654'
342 * @return int
343 * Number of seconds.
344 */
345 protected static function pickSessionTtl($sessionKey) {
346 $secureSessionTimeoutMinutes = (int) Civi::settings()->get('secure_cache_timeout_minutes');
347 if ($secureSessionTimeoutMinutes) {
348 $transactionPages = [
349 'CRM_Contribute_Controller_Contribution',
350 'CRM_Event_Controller_Registration',
351 ];
352 foreach ($transactionPages as $transactionPage) {
353 if (strpos($sessionKey, $transactionPage) !== FALSE) {
354 return $secureSessionTimeoutMinutes * 60;
355 }
356 }
357 }
358
359 return self::DEFAULT_SESSION_TTL;
360 }
361
362 /**
363 * Do periodic cleanup of the CiviCRM session table.
364 *
365 * Also delete all session cache entries which are a couple of days old.
366 * This keeps the session cache to a manageable size
367 * Delete Contribution page session caches more energetically.
368 *
369 * @param bool $session
370 * @param bool $table
371 * @param bool $prevNext
372 * @param bool $expired
373 */
374 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE, $expired = FALSE) {
375 // clean up the session cache every $cacheCleanUpNumber probabilistically
376 $cleanUpNumber = 757;
377
378 // clean up all sessions older than $cacheTimeIntervalDays days
379 $timeIntervalDays = 2;
380
381 if (mt_rand(1, 100000) % $cleanUpNumber == 0) {
382 $expired = $session = $table = $prevNext = TRUE;
383 }
384
385 if (!$session && !$table && !$prevNext && !$expired) {
386 return;
387 }
388
389 if ($prevNext) {
390 // delete all PrevNext caches
391 Civi::service('prevnext')->cleanup();
392 }
393
394 if ($table) {
395 CRM_Core_Config::clearTempTables($timeIntervalDays . ' day');
396 }
397
398 if ($session) {
399 // Session caches are just regular caches, so they expire naturally per TTL.
400 $expired = TRUE;
401 }
402
403 if ($expired) {
404 $sql = "DELETE FROM civicrm_cache WHERE expired_date < %1";
405 $params = [
406 1 => [date(CRM_Utils_Cache_SqlGroup::TS_FMT, CRM_Utils_Time::getTimeRaw()), 'String'],
407 ];
408 CRM_Core_DAO::executeQuery($sql, $params);
409 }
410 }
411
412 /**
413 * (Quasi-private) Encode an object/array/string/int as a string.
414 *
415 * @param $mixed
416 * @return string
417 */
418 public static function encode($mixed) {
419 return base64_encode(serialize($mixed));
420 }
421
422 /**
423 * (Quasi-private) Decode an object/array/string/int from a string.
424 *
425 * @param $string
426 * @return mixed
427 */
428 public static function decode($string) {
429 // Upgrade support -- old records (serialize) always have this punctuation,
430 // and new records (base64) never do.
431 if (strpos($string, ':') !== FALSE || strpos($string, ';') !== FALSE) {
432 return unserialize($string);
433 }
434 else {
435 return unserialize(base64_decode($string));
436 }
437 }
438
439 /**
440 * Compose a SQL WHERE clause for the cache.
441 *
442 * Note: We need to use the cache during bootstrap, so we don't have
443 * full access to DAO services.
444 *
445 * @param string $group
446 * @param string|null $path
447 * Filter by path. If NULL, then return any paths.
448 * @param int|null $componentID
449 * Filter by component. If NULL, then look for explicitly NULL records.
450 * @return string
451 */
452 protected static function whereCache($group, $path, $componentID) {
453 $clauses = [];
454 $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($group) . '"');
455 if ($path) {
456 $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"');
457 }
458 if ($componentID && is_numeric($componentID)) {
459 $clauses[] = ('component_id = ' . (int) $componentID);
460 }
461 return $clauses ? implode(' AND ', $clauses) : '(1)';
462 }
463
464 /**
465 * Normalize a cache key.
466 *
467 * This bridges an impedance mismatch between our traditional caching
468 * and PSR-16 -- PSR-16 accepts a narrower range of cache keys.
469 *
470 * @param string $key
471 * Ex: 'ab/cd:ef'
472 * @return string
473 * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'.
474 * A similar key, but suitable for use with PSR-16-compliant cache providers.
475 * @deprecated
476 * @see CRM_Utils_Cache::cleanKey()
477 */
478 public static function cleanKey($key) {
479 return CRM_Utils_Cache::cleanKey($key);
480 }
481
482 }