3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 * @var array ($cacheKey => $cacheValue)
52 static $_cache = NULL;
55 * Retrieve an item from the DB cache
57 * @param string $group (required) The group name of the item
58 * @param string $path (required) The path under which this item is stored
59 * @param int $componentID The optional component ID (so componenets can share the same name space)
61 * @return object The data if present in cache, else null
65 static function &getItem($group, $path, $componentID = NULL) {
66 if (self
::$_cache === NULL) {
67 self
::$_cache = array();
70 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
71 if (!array_key_exists($argString, self
::$_cache)) {
72 $cache = CRM_Utils_Cache
::singleton();
73 self
::$_cache[$argString] = $cache->get($argString);
74 if (!self
::$_cache[$argString]) {
75 $dao = new CRM_Core_DAO_Cache();
77 $dao->group_name
= $group;
79 $dao->component_id
= $componentID;
82 if ($dao->find(TRUE)) {
83 $data = unserialize($dao->data
);
86 self
::$_cache[$argString] = $data;
87 $cache->set($argString, self
::$_cache[$argString]);
90 return self
::$_cache[$argString];
94 * Retrieve all items in a group
96 * @param string $group (required) The group name of the item
97 * @param int $componentID The optional component ID (so componenets can share the same name space)
99 * @return object The data if present in cache, else null
103 static function &getItems($group, $componentID = NULL) {
104 if (self
::$_cache === NULL) {
105 self
::$_cache = array();
108 $argString = "CRM_CT_CI_{$group}_{$componentID}";
109 if (!array_key_exists($argString, self
::$_cache)) {
110 $cache = CRM_Utils_Cache
::singleton();
111 self
::$_cache[$argString] = $cache->get($argString);
112 if (!self
::$_cache[$argString]) {
113 $dao = new CRM_Core_DAO_Cache();
115 $dao->group_name
= $group;
116 $dao->component_id
= $componentID;
119 $result = array(); // array($path => $data)
120 while ($dao->fetch()) {
121 $result[$dao->path
] = unserialize($dao->data
);
125 self
::$_cache[$argString] = $result;
126 $cache->set($argString, self
::$_cache[$argString]);
130 return self
::$_cache[$argString];
134 * Store an item in the DB cache
136 * @param object $data (required) A reference to the data that will be serialized and stored
137 * @param string $group (required) The group name of the item
138 * @param string $path (required) The path under which this item is stored
139 * @param int $componentID The optional component ID (so componenets can share the same name space)
145 static function setItem(&$data, $group, $path, $componentID = NULL) {
146 if (self
::$_cache === NULL) {
147 self
::$_cache = array();
150 $dao = new CRM_Core_DAO_Cache();
152 $dao->group_name
= $group;
154 $dao->component_id
= $componentID;
156 // get a lock so that multiple ajax requests on the same page
157 // dont trample on each other
159 $lockName = "civicrm.cache.{$group}_{$path}._{$componentID}";
160 $lock = new CRM_Core_Lock($lockName);
161 if (!$lock->isAcquired()) {
162 CRM_Core_Error
::fatal();
166 $dao->data
= serialize($data);
167 $dao->created_date
= date('YmdHis');
174 // cache coherency - refresh or remove dependent caches
176 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
177 $cache = CRM_Utils_Cache
::singleton();
178 $data = unserialize($dao->data
);
179 self
::$_cache[$argString] = $data;
180 $cache->set($argString, $data);
182 $argString = "CRM_CT_CI_{$group}_{$componentID}";
183 unset(self
::$_cache[$argString]);
184 $cache->delete($argString);
188 * Delete all the cache elements that belong to a group OR
189 * delete the entire cache if group is not specified
191 * @param string $group The group name of the entries to be deleted
192 * @param string $path path of the item that needs to be deleted
193 * @param booleab $clearAll clear all caches
199 static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
200 $dao = new CRM_Core_DAO_Cache();
202 if (!empty($group)) {
203 $dao->group_name
= $group;
213 // also reset ACL Cache
214 CRM_ACL_BAO_Cache
::resetCache();
216 // also reset memory cache if any
217 CRM_Utils_System
::flushCache();
222 * The next two functions are internal functions used to store and retrieve session from
223 * the database cache. This keeps the session to a limited size and allows us to
224 * create separate session scopes for each form in a tab
229 * This function takes entries from the session array and stores it in the cache.
230 * It also deletes the entries from the $_SESSION object (for a smaller session size)
232 * @param array $names Array of session values that should be persisted
233 * This is either a form name + qfKey or just a form name
234 * (in the case of profile)
235 * @param boolean $resetSession Should session state be reset on completion of DB store?
241 static function storeSessionToCache($names, $resetSession = TRUE) {
242 foreach ($names as $key => $sessionName) {
243 if (is_array($sessionName)) {
245 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
246 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
248 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
250 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
251 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
256 if (!empty($_SESSION[$sessionName])) {
257 $value = $_SESSION[$sessionName];
259 self
::setItem($value, 'CiviCRM Session', $sessionName);
261 $_SESSION[$sessionName] = NULL;
262 unset($_SESSION[$sessionName]);
270 /* Retrieve the session values from the cache and populate the $_SESSION array
272 * @param array $names Array of session values that should be persisted
273 * This is either a form name + qfKey or just a form name
274 * (in the case of profile)
281 static function restoreSessionFromCache($names) {
282 foreach ($names as $key => $sessionName) {
283 if (is_array($sessionName)) {
284 $value = self
::getItem('CiviCRM Session',
285 "{$sessionName[0]}_{$sessionName[1]}"
288 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
292 $value = self
::getItem('CiviCRM Session',
296 $_SESSION[$sessionName] = $value;
303 * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
304 * which are a couple of days old. This keeps the session cache to a manageable size
310 static function cleanup($session = false, $table = false, $prevNext = false) {
311 // clean up the session cache every $cacheCleanUpNumber probabilistically
312 $cleanUpNumber = 757;
314 // clean up all sessions older than $cacheTimeIntervalDays days
315 $timeIntervalDays = 2;
316 $timeIntervalMins = 30;
318 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
319 $session = $table = $prevNext = true;
322 if ( ! $session && ! $table && ! $prevNext ) {
327 // delete all PrevNext caches
328 CRM_Core_BAO_PrevNextCache
::cleanupCache();
332 // also delete all the action temp tables
333 // that were created the same interval ago
334 $dao = new CRM_Core_DAO();
336 SELECT TABLE_NAME as tableName
337 FROM INFORMATION_SCHEMA.TABLES
338 WHERE TABLE_SCHEMA = %1
339 AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
340 OR TABLE_NAME LIKE 'civicrm_export_temp_%'
341 OR TABLE_NAME LIKE 'civicrm_import_job_%' )
342 AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
345 $params = array(1 => array($dao->database(), 'String'));
346 $tableDAO = CRM_Core_DAO
::executeQuery($query, $params);
348 while ($tableDAO->fetch()) {
349 $tables[] = $tableDAO->tableName
;
351 if (!empty($tables)) {
352 $table = implode(',', $tables);
353 // drop leftover temporary tables
354 CRM_Core_DAO
::executeQuery("DROP TABLE $table");
359 // first delete all sessions which are related to any potential transaction
361 $transactionPages = array(
362 'CRM_Contribute_Controller_Contribution',
363 'CRM_Event_Controller_Registration',
367 1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
369 foreach ($transactionPages as $trPage) {
370 $params[] = array("%${trPage}%", 'String');
371 $where[] = 'path LIKE %' . sizeof($params);
375 DELETE FROM civicrm_cache
376 WHERE group_name = 'CiviCRM Session'
377 AND created_date <= %1
378 AND (" . implode(' OR ', $where) . ")";
379 CRM_Core_DAO
::executeQuery($sql, $params);
382 DELETE FROM civicrm_cache
383 WHERE group_name = 'CiviCRM Session'
384 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
386 CRM_Core_DAO
::executeQuery($sql);