diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index e19ff5128651..695f6f097df4 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -233,6 +233,7 @@ public function commonProcess(&$params) { } CRM_Core_Config::clearDBCache(); + Civi::cache('session')->clear(); // This doesn't make a lot of sense to me, but it maintains pre-existing behavior. CRM_Utils_System::flushCache(); CRM_Core_Resources::singleton()->resetCacheCode(); diff --git a/CRM/Admin/Form/Setting/UpdateConfigBackend.php b/CRM/Admin/Form/Setting/UpdateConfigBackend.php index 68711ff42aa6..b5c7bb8c57b8 100644 --- a/CRM/Admin/Form/Setting/UpdateConfigBackend.php +++ b/CRM/Admin/Form/Setting/UpdateConfigBackend.php @@ -65,6 +65,7 @@ public function postProcess() { // clear all caches CRM_Core_Config::clearDBCache(); + Civi::cache('session')->clear(); CRM_Utils_System::flushCache(); parent::rebuildMenu(); diff --git a/CRM/Core/BAO/Cache.php b/CRM/Core/BAO/Cache.php index a8bde2f16b3d..ce1d63ede35a 100644 --- a/CRM/Core/BAO/Cache.php +++ b/CRM/Core/BAO/Cache.php @@ -40,6 +40,13 @@ */ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache { + /** + * When store session/form state, how long should the data be retained? + * + * @var int, number of second + */ + const DEFAULT_SESSION_TTL = 172800; // Two days: 2*24*60*60 + /** * @var array ($cacheKey => $cacheValue) */ @@ -237,7 +244,8 @@ public static function storeSessionToCache($names, $resetSession = TRUE) { if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) { $value = $_SESSION[$sessionName[0]][$sessionName[1]]; } - self::setItem($value, 'CiviCRM Session', "{$sessionName[0]}_{$sessionName[1]}"); + $key = "{$sessionName[0]}_{$sessionName[1]}"; + Civi::cache('session')->set($key, $value, self::pickSessionTtl($key)); if ($resetSession) { $_SESSION[$sessionName[0]][$sessionName[1]] = NULL; unset($_SESSION[$sessionName[0]][$sessionName[1]]); @@ -248,7 +256,7 @@ public static function storeSessionToCache($names, $resetSession = TRUE) { if (!empty($_SESSION[$sessionName])) { $value = $_SESSION[$sessionName]; } - self::setItem($value, 'CiviCRM Session', $sessionName); + Civi::cache('session')->set($sessionName, $value, self::pickSessionTtl($sessionName)); if ($resetSession) { $_SESSION[$sessionName] = NULL; unset($_SESSION[$sessionName]); @@ -275,17 +283,13 @@ public static function storeSessionToCache($names, $resetSession = TRUE) { public static function restoreSessionFromCache($names) { foreach ($names as $key => $sessionName) { if (is_array($sessionName)) { - $value = self::getItem('CiviCRM Session', - "{$sessionName[0]}_{$sessionName[1]}" - ); + $value = Civi::cache('session')->get("{$sessionName[0]}_{$sessionName[1]}"); if ($value) { $_SESSION[$sessionName[0]][$sessionName[1]] = $value; } } else { - $value = self::getItem('CiviCRM Session', - $sessionName - ); + $value = Civi::cache('session')->get($sessionName); if ($value) { $_SESSION[$sessionName] = $value; } @@ -293,6 +297,32 @@ public static function restoreSessionFromCache($names) { } } + /** + * Determine how long session-state should be retained. + * + * @param string $sessionKey + * Ex: '_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654_container' + * Ex: 'CiviCRM_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654' + * @return int + * Number of seconds. + */ + protected static function pickSessionTtl($sessionKey) { + $secureSessionTimeoutMinutes = (int) Civi::settings()->get('secure_cache_timeout_minutes'); + if ($secureSessionTimeoutMinutes) { + $transactionPages = array( + 'CRM_Contribute_Controller_Contribution', + 'CRM_Event_Controller_Registration', + ); + foreach ($transactionPages as $transactionPage) { + if (strpos($sessionKey, $transactionPage) !== FALSE) { + return $secureSessionTimeoutMinutes * 60; + } + } + } + + return self::DEFAULT_SESSION_TTL; + } + /** * Do periodic cleanup of the CiviCRM session table. * @@ -305,32 +335,6 @@ public static function restoreSessionFromCache($names) { * @param bool $prevNext */ public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE, $expired = FALSE) { - // first delete all sessions more than 20 minutes old which are related to any potential transaction - $timeIntervalMins = (int) Civi::settings()->get('secure_cache_timeout_minutes'); - if ($timeIntervalMins && $session) { - $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 = " -DELETE FROM civicrm_cache -WHERE group_name = 'CiviCRM Session' -AND created_date <= %1 -AND (" . implode(' OR ', $where) . ")"; - CRM_Core_DAO::executeQuery($sql, $params); - } // clean up the session cache every $cacheCleanUpNumber probabilistically $cleanUpNumber = 757; @@ -355,13 +359,8 @@ public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FAL } if ($session) { - - $sql = " -DELETE FROM civicrm_cache -WHERE group_name = 'CiviCRM Session' -AND created_date < date_sub( NOW( ), INTERVAL $timeIntervalDays DAY ) -"; - CRM_Core_DAO::executeQuery($sql); + // Session caches are just regular caches, so they expire naturally per TTL. + $expired = TRUE; } if ($expired) { diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 8cc765328614..bb5340bbec4a 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -260,6 +260,7 @@ public static function doSiteMove($defaultValues = array()) { // clear all caches CRM_Core_Config::clearDBCache(); + Civi::cache('session')->clear(); $moveStatus .= ts('Database cache tables cleared.') . '
'; $resetSessionTable = CRM_Utils_Request::retrieve('resetSessionTable', diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 14baccd3510b..265532c67d8b 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -295,6 +295,7 @@ public function cleanupCaches($sessionReset = TRUE) { // clear all caches self::clearDBCache(); + Civi::cache('session')->clear(); CRM_Utils_System::flushCache(); if ($sessionReset) { diff --git a/CRM/Core/Session.php b/CRM/Core/Session.php index ccadd74bcda4..b81cc961f0f9 100644 --- a/CRM/Core/Session.php +++ b/CRM/Core/Session.php @@ -286,7 +286,7 @@ public function getVars(&$vars, $prefix = '') { $values = &$this->_session[$this->_key]; } else { - $values = CRM_Core_BAO_Cache::getItem('CiviCRM Session', "CiviCRM_{$prefix}"); + $values = Civi::cache('session')->get("CiviCRM_{$prefix}"); } if ($values) { diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index a137d166b56a..bb6ca7931960 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -164,6 +164,7 @@ public function createContainer() { 'js_strings' => 'js_strings', 'community_messages' => 'community_messages', 'checks' => 'checks', + 'session' => 'CiviCRM Session', ); foreach ($basicCaches as $cacheSvc => $cacheGrp) { $container->setDefinition("cache.{$cacheSvc}", new Definition(