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
237 * This function takes entries from the session array and stores it in the cache.
238 * It also deletes the entries from the $_SESSION object (for a smaller session size)
240 * @param array $names
241 * Array of session values that should be persisted.
242 * This is either a form name + qfKey or just a form name
243 * (in the case of profile)
244 * @param bool $resetSession
245 * Should session state be reset on completion of DB store?.
250 public static function storeSessionToCache($names, $resetSession = TRUE) {
251 foreach ($names as $key => $sessionName) {
252 if (is_array($sessionName)) {
254 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
255 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
257 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
259 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
260 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
265 if (!empty($_SESSION[$sessionName])) {
266 $value = $_SESSION[$sessionName];
268 self
::setItem($value, 'CiviCRM Session', $sessionName);
270 $_SESSION[$sessionName] = NULL;
271 unset($_SESSION[$sessionName]);
279 /* Retrieve the session values from the cache and populate the $_SESSION array
281 * @param array $names
282 * Array of session values that should be persisted.
283 * This is either a form name + qfKey or just a form name
284 * (in the case of profile)
291 * @param string $names
293 public static function restoreSessionFromCache($names) {
294 foreach ($names as $key => $sessionName) {
295 if (is_array($sessionName)) {
296 $value = self
::getItem('CiviCRM Session',
297 "{$sessionName[0]}_{$sessionName[1]}"
300 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
304 $value = self
::getItem('CiviCRM Session',
308 $_SESSION[$sessionName] = $value;
315 * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
316 * which are a couple of days old. This keeps the session cache to a manageable size
318 * @param bool $session
320 * @param bool $prevNext
325 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
326 // clean up the session cache every $cacheCleanUpNumber probabilistically
327 $cleanUpNumber = 757;
329 // clean up all sessions older than $cacheTimeIntervalDays days
330 $timeIntervalDays = 2;
331 $timeIntervalMins = 30;
333 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
334 $session = $table = $prevNext = TRUE;
337 if (!$session && !$table && !$prevNext) {
342 // delete all PrevNext caches
343 CRM_Core_BAO_PrevNextCache
::cleanupCache();
347 // also delete all the action temp tables
348 // that were created the same interval ago
349 $dao = new CRM_Core_DAO();
351 SELECT TABLE_NAME as tableName
352 FROM INFORMATION_SCHEMA.TABLES
353 WHERE TABLE_SCHEMA = %1
354 AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
355 OR TABLE_NAME LIKE 'civicrm_export_temp_%'
356 OR TABLE_NAME LIKE 'civicrm_import_job_%' )
357 AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
360 $params = array(1 => array($dao->database(), 'String'));
361 $tableDAO = CRM_Core_DAO
::executeQuery($query, $params);
363 while ($tableDAO->fetch()) {
364 $tables[] = $tableDAO->tableName
;
366 if (!empty($tables)) {
367 $table = implode(',', $tables);
368 // drop leftover temporary tables
369 CRM_Core_DAO
::executeQuery("DROP TABLE $table");
374 // first delete all sessions which are related to any potential transaction
376 $transactionPages = array(
377 'CRM_Contribute_Controller_Contribution',
378 'CRM_Event_Controller_Registration',
382 1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
384 foreach ($transactionPages as $trPage) {
385 $params[] = array("%${trPage}%", 'String');
386 $where[] = 'path LIKE %' . sizeof($params);
390 DELETE FROM civicrm_cache
391 WHERE group_name = 'CiviCRM Session'
392 AND created_date <= %1
393 AND (" . implode(' OR ', $where) . ")";
394 CRM_Core_DAO
::executeQuery($sql, $params);
397 DELETE FROM civicrm_cache
398 WHERE group_name = 'CiviCRM Session'
399 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
401 CRM_Core_DAO
::executeQuery($sql);