Merge pull request #9596 from eileenmcnaughton/performance
[civicrm-core.git] / CRM / Core / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * BAO object for civicrm_cache table.
30 *
31 * This is a database cache and is persisted across sessions. Typically we use
32 * this to store meta data (like profile fields, custom fields etc).
33 *
34 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
35 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
36 * a specific group if needed.
37 *
38 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
39 * the unique form name for each form (per user)
40 */
41 class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
42
43 /**
44 * @var array ($cacheKey => $cacheValue)
45 */
46 static $_cache = NULL;
47
48 /**
49 * Retrieve an item from the DB cache.
50 *
51 * @param string $group
52 * (required) The group name of the item.
53 * @param string $path
54 * (required) The path under which this item is stored.
55 * @param int $componentID
56 * The optional component ID (so componenets can share the same name space).
57 *
58 * @return object
59 * The data if present in cache, else null
60 */
61 public static function &getItem($group, $path, $componentID = NULL) {
62 if (self::$_cache === NULL) {
63 self::$_cache = array();
64 }
65
66 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
67 if (!array_key_exists($argString, self::$_cache)) {
68 $cache = CRM_Utils_Cache::singleton();
69 self::$_cache[$argString] = $cache->get($argString);
70 if (!self::$_cache[$argString]) {
71 $table = self::getTableName();
72 $where = self::whereCache($group, $path, $componentID);
73 $rawData = CRM_Core_DAO::singleValueQuery("SELECT data FROM $table WHERE $where");
74 $data = $rawData ? unserialize($rawData) : NULL;
75
76 self::$_cache[$argString] = $data;
77 $cache->set($argString, self::$_cache[$argString]);
78 }
79 }
80 return self::$_cache[$argString];
81 }
82
83 /**
84 * Retrieve all items in a group.
85 *
86 * @param string $group
87 * (required) The group name of the item.
88 * @param int $componentID
89 * The optional component ID (so componenets can share the same name space).
90 *
91 * @return object
92 * The data if present in cache, else null
93 */
94 public static function &getItems($group, $componentID = NULL) {
95 if (self::$_cache === NULL) {
96 self::$_cache = array();
97 }
98
99 $argString = "CRM_CT_CI_{$group}_{$componentID}";
100 if (!array_key_exists($argString, self::$_cache)) {
101 $cache = CRM_Utils_Cache::singleton();
102 self::$_cache[$argString] = $cache->get($argString);
103 if (!self::$_cache[$argString]) {
104 $table = self::getTableName();
105 $where = self::whereCache($group, NULL, $componentID);
106 $dao = CRM_Core_DAO::executeQuery("SELECT path, data FROM $table WHERE $where");
107
108 $result = array();
109 while ($dao->fetch()) {
110 $result[$dao->path] = unserialize($dao->data);
111 }
112 $dao->free();
113
114 self::$_cache[$argString] = $result;
115 $cache->set($argString, self::$_cache[$argString]);
116 }
117 }
118
119 return self::$_cache[$argString];
120 }
121
122 /**
123 * Store an item in the DB cache.
124 *
125 * @param object $data
126 * (required) A reference to the data that will be serialized and stored.
127 * @param string $group
128 * (required) The group name of the item.
129 * @param string $path
130 * (required) The path under which this item is stored.
131 * @param int $componentID
132 * The optional component ID (so componenets can share the same name space).
133 */
134 public static function setItem(&$data, $group, $path, $componentID = NULL) {
135 if (self::$_cache === NULL) {
136 self::$_cache = array();
137 }
138
139 // get a lock so that multiple ajax requests on the same page
140 // dont trample on each other
141 // CRM-11234
142 $lock = Civi::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
143 if (!$lock->isAcquired()) {
144 CRM_Core_Error::fatal();
145 }
146
147 $table = self::getTableName();
148 $where = self::whereCache($group, $path, $componentID);
149 $id = CRM_Core_DAO::singleValueQuery("SELECT id FROM $table WHERE $where");
150 $now = date('Y-m-d H:i:s'); // FIXME - Use SQL NOW() or CRM_Utils_Time?
151 $dataSerialized = serialize($data);
152
153 // This table has a wonky index, so we cannot use REPLACE or
154 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
155 if ($id) {
156 $sql = "UPDATE $table SET data = %1, created_date = %2 WHERE id = %3";
157 $args = array(
158 1 => array($dataSerialized, 'String'),
159 2 => array($now, 'String'),
160 3 => array($id, 'Int'),
161 );
162 $dao = CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
163 }
164 else {
165 $insert = CRM_Utils_SQL_Insert::into($table)
166 ->row(array(
167 'group_name' => $group,
168 'path' => $path,
169 'component_id' => $componentID,
170 'data' => $dataSerialized,
171 'created_date' => $now,
172 ));
173 $dao = CRM_Core_DAO::executeQuery($insert->toSQL(), array(), TRUE, NULL, FALSE, FALSE);
174 }
175
176 $lock->release();
177
178 $dao->free();
179
180 // cache coherency - refresh or remove dependent caches
181
182 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
183 $cache = CRM_Utils_Cache::singleton();
184 $data = unserialize($dataSerialized);
185 self::$_cache[$argString] = $data;
186 $cache->set($argString, $data);
187
188 $argString = "CRM_CT_CI_{$group}_{$componentID}";
189 unset(self::$_cache[$argString]);
190 $cache->delete($argString);
191 }
192
193 /**
194 * Delete all the cache elements that belong to a group OR delete the entire cache if group is not specified.
195 *
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 $clearAll clear all caches
201 */
202 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
203 $table = self::getTableName();
204 $where = self::whereCache($group, $path, NULL);
205 CRM_Core_DAO::executeQuery("DELETE FROM $table WHERE $where");
206
207 if ($clearAll) {
208 // also reset ACL Cache
209 CRM_ACL_BAO_Cache::resetCache();
210
211 // also reset memory cache if any
212 CRM_Utils_System::flushCache();
213 }
214 }
215
216 /**
217 * The next two functions are internal functions used to store and retrieve session from
218 * the database cache. This keeps the session to a limited size and allows us to
219 * create separate session scopes for each form in a tab
220 */
221
222 /**
223 * This function takes entries from the session array and stores it in the cache.
224 *
225 * It also deletes the entries from the $_SESSION object (for a smaller session size)
226 *
227 * @param array $names
228 * Array of session values that should be persisted.
229 * This is either a form name + qfKey or just a form name
230 * (in the case of profile)
231 * @param bool $resetSession
232 * Should session state be reset on completion of DB store?.
233 */
234 public static function storeSessionToCache($names, $resetSession = TRUE) {
235 foreach ($names as $key => $sessionName) {
236 if (is_array($sessionName)) {
237 $value = NULL;
238 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
239 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
240 }
241 self::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}");
242 if ($resetSession) {
243 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
244 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
245 }
246 }
247 else {
248 $value = NULL;
249 if (!empty($_SESSION[$sessionName])) {
250 $value = $_SESSION[$sessionName];
251 }
252 self::setItem($value, 'CiviCRM Session', $sessionName);
253 if ($resetSession) {
254 $_SESSION[$sessionName] = NULL;
255 unset($_SESSION[$sessionName]);
256 }
257 }
258 }
259
260 self::cleanup();
261 }
262
263 /* Retrieve the session values from the cache and populate the $_SESSION array
264 *
265 * @param array $names
266 * Array of session values that should be persisted.
267 * This is either a form name + qfKey or just a form name
268 * (in the case of profile)
269 */
270
271 /**
272 * Restore session from cache.
273 *
274 * @param string $names
275 */
276 public static function restoreSessionFromCache($names) {
277 foreach ($names as $key => $sessionName) {
278 if (is_array($sessionName)) {
279 $value = self::getItem('CiviCRM Session',
280 "{$sessionName[0]}_{$sessionName[1]}"
281 );
282 if ($value) {
283 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
284 }
285 }
286 else {
287 $value = self::getItem('CiviCRM Session',
288 $sessionName
289 );
290 if ($value) {
291 $_SESSION[$sessionName] = $value;
292 }
293 }
294 }
295 }
296
297 /**
298 * Do periodic cleanup of the CiviCRM session table.
299 *
300 * Also delete all session cache entries which are a couple of days old.
301 * This keeps the session cache to a manageable size
302 * Delete Contribution page session caches more energetically.
303 *
304 * @param bool $session
305 * @param bool $table
306 * @param bool $prevNext
307 */
308 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
309 // first delete all sessions more than 20 minutes old which are related to any potential transaction
310 $timeIntervalMins = (int) Civi::settings()->get('secure_cache_timeout_minutes');
311 if ($timeIntervalMins && $session) {
312 $transactionPages = array(
313 'CRM_Contribute_Controller_Contribution',
314 'CRM_Event_Controller_Registration',
315 );
316
317 $params = array(
318 1 => array(
319 date('Y-m-d H:i:s', time() - $timeIntervalMins * 60),
320 'String',
321 ),
322 );
323 foreach ($transactionPages as $trPage) {
324 $params[] = array("%${trPage}%", 'String');
325 $where[] = 'path LIKE %' . count($params);
326 }
327
328 $sql = "
329 DELETE FROM civicrm_cache
330 WHERE group_name = 'CiviCRM Session'
331 AND created_date <= %1
332 AND (" . implode(' OR ', $where) . ")";
333 CRM_Core_DAO::executeQuery($sql, $params);
334 }
335 // clean up the session cache every $cacheCleanUpNumber probabilistically
336 $cleanUpNumber = 757;
337
338 // clean up all sessions older than $cacheTimeIntervalDays days
339 $timeIntervalDays = 2;
340
341 if (mt_rand(1, 100000) % $cleanUpNumber == 0) {
342 $session = $table = $prevNext = TRUE;
343 }
344
345 if (!$session && !$table && !$prevNext) {
346 return;
347 }
348
349 if ($prevNext) {
350 // delete all PrevNext caches
351 CRM_Core_BAO_PrevNextCache::cleanupCache();
352 }
353
354 if ($table) {
355 CRM_Core_Config::clearTempTables($timeIntervalDays . ' day');
356 }
357
358 if ($session) {
359
360 $sql = "
361 DELETE FROM civicrm_cache
362 WHERE group_name = 'CiviCRM Session'
363 AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY )
364 ";
365 CRM_Core_DAO::executeQuery($sql);
366 }
367 }
368
369 /**
370 * Compose a SQL WHERE clause for the cache.
371 *
372 * Note: We need to use the cache during bootstrap, so we don't have
373 * full access to DAO services.
374 *
375 * @param string $group
376 * @param string|NULL $path
377 * Filter by path. If NULL, then return any paths.
378 * @param int|NULL $componentID
379 * Filter by component. If NULL, then look for explicitly NULL records.
380 * @return string
381 */
382 protected static function whereCache($group, $path, $componentID) {
383 $clauses = array();
384 $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($group) . '"');
385 if ($path) {
386 $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"');
387 }
388 if ($componentID && is_numeric($componentID)) {
389 $clauses[] = ('component_id = ' . (int) $componentID);
390 }
391 return $clauses ? implode(' AND ', $clauses) : '(1)';
392 }
393
394 }