3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * Retrieve an item from the DB cache
52 * @param string $group (required) The group name of the item
53 * @param string $path (required) The path under which this item is stored
54 * @param int $componentID The optional component ID (so componenets can share the same name space)
56 * @return object The data if present in cache, else null
60 static function &getItem($group, $path, $componentID = NULL) {
61 $dao = new CRM_Core_DAO_Cache();
63 $dao->group_name
= $group;
65 $dao->component_id
= $componentID;
68 if ($dao->find(TRUE)) {
69 $data = unserialize($dao->data
);
76 * Retrieve all items in a group
78 * @param string $group (required) The group name of the item
79 * @param int $componentID The optional component ID (so componenets can share the same name space)
81 * @return object The data if present in cache, else null
85 static function &getItems($group, $componentID = NULL) {
86 $dao = new CRM_Core_DAO_Cache();
88 $dao->group_name
= $group;
89 $dao->component_id
= $componentID;
92 $result = array(); // array($path => $data)
93 while ($dao->fetch()) {
94 $result[$dao->path
] = unserialize($dao->data
);
101 * Store an item in the DB cache
103 * @param object $data (required) A reference to the data that will be serialized and stored
104 * @param string $group (required) The group name of the item
105 * @param string $path (required) The path under which this item is stored
106 * @param int $componentID The optional component ID (so componenets can share the same name space)
112 static function setItem(&$data, $group, $path, $componentID = NULL) {
113 $dao = new CRM_Core_DAO_Cache();
115 $dao->group_name
= $group;
117 $dao->component_id
= $componentID;
119 // get a lock so that multiple ajax requests on the same page
120 // dont trample on each other
122 $lockName = "civicrm.cache.{$group}_{$path}._{$componentID}";
123 $lock = new CRM_Core_Lock($lockName);
124 if (!$lock->isAcquired()) {
125 CRM_Core_Error
::fatal();
129 $dao->data
= serialize($data);
130 $dao->created_date
= date('YmdHis');
139 * Delete all the cache elements that belong to a group OR
140 * delete the entire cache if group is not specified
142 * @param string $group The group name of the entries to be deleted
143 * @param string $path path of the item that needs to be deleted
144 * @param booleab $clearAll clear all caches
150 static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
151 $dao = new CRM_Core_DAO_Cache();
153 if (!empty($group)) {
154 $dao->group_name
= $group;
164 // also reset ACL Cache
165 CRM_ACL_BAO_Cache
::resetCache();
167 // also reset memory cache if any
168 CRM_Utils_System
::flushCache();
173 * The next two functions are internal functions used to store and retrieve session from
174 * the database cache. This keeps the session to a limited size and allows us to
175 * create separate session scopes for each form in a tab
180 * This function takes entries from the session array and stores it in the cache.
181 * It also deletes the entries from the $_SESSION object (for a smaller session size)
183 * @param array $names Array of session values that should be persisted
184 * This is either a form name + qfKey or just a form name
185 * (in the case of profile)
186 * @param boolean $resetSession Should session state be reset on completion of DB store?
192 static function storeSessionToCache($names, $resetSession = TRUE) {
193 foreach ($names as $key => $sessionName) {
194 if (is_array($sessionName)) {
196 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
197 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
199 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
201 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
202 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
207 if (!empty($_SESSION[$sessionName])) {
208 $value = $_SESSION[$sessionName];
210 self
::setItem($value, 'CiviCRM Session', $sessionName);
212 $_SESSION[$sessionName] = NULL;
213 unset($_SESSION[$sessionName]);
221 /* Retrieve the session values from the cache and populate the $_SESSION array
223 * @param array $names Array of session values that should be persisted
224 * This is either a form name + qfKey or just a form name
225 * (in the case of profile)
232 static function restoreSessionFromCache($names) {
233 foreach ($names as $key => $sessionName) {
234 if (is_array($sessionName)) {
235 $value = self
::getItem('CiviCRM Session',
236 "{$sessionName[0]}_{$sessionName[1]}"
239 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
243 $value = self
::getItem('CiviCRM Session',
247 $_SESSION[$sessionName] = $value;
254 * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
255 * which are a couple of days old. This keeps the session cache to a manageable size
261 static function cleanup($session = false, $table = false, $prevNext = false) {
262 // clean up the session cache every $cacheCleanUpNumber probabilistically
263 $cleanUpNumber = 757;
265 // clean up all sessions older than $cacheTimeIntervalDays days
266 $timeIntervalDays = 2;
267 $timeIntervalMins = 30;
269 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
270 $session = $table = $prevNext = true;
273 if ( ! $session && ! $table && ! $prevNext ) {
278 // delete all PrevNext caches
279 CRM_Core_BAO_PrevNextCache
::cleanupCache();
283 // also delete all the action temp tables
284 // that were created the same interval ago
285 $dao = new CRM_Core_DAO();
287 SELECT TABLE_NAME as tableName
288 FROM INFORMATION_SCHEMA.TABLES
289 WHERE TABLE_SCHEMA = %1
290 AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
291 OR TABLE_NAME LIKE 'civicrm_export_temp_%'
292 OR TABLE_NAME LIKE 'civicrm_import_job_%' )
293 AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
296 $params = array(1 => array($dao->database(), 'String'));
297 $tableDAO = CRM_Core_DAO
::executeQuery($query, $params);
299 while ($tableDAO->fetch()) {
300 $tables[] = $tableDAO->tableName
;
302 if (!empty($tables)) {
303 $table = implode(',', $tables);
304 // drop leftover temporary tables
305 CRM_Core_DAO
::executeQuery("DROP TABLE $table");
310 // first delete all sessions which are related to any potential transaction
312 $transactionPages = array(
313 'CRM_Contribute_Controller_Contribution',
314 'CRM_Event_Controller_Registration',
318 1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
320 foreach ($transactionPages as $trPage) {
321 $params[] = array("%${trPage}%", 'String');
322 $where[] = 'path LIKE %' . sizeof($params);
326 DELETE FROM civicrm_cache
327 WHERE group_name = 'CiviCRM Session'
328 AND created_date <= %1
329 AND (" . implode(' OR ', $where) . ")";
330 CRM_Core_DAO
::executeQuery($sql, $params);
333 DELETE FROM civicrm_cache
334 WHERE group_name = 'CiviCRM Session'
335 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
337 CRM_Core_DAO
::executeQuery($sql);