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
67 public static function &getItem($group, $path, $componentID = NULL) {
68 if (self
::$_cache === NULL) {
69 self
::$_cache = array();
72 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
73 if (!array_key_exists($argString, self
::$_cache)) {
74 $cache = CRM_Utils_Cache
::singleton();
75 self
::$_cache[$argString] = $cache->get($argString);
76 if (!self
::$_cache[$argString]) {
77 $dao = new CRM_Core_DAO_Cache();
79 $dao->group_name
= $group;
81 $dao->component_id
= $componentID;
84 if ($dao->find(TRUE)) {
85 $data = unserialize($dao->data
);
88 self
::$_cache[$argString] = $data;
89 $cache->set($argString, self
::$_cache[$argString]);
92 return self
::$_cache[$argString];
96 * Retrieve all items in a group.
98 * @param string $group
99 * (required) The group name of the item.
100 * @param int $componentID
101 * The optional component ID (so componenets can share the same name space).
104 * The data if present in cache, else null
106 public static function &getItems($group, $componentID = NULL) {
107 if (self
::$_cache === NULL) {
108 self
::$_cache = array();
111 $argString = "CRM_CT_CI_{$group}_{$componentID}";
112 if (!array_key_exists($argString, self
::$_cache)) {
113 $cache = CRM_Utils_Cache
::singleton();
114 self
::$_cache[$argString] = $cache->get($argString);
115 if (!self
::$_cache[$argString]) {
116 $dao = new CRM_Core_DAO_Cache();
118 $dao->group_name
= $group;
119 $dao->component_id
= $componentID;
122 $result = array(); // array($path => $data)
123 while ($dao->fetch()) {
124 $result[$dao->path
] = unserialize($dao->data
);
128 self
::$_cache[$argString] = $result;
129 $cache->set($argString, self
::$_cache[$argString]);
133 return self
::$_cache[$argString];
137 * Store an item in the DB cache.
139 * @param object $data
140 * (required) A reference to the data that will be serialized and stored.
141 * @param string $group
142 * (required) The group name of the item.
143 * @param string $path
144 * (required) The path under which this item is stored.
145 * @param int $componentID
146 * The optional component ID (so componenets can share the same name space).
150 public static function setItem(&$data, $group, $path, $componentID = NULL) {
151 if (self
::$_cache === NULL) {
152 self
::$_cache = array();
155 $dao = new CRM_Core_DAO_Cache();
157 $dao->group_name
= $group;
159 $dao->component_id
= $componentID;
161 // get a lock so that multiple ajax requests on the same page
162 // dont trample on each other
164 $lockName = "civicrm.cache.{$group}_{$path}._{$componentID}";
165 $lock = new CRM_Core_Lock($lockName);
166 if (!$lock->isAcquired()) {
167 CRM_Core_Error
::fatal();
171 $dao->data
= serialize($data);
172 $dao->created_date
= date('YmdHis');
179 // cache coherency - refresh or remove dependent caches
181 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
182 $cache = CRM_Utils_Cache
::singleton();
183 $data = unserialize($dao->data
);
184 self
::$_cache[$argString] = $data;
185 $cache->set($argString, $data);
187 $argString = "CRM_CT_CI_{$group}_{$componentID}";
188 unset(self
::$_cache[$argString]);
189 $cache->delete($argString);
193 * Delete all the cache elements that belong to a group OR
194 * delete the entire cache if group is not specified
196 * @param string $group
197 * The group name of the entries to be deleted.
198 * @param string $path
199 * Path of the item that needs to be deleted.
200 * @param bool|\booleab $clearAll clear all caches
204 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
205 $dao = new CRM_Core_DAO_Cache();
207 if (!empty($group)) {
208 $dao->group_name
= $group;
218 // also reset ACL Cache
219 CRM_ACL_BAO_Cache
::resetCache();
221 // also reset memory cache if any
222 CRM_Utils_System
::flushCache();
227 * The next two functions are internal functions used to store and retrieve session from
228 * the database cache. This keeps the session to a limited size and allows us to
229 * create separate session scopes for each form in a tab
233 * This function takes entries from the session array and stores it in the cache.
234 * It also deletes the entries from the $_SESSION object (for a smaller session size)
236 * @param array $names
237 * Array of session values that should be persisted.
238 * This is either a form name + qfKey or just a form name
239 * (in the case of profile)
240 * @param bool $resetSession
241 * Should session state be reset on completion of DB store?.
245 public static function storeSessionToCache($names, $resetSession = TRUE) {
246 foreach ($names as $key => $sessionName) {
247 if (is_array($sessionName)) {
249 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
250 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
252 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
254 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
255 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
260 if (!empty($_SESSION[$sessionName])) {
261 $value = $_SESSION[$sessionName];
263 self
::setItem($value, 'CiviCRM Session', $sessionName);
265 $_SESSION[$sessionName] = NULL;
266 unset($_SESSION[$sessionName]);
274 /* Retrieve the session values from the cache and populate the $_SESSION array
276 * @param array $names
277 * Array of session values that should be persisted.
278 * This is either a form name + qfKey or just a form name
279 * (in the case of profile)
285 * @param string $names
287 public static function restoreSessionFromCache($names) {
288 foreach ($names as $key => $sessionName) {
289 if (is_array($sessionName)) {
290 $value = self
::getItem('CiviCRM Session',
291 "{$sessionName[0]}_{$sessionName[1]}"
294 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
298 $value = self
::getItem('CiviCRM Session',
302 $_SESSION[$sessionName] = $value;
309 * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
310 * which are a couple of days old. This keeps the session cache to a manageable size
312 * @param bool $session
314 * @param bool $prevNext
318 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
319 // clean up the session cache every $cacheCleanUpNumber probabilistically
320 $cleanUpNumber = 757;
322 // clean up all sessions older than $cacheTimeIntervalDays days
323 $timeIntervalDays = 2;
324 $timeIntervalMins = 30;
326 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
327 $session = $table = $prevNext = TRUE;
330 if (!$session && !$table && !$prevNext) {
335 // delete all PrevNext caches
336 CRM_Core_BAO_PrevNextCache
::cleanupCache();
340 // also delete all the action temp tables
341 // that were created the same interval ago
342 $dao = new CRM_Core_DAO();
344 SELECT TABLE_NAME as tableName
345 FROM INFORMATION_SCHEMA.TABLES
346 WHERE TABLE_SCHEMA = %1
347 AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
348 OR TABLE_NAME LIKE 'civicrm_export_temp_%'
349 OR TABLE_NAME LIKE 'civicrm_import_job_%' )
350 AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
353 $params = array(1 => array($dao->database(), 'String'));
354 $tableDAO = CRM_Core_DAO
::executeQuery($query, $params);
356 while ($tableDAO->fetch()) {
357 $tables[] = $tableDAO->tableName
;
359 if (!empty($tables)) {
360 $table = implode(',', $tables);
361 // drop leftover temporary tables
362 CRM_Core_DAO
::executeQuery("DROP TABLE $table");
367 // first delete all sessions which are related to any potential transaction
369 $transactionPages = array(
370 'CRM_Contribute_Controller_Contribution',
371 'CRM_Event_Controller_Registration',
375 1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
377 foreach ($transactionPages as $trPage) {
378 $params[] = array("%${trPage}%", 'String');
379 $where[] = 'path LIKE %' . count($params);
383 DELETE FROM civicrm_cache
384 WHERE group_name = 'CiviCRM Session'
385 AND created_date <= %1
386 AND (" . implode(' OR ', $where) . ")";
387 CRM_Core_DAO
::executeQuery($sql, $params);
390 DELETE FROM civicrm_cache
391 WHERE group_name = 'CiviCRM Session'
392 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
394 CRM_Core_DAO
::executeQuery($sql);