3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
29 * BAO object for civicrm_cache table.
31 * This is a database cache and is persisted across sessions. Typically we use
32 * this to store meta data (like profile fields, custom fields etc).
34 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
35 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
36 * a specific group if needed.
38 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
39 * the unique form name for each form (per user)
41 class CRM_Core_BAO_Cache
extends CRM_Core_DAO_Cache
{
44 * When store session/form state, how long should the data be retained?
46 * Default is Two days: 2*24*60*60
48 * @var int, number of second
50 const DEFAULT_SESSION_TTL
= 172800;
55 * Format is ($cacheKey => $cacheValue)
59 public static $_cache = NULL;
62 * Retrieve an item from the DB cache.
64 * @param string $group
65 * (required) The group name of the item.
67 * (required) The path under which this item is stored.
68 * @param int $componentID
69 * The optional component ID (so componenets can share the same name space).
72 * The data if present in cache, else null
75 public static function &getItem($group, $path, $componentID = NULL) {
76 if (($adapter = CRM_Utils_Constant
::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
77 $value = $adapter::getItem($group, $path, $componentID);
81 if (self
::$_cache === NULL) {
85 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
86 if (!array_key_exists($argString, self
::$_cache)) {
87 $cache = CRM_Utils_Cache
::singleton();
88 $cleanKey = self
::cleanKey($argString);
89 self
::$_cache[$argString] = $cache->get($cleanKey);
90 if (self
::$_cache[$argString] === NULL) {
91 $table = self
::getTableName();
92 $where = self
::whereCache($group, $path, $componentID);
93 $rawData = CRM_Core_DAO
::singleValueQuery("SELECT data FROM $table WHERE $where");
94 $data = $rawData ? self
::decode($rawData) : NULL;
96 self
::$_cache[$argString] = $data;
98 // Do not cache 'null' as that is most likely a cache miss & we shouldn't then cache it.
99 $cache->set($cleanKey, self
::$_cache[$argString]);
103 return self
::$_cache[$argString];
107 * Retrieve all items in a group.
109 * @param string $group
110 * (required) The group name of the item.
111 * @param int $componentID
112 * The optional component ID (so componenets can share the same name space).
115 * The data if present in cache, else null
118 public static function &getItems($group, $componentID = NULL) {
119 if (($adapter = CRM_Utils_Constant
::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
120 return $adapter::getItems($group, $componentID);
123 if (self
::$_cache === NULL) {
127 $argString = "CRM_CT_CI_{$group}_{$componentID}";
128 if (!array_key_exists($argString, self
::$_cache)) {
129 $cache = CRM_Utils_Cache
::singleton();
130 $cleanKey = self
::cleanKey($argString);
131 self
::$_cache[$argString] = $cache->get($cleanKey);
132 if (!self
::$_cache[$argString]) {
133 $table = self
::getTableName();
134 $where = self
::whereCache($group, NULL, $componentID);
135 $dao = CRM_Core_DAO
::executeQuery("SELECT path, data FROM $table WHERE $where");
138 while ($dao->fetch()) {
139 $result[$dao->path
] = self
::decode($dao->data
);
142 self
::$_cache[$argString] = $result;
143 $cache->set($cleanKey, self
::$_cache[$argString]);
147 return self
::$_cache[$argString];
151 * Store an item in the DB cache.
153 * @param object $data
154 * (required) A reference to the data that will be serialized and stored.
155 * @param string $group
156 * (required) The group name of the item.
157 * @param string $path
158 * (required) The path under which this item is stored.
159 * @param int $componentID
160 * The optional component ID (so componenets can share the same name space).
163 public static function setItem(&$data, $group, $path, $componentID = NULL) {
164 if (($adapter = CRM_Utils_Constant
::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
165 return $adapter::setItem($data, $group, $path, $componentID);
168 if (self
::$_cache === NULL) {
172 // get a lock so that multiple ajax requests on the same page
173 // dont trample on each other
175 $lock = Civi
::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
176 if (!$lock->isAcquired()) {
177 CRM_Core_Error
::fatal();
180 $table = self
::getTableName();
181 $where = self
::whereCache($group, $path, $componentID);
182 $dataExists = CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM $table WHERE {$where}");
183 // FIXME - Use SQL NOW() or CRM_Utils_Time?
184 $now = date('Y-m-d H:i:s');
185 $dataSerialized = self
::encode($data);
187 // This table has a wonky index, so we cannot use REPLACE or
188 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
190 $sql = "UPDATE $table SET data = %1, created_date = %2 WHERE {$where}";
192 1 => [$dataSerialized, 'String'],
193 2 => [$now, 'String'],
195 $dao = CRM_Core_DAO
::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
198 $insert = CRM_Utils_SQL_Insert
::into($table)
200 'group_name' => $group,
202 'component_id' => $componentID,
203 'data' => $dataSerialized,
204 'created_date' => $now,
206 $dao = CRM_Core_DAO
::executeQuery($insert->toSQL(), [], TRUE, NULL, FALSE, FALSE);
211 // cache coherency - refresh or remove dependent caches
213 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
214 $cache = CRM_Utils_Cache
::singleton();
215 $data = self
::decode($dataSerialized);
216 self
::$_cache[$argString] = $data;
217 $cache->set(self
::cleanKey($argString), $data);
219 $argString = "CRM_CT_CI_{$group}_{$componentID}";
220 unset(self
::$_cache[$argString]);
221 $cache->delete(self
::cleanKey($argString));
225 * Delete all the cache elements that belong to a group OR delete the entire cache if group is not specified.
227 * @param string $group
228 * The group name of the entries to be deleted.
229 * @param string $path
230 * Path of the item that needs to be deleted.
231 * @param bool $clearAll clear all caches
234 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
235 if (($adapter = CRM_Utils_Constant
::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
236 return $adapter::deleteGroup($group, $path);
239 $table = self
::getTableName();
240 $where = self
::whereCache($group, $path, NULL);
241 CRM_Core_DAO
::executeQuery("DELETE FROM $table WHERE $where");
245 // also reset ACL Cache
246 CRM_ACL_BAO_Cache
::resetCache();
248 // also reset memory cache if any
249 CRM_Utils_System
::flushCache();
254 * The next two functions are internal functions used to store and retrieve session from
255 * the database cache. This keeps the session to a limited size and allows us to
256 * create separate session scopes for each form in a tab
260 * This function takes entries from the session array and stores it in the cache.
262 * It also deletes the entries from the $_SESSION object (for a smaller session size)
264 * @param array $names
265 * Array of session values that should be persisted.
266 * This is either a form name + qfKey or just a form name
267 * (in the case of profile)
268 * @param bool $resetSession
269 * Should session state be reset on completion of DB store?.
271 public static function storeSessionToCache($names, $resetSession = TRUE) {
272 foreach ($names as $key => $sessionName) {
273 if (is_array($sessionName)) {
275 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
276 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
278 $key = "{$sessionName[0]}_{$sessionName[1]}";
279 Civi
::cache('session')->set($key, $value, self
::pickSessionTtl($key));
281 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
282 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
287 if (!empty($_SESSION[$sessionName])) {
288 $value = $_SESSION[$sessionName];
290 Civi
::cache('session')->set($sessionName, $value, self
::pickSessionTtl($sessionName));
292 $_SESSION[$sessionName] = NULL;
293 unset($_SESSION[$sessionName]);
301 /* Retrieve the session values from the cache and populate the $_SESSION array
303 * @param array $names
304 * Array of session values that should be persisted.
305 * This is either a form name + qfKey or just a form name
306 * (in the case of profile)
310 * Restore session from cache.
312 * @param string $names
314 public static function restoreSessionFromCache($names) {
315 foreach ($names as $key => $sessionName) {
316 if (is_array($sessionName)) {
317 $value = Civi
::cache('session')->get("{$sessionName[0]}_{$sessionName[1]}");
319 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
323 $value = Civi
::cache('session')->get($sessionName);
325 $_SESSION[$sessionName] = $value;
332 * Determine how long session-state should be retained.
334 * @param string $sessionKey
335 * Ex: '_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654_container'
336 * Ex: 'CiviCRM_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654'
340 protected static function pickSessionTtl($sessionKey) {
341 $secureSessionTimeoutMinutes = (int) Civi
::settings()->get('secure_cache_timeout_minutes');
342 if ($secureSessionTimeoutMinutes) {
343 $transactionPages = [
344 'CRM_Contribute_Controller_Contribution',
345 'CRM_Event_Controller_Registration',
347 foreach ($transactionPages as $transactionPage) {
348 if (strpos($sessionKey, $transactionPage) !== FALSE) {
349 return $secureSessionTimeoutMinutes * 60;
354 return self
::DEFAULT_SESSION_TTL
;
358 * Do periodic cleanup of the CiviCRM session table.
360 * Also delete all session cache entries which are a couple of days old.
361 * This keeps the session cache to a manageable size
362 * Delete Contribution page session caches more energetically.
364 * @param bool $session
366 * @param bool $prevNext
367 * @param bool $expired
369 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE, $expired = FALSE) {
370 // clean up the session cache every $cacheCleanUpNumber probabilistically
371 $cleanUpNumber = 757;
373 // clean up all sessions older than $cacheTimeIntervalDays days
374 $timeIntervalDays = 2;
376 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
377 $expired = $session = $table = $prevNext = TRUE;
380 if (!$session && !$table && !$prevNext && !$expired) {
385 // delete all PrevNext caches
386 CRM_Core_BAO_PrevNextCache
::cleanupCache();
390 CRM_Core_Config
::clearTempTables($timeIntervalDays . ' day');
394 // Session caches are just regular caches, so they expire naturally per TTL.
399 $sql = "DELETE FROM civicrm_cache WHERE expired_date < %1";
401 1 => [date(CRM_Utils_Cache_SqlGroup
::TS_FMT
, CRM_Utils_Time
::getTimeRaw()), 'String'],
403 CRM_Core_DAO
::executeQuery($sql, $params);
408 * (Quasi-private) Encode an object/array/string/int as a string.
413 public static function encode($mixed) {
414 return base64_encode(serialize($mixed));
418 * (Quasi-private) Decode an object/array/string/int from a string.
423 public static function decode($string) {
424 // Upgrade support -- old records (serialize) always have this punctuation,
425 // and new records (base64) never do.
426 if (strpos($string, ':') !== FALSE ||
strpos($string, ';') !== FALSE) {
427 return unserialize($string);
430 return unserialize(base64_decode($string));
435 * Compose a SQL WHERE clause for the cache.
437 * Note: We need to use the cache during bootstrap, so we don't have
438 * full access to DAO services.
440 * @param string $group
441 * @param string|null $path
442 * Filter by path. If NULL, then return any paths.
443 * @param int|null $componentID
444 * Filter by component. If NULL, then look for explicitly NULL records.
447 protected static function whereCache($group, $path, $componentID) {
449 $clauses[] = ('group_name = "' . CRM_Core_DAO
::escapeString($group) . '"');
451 $clauses[] = ('path = "' . CRM_Core_DAO
::escapeString($path) . '"');
453 if ($componentID && is_numeric($componentID)) {
454 $clauses[] = ('component_id = ' . (int) $componentID);
456 return $clauses ?
implode(' AND ', $clauses) : '(1)';
460 * Normalize a cache key.
462 * This bridges an impedance mismatch between our traditional caching
463 * and PSR-16 -- PSR-16 accepts a narrower range of cache keys.
468 * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'.
469 * A similar key, but suitable for use with PSR-16-compliant cache providers.
471 public static function cleanKey($key) {
472 if (!is_string($key) && !is_int($key)) {
473 throw new \
RuntimeException("Malformed cache key");
479 if (strlen($key) >= $maxLen) {
480 return $escape . md5($key);
483 $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) {
484 return $escape . dechex(ord($m[0]));
487 return strlen($r) >= $maxLen ?
$escape . md5($key) : $r;