/** * Do periodic cleanup of the CiviCRM session table. * * Also delete all session cache entries which are a couple of days old. * This keeps the session cache to a manageable size * * @param bool $session * @param bool $table * @param bool $prevNext */ public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) { // clean up the session cache every $cacheCleanUpNumber probabilistically $cleanUpNumber = 757; // clean up all sessions older than $cacheTimeIntervalDays days $timeIntervalDays = 2; $timeIntervalMins = 30; if (mt_rand(1, 100000) % $cleanUpNumber == 0) { $session = $table = $prevNext = TRUE; } if (!$session && !$table && !$prevNext) { return; } if ($prevNext) { // delete all PrevNext caches CRM_Core_BAO_PrevNextCache::cleanupCache(); } if ($table) { CRM_Core_Config::clearTempTables($timeIntervalDays . ' day'); } if ($session) { // first delete all sessions which are related to any potential transaction // page $transactionPages = array('CRM_Contribute_Controller_Contribution', 'CRM_Event_Controller_Registration'); $params = array(1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String')); foreach ($transactionPages as $trPage) { $params[] = array("%{$trPage}%", 'String'); $where[] = 'path LIKE %' . count($params); } $sql = "\nDELETE FROM civicrm_cache\nWHERE group_name = 'CiviCRM Session'\nAND created_date <= %1\nAND (" . implode(' OR ', $where) . ")"; CRM_Core_DAO::executeQuery($sql, $params); $sql = "\nDELETE FROM civicrm_cache\nWHERE group_name = 'CiviCRM Session'\nAND created_date < date_sub( NOW( ), INTERVAL {$timeIntervalDays} DAY )\n"; CRM_Core_DAO::executeQuery($sql); } }