3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
29 * BAO object for civicrm_cache table. This is a database cache and is persisted across sessions. Typically we use
30 * this to store meta data (like profile fields, custom fields etc).
32 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
33 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
34 * a specific group if needed.
36 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
37 * the unique form name for each form (per user)
39 class CRM_Core_BAO_Cache
extends CRM_Core_DAO_Cache
{
42 * @var array ($cacheKey => $cacheValue)
44 static $_cache = NULL;
47 * Retrieve an item from the DB cache.
49 * @param string $group
50 * (required) The group name of the item.
52 * (required) The path under which this item is stored.
53 * @param int $componentID
54 * The optional component ID (so componenets can share the same name space).
57 * The data if present in cache, else null
59 public static function &getItem($group, $path, $componentID = NULL) {
60 if (self
::$_cache === NULL) {
61 self
::$_cache = array();
64 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
65 if (!array_key_exists($argString, self
::$_cache)) {
66 $cache = CRM_Utils_Cache
::singleton();
67 self
::$_cache[$argString] = $cache->get($argString);
68 if (!self
::$_cache[$argString]) {
69 $dao = new CRM_Core_DAO_Cache();
71 $dao->group_name
= $group;
73 $dao->component_id
= $componentID;
76 if ($dao->find(TRUE)) {
77 $data = unserialize($dao->data
);
80 self
::$_cache[$argString] = $data;
81 $cache->set($argString, self
::$_cache[$argString]);
84 return self
::$_cache[$argString];
88 * Retrieve all items in a group.
90 * @param string $group
91 * (required) The group name of the item.
92 * @param int $componentID
93 * The optional component ID (so componenets can share the same name space).
96 * The data if present in cache, else null
98 public static function &getItems($group, $componentID = NULL) {
99 if (self
::$_cache === NULL) {
100 self
::$_cache = array();
103 $argString = "CRM_CT_CI_{$group}_{$componentID}";
104 if (!array_key_exists($argString, self
::$_cache)) {
105 $cache = CRM_Utils_Cache
::singleton();
106 self
::$_cache[$argString] = $cache->get($argString);
107 if (!self
::$_cache[$argString]) {
108 $dao = new CRM_Core_DAO_Cache();
110 $dao->group_name
= $group;
111 $dao->component_id
= $componentID;
115 while ($dao->fetch()) {
116 $result[$dao->path
] = unserialize($dao->data
);
120 self
::$_cache[$argString] = $result;
121 $cache->set($argString, self
::$_cache[$argString]);
125 return self
::$_cache[$argString];
129 * Store an item in the DB cache.
131 * @param object $data
132 * (required) A reference to the data that will be serialized and stored.
133 * @param string $group
134 * (required) The group name of the item.
135 * @param string $path
136 * (required) The path under which this item is stored.
137 * @param int $componentID
138 * The optional component ID (so componenets can share the same name space).
140 public static function setItem(&$data, $group, $path, $componentID = NULL) {
141 if (self
::$_cache === NULL) {
142 self
::$_cache = array();
145 $dao = new CRM_Core_DAO_Cache();
147 $dao->group_name
= $group;
149 $dao->component_id
= $componentID;
151 // get a lock so that multiple ajax requests on the same page
152 // dont trample on each other
154 $lock = Civi\Core\Container
::singleton()->get('lockManager')->acquire("cache.{$group}_{$path}._{$componentID}");
155 if (!$lock->isAcquired()) {
156 CRM_Core_Error
::fatal();
160 $dao->data
= serialize($data);
161 $dao->created_date
= date('YmdHis');
168 // cache coherency - refresh or remove dependent caches
170 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
171 $cache = CRM_Utils_Cache
::singleton();
172 $data = unserialize($dao->data
);
173 self
::$_cache[$argString] = $data;
174 $cache->set($argString, $data);
176 $argString = "CRM_CT_CI_{$group}_{$componentID}";
177 unset(self
::$_cache[$argString]);
178 $cache->delete($argString);
182 * Delete all the cache elements that belong to a group OR delete the entire cache if group is not specified.
184 * @param string $group
185 * The group name of the entries to be deleted.
186 * @param string $path
187 * Path of the item that needs to be deleted.
188 * @param bool $clearAll clear all caches
190 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
191 $dao = new CRM_Core_DAO_Cache();
193 if (!empty($group)) {
194 $dao->group_name
= $group;
204 // also reset ACL Cache
205 CRM_ACL_BAO_Cache
::resetCache();
207 // also reset memory cache if any
208 CRM_Utils_System
::flushCache();
213 * The next two functions are internal functions used to store and retrieve session from
214 * the database cache. This keeps the session to a limited size and allows us to
215 * create separate session scopes for each form in a tab
219 * This function takes entries from the session array and stores it in the cache.
221 * It also deletes the entries from the $_SESSION object (for a smaller session size)
223 * @param array $names
224 * Array of session values that should be persisted.
225 * This is either a form name + qfKey or just a form name
226 * (in the case of profile)
227 * @param bool $resetSession
228 * Should session state be reset on completion of DB store?.
230 public static function storeSessionToCache($names, $resetSession = TRUE) {
231 foreach ($names as $key => $sessionName) {
232 if (is_array($sessionName)) {
234 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
235 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
237 self
::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
239 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
240 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
245 if (!empty($_SESSION[$sessionName])) {
246 $value = $_SESSION[$sessionName];
248 self
::setItem($value, 'CiviCRM Session', $sessionName);
250 $_SESSION[$sessionName] = NULL;
251 unset($_SESSION[$sessionName]);
259 /* Retrieve the session values from the cache and populate the $_SESSION array
261 * @param array $names
262 * Array of session values that should be persisted.
263 * This is either a form name + qfKey or just a form name
264 * (in the case of profile)
270 * Restore session from cache.
272 * @param string $names
274 public static function restoreSessionFromCache($names) {
275 foreach ($names as $key => $sessionName) {
276 if (is_array($sessionName)) {
277 $value = self
::getItem('CiviCRM Session',
278 "{$sessionName[0]}_{$sessionName[1]}"
281 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
285 $value = self
::getItem('CiviCRM Session',
289 $_SESSION[$sessionName] = $value;
296 * Do periodic cleanup of the CiviCRM session table.
298 * Also delete all session cache entries which are a couple of days old.
299 * This keeps the session cache to a manageable size
301 * @param bool $session
303 * @param bool $prevNext
305 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
306 // clean up the session cache every $cacheCleanUpNumber probabilistically
307 $cleanUpNumber = 757;
309 // clean up all sessions older than $cacheTimeIntervalDays days
310 $timeIntervalDays = 2;
311 $timeIntervalMins = 30;
313 if (mt_rand(1, 100000) %
$cleanUpNumber == 0) {
314 $session = $table = $prevNext = TRUE;
317 if (!$session && !$table && !$prevNext) {
322 // delete all PrevNext caches
323 CRM_Core_BAO_PrevNextCache
::cleanupCache();
327 CRM_Core_Config
::clearTempTables($timeIntervalDays . ' day');
331 // first delete all sessions which are related to any potential transaction
333 $transactionPages = array(
334 'CRM_Contribute_Controller_Contribution',
335 'CRM_Event_Controller_Registration',
340 date('Y-m-d H:i:s', time() - $timeIntervalMins * 60),
344 foreach ($transactionPages as $trPage) {
345 $params[] = array("%${trPage}%", 'String');
346 $where[] = 'path LIKE %' . count($params);
350 DELETE FROM civicrm_cache
351 WHERE group_name = 'CiviCRM Session'
352 AND created_date <= %1
353 AND (" . implode(' OR ', $where) . ")";
354 CRM_Core_DAO
::executeQuery($sql, $params);
357 DELETE FROM civicrm_cache
358 WHERE group_name = 'CiviCRM Session'
359 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
361 CRM_Core_DAO
::executeQuery($sql);