/** * 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); } }
/** * 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 * * @return void * @static * @access private */ 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) { // also delete all the action temp tables // that were created the same interval ago $dao = new CRM_Core_DAO(); $query = "\nSELECT TABLE_NAME as tableName\nFROM INFORMATION_SCHEMA.TABLES\nWHERE TABLE_SCHEMA = %1\nAND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'\n OR TABLE_NAME LIKE 'civicrm_export_temp_%'\n OR TABLE_NAME LIKE 'civicrm_import_job_%' )\nAND CREATE_TIME < date_sub( NOW( ), INTERVAL {$timeIntervalDays} day )\n"; $params = array(1 => array($dao->database(), 'String')); $tableDAO = CRM_Core_DAO::executeQuery($query, $params); $tables = array(); while ($tableDAO->fetch()) { $tables[] = $tableDAO->tableName; } if (!empty($tables)) { $table = implode(',', $tables); // drop leftover temporary tables CRM_Core_DAO::executeQuery("DROP TABLE {$table}"); } } 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 %' . sizeof($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); } }