3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * BAO object for civicrm_cache table. This is a database cache and is persisted across sessions. Typically we use
38 * this to store meta data (like profile fields, custom fields etc).
40 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
41 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
42 * a specific group if needed.
44 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
45 * the unique form name for each form (per user)
47 class CRM_Core_BAO_Cache
extends CRM_Core_DAO_Cache
{
50 * @var array ($cacheKey => $cacheValue)
52 static $_cache = NULL;
55 * Retrieve an item from the DB cache
57 * @param string $group
58 * (required) The group name of the item.
60 * (required) The path under which this item is stored.
61 * @param int $componentID
62 * The optional component ID (so componenets can share the same name space).
65 * The data if present in cache, else null
68 public static function &getItem($group, $path, $componentID = NULL) {
69 if (self
::$_cache === NULL) {
70 self
::$_cache = array();
73 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
74 if (!array_key_exists($argString, self
::$_cache)) {
75 $cache = CRM_Utils_Cache
::singleton();
76 self
::$_cache[$argString] = $cache->get($argString);
77 if (!self
::$_cache[$argString]) {
78 $dao = new CRM_Core_DAO_Cache();
80 $dao->group_name
= $group;
82 $dao->component_id
= $componentID;
85 if ($dao->find(TRUE)) {
86 $data = unserialize($dao->data
);
89 self
::$_cache[$argString] = $data;
90 $cache->set($argString, self
::$_cache[$argString]);
93 return self
::$_cache[$argString];
97 * Retrieve all items in a group
99 * @param string $group
100 * (required) The group name of the item.
101 * @param int $componentID
102 * The optional component ID (so componenets can share the same name space).
105 * The data if present in cache, else null
108 public static function &getItems($group, $componentID = NULL) {
109 if (self
::$_cache === NULL) {
110 self
::$_cache = array();
113 $argString = "CRM_CT_CI_{$group}_{$componentID}";
114 if (!array_key_exists($argString, self
::$_cache)) {
115 $cache = CRM_Utils_Cache
::singleton();
116 self
::$_cache[$argString] = $cache->get($argString);
117 if (!self
::$_cache[$argString]) {
118 $dao = new CRM_Core_DAO_Cache();
120 $dao->group_name
= $group;
121 $dao->component_id
= $componentID;
124 $result = array(); // array($path => $data)
125 while ($dao->fetch()) {
126 $result[$dao->path
] = unserialize($dao->data
);
130 self
::$_cache[$argString] = $result;
131 $cache->set($argString, self
::$_cache[$argString]);
135 return self
::$_cache[$argString];
139 * Store an item in the DB cache
141 * @param object $data
142 * (required) A reference to the data that will be serialized and stored.
143 * @param string $group
144 * (required) The group name of the item.
145 * @param string $path
146 * (required) The path under which this item is stored.
147 * @param int $componentID
148 * The optional component ID (so componenets can share the same name space).
153 public static function setItem(&$data, $group, $path, $componentID = NULL) {
154 if (self
::$_cache === NULL) {
155 self
::$_cache = array();
158 $dao = new CRM_Core_DAO_Cache();
160 $dao->group_name
= $group;
162 $dao->component_id
= $componentID;
164 // get a lock so that multiple ajax requests on the same page
165 // dont trample on each other
167 $lockName = "civicrm.cache.{$group}_{$path}._{$componentID}";
168 $lock = new CRM_Core_Lock($lockName);
169 if (!$lock->isAcquired()) {
170 CRM_Core_Error
::fatal();
174 $dao->data
= serialize($data);
175 $dao->created_date
= date('YmdHis');
182 // cache coherency - refresh or remove dependent caches
184 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
185 $cache = CRM_Utils_Cache
::singleton();
186 $data = unserialize($dao->data
);
187 self
::$_cache[$argString] = $data;
188 $cache->set($argString, $data);
190 $argString = "CRM_CT_CI_{$group}_{$componentID}";
191 unset(self
::$_cache[$argString]);
192 $cache->delete($argString);
196 * Delete all the cache elements that belong to a group OR
197 * delete the entire cache if group is not specified
199 * @param string $group
200 * The group name of the entries to be deleted.
201 * @param string $path
202 * Path of the item that needs to be deleted.
203 * @param bool|\booleab $clearAll clear all caches
208 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
209 $dao = new CRM_Core_DAO_Cache();
211 if (!empty($group)) {
212 $dao->group_name
= $group;
222 // also reset ACL Cache
223 CRM_ACL_BAO_Cache
::resetCache();
225 // also reset memory cache if any
226 CRM_Utils_System
::flushCache();
231 * The next two functions are internal functions used to store and retrieve session from
232 * the database cache. This keeps the session to a limited size and allows us to
233 * create separate session scopes for each form in a tab
238 * This function takes entries from the session array and stores it in the cache.
239 * It also deletes the entries from the $_SESSION object (for a smaller session size)
241 * @param array $names
242 * Array of session values that should be persisted.
243 * This is either a form name + qfKey or just a form name
244 * (in the case of profile)
245 * @param bool $resetSession
246 * Should session state be reset on completion of DB store?.
251 public static function storeSessionToCache($names, $resetSession = TRUE) {
252 foreach ($names as $key => $sessionName) {
253 if (is_array($sessionName)) {
255 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
256 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
258 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
260 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
261 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
266 if (!empty($_SESSION[$sessionName])) {
267 $value = $_SESSION[$sessionName];
269 self
::setItem($value, 'CiviCRM Session', $sessionName);
271 $_SESSION[$sessionName] = NULL;
272 unset($_SESSION[$sessionName]);
280 /* Retrieve the session values from the cache and populate the $_SESSION array
282 * @param array $names
283 * Array of session values that should be persisted.
284 * This is either a form name + qfKey or just a form name
285 * (in the case of profile)
292 * @param string $names
294 public static function restoreSessionFromCache($names) {
295 foreach ($names as $key => $sessionName) {
296 if (is_array($sessionName)) {
297 $value = self
::getItem('CiviCRM Session',
298 "{$sessionName[0]}_{$sessionName[1]}"
301 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
305 $value = self
::getItem('CiviCRM Session',
309 $_SESSION[$sessionName] = $value;
316 * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
317 * which are a couple of days old. This keeps the session cache to a manageable size
319 * @param bool $session
321 * @param bool $prevNext
326 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
327 // clean up the session cache every $cacheCleanUpNumber probabilistically
328 $cleanUpNumber = 757;
330 // clean up all sessions older than $cacheTimeIntervalDays days
331 $timeIntervalDays = 2;
332 $timeIntervalMins = 30;
334 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
335 $session = $table = $prevNext = TRUE;
338 if (!$session && !$table && !$prevNext) {
343 // delete all PrevNext caches
344 CRM_Core_BAO_PrevNextCache
::cleanupCache();
348 // also delete all the action temp tables
349 // that were created the same interval ago
350 $dao = new CRM_Core_DAO();
352 SELECT TABLE_NAME as tableName
353 FROM INFORMATION_SCHEMA.TABLES
354 WHERE TABLE_SCHEMA = %1
355 AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
356 OR TABLE_NAME LIKE 'civicrm_export_temp_%'
357 OR TABLE_NAME LIKE 'civicrm_import_job_%' )
358 AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
361 $params = array(1 => array($dao->database(), 'String'));
362 $tableDAO = CRM_Core_DAO
::executeQuery($query, $params);
364 while ($tableDAO->fetch()) {
365 $tables[] = $tableDAO->tableName
;
367 if (!empty($tables)) {
368 $table = implode(',', $tables);
369 // drop leftover temporary tables
370 CRM_Core_DAO
::executeQuery("DROP TABLE $table");
375 // first delete all sessions which are related to any potential transaction
377 $transactionPages = array(
378 'CRM_Contribute_Controller_Contribution',
379 'CRM_Event_Controller_Registration',
383 1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
385 foreach ($transactionPages as $trPage) {
386 $params[] = array("%${trPage}%", 'String');
387 $where[] = 'path LIKE %' . sizeof($params);
391 DELETE FROM civicrm_cache
392 WHERE group_name = 'CiviCRM Session'
393 AND created_date <= %1
394 AND (" . implode(' OR ', $where) . ")";
395 CRM_Core_DAO
::executeQuery($sql, $params);
398 DELETE FROM civicrm_cache
399 WHERE group_name = 'CiviCRM Session'
400 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
402 CRM_Core_DAO
::executeQuery($sql);