From 269d50c4d04bbdb1d5ffb7a915a3378ca733d9d4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 25 Jun 2018 16:20:16 -0700 Subject: [PATCH] (dev/core#174) CRM_Core_BAO_Cache - Periodically flush expired records --- CRM/Core/BAO/Cache.php | 14 +++++++++++--- api/v3/Job.php | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/Cache.php b/CRM/Core/BAO/Cache.php index 271bc71ddede..a8bde2f16b3d 100644 --- a/CRM/Core/BAO/Cache.php +++ b/CRM/Core/BAO/Cache.php @@ -304,7 +304,7 @@ public static function restoreSessionFromCache($names) { * @param bool $table * @param bool $prevNext */ - public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) { + 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) { @@ -338,10 +338,10 @@ public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FAL $timeIntervalDays = 2; if (mt_rand(1, 100000) % $cleanUpNumber == 0) { - $session = $table = $prevNext = TRUE; + $expired = $session = $table = $prevNext = TRUE; } - if (!$session && !$table && !$prevNext) { + if (!$session && !$table && !$prevNext && !$expired) { return; } @@ -363,6 +363,14 @@ public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FAL "; CRM_Core_DAO::executeQuery($sql); } + + if ($expired) { + $sql = "DELETE FROM civicrm_cache WHERE expired_date < %1"; + $params = [ + 1 => [date(CRM_Utils_Cache_SqlGroup::TS_FMT, CRM_Utils_Time::getTimeRaw()), 'String'], + ]; + CRM_Core_DAO::executeQuery($sql, $params); + } } /** diff --git a/api/v3/Job.php b/api/v3/Job.php index 3504a766f6ed..ab655369d1b5 100644 --- a/api/v3/Job.php +++ b/api/v3/Job.php @@ -619,14 +619,15 @@ function civicrm_api3_job_cleanup($params) { $session = CRM_Utils_Array::value('session', $params, TRUE); $tempTable = CRM_Utils_Array::value('tempTables', $params, TRUE); $jobLog = CRM_Utils_Array::value('jobLog', $params, TRUE); + $expired = CRM_Utils_Array::value('expiredDbCache', $params, TRUE); $prevNext = CRM_Utils_Array::value('prevNext', $params, TRUE); $dbCache = CRM_Utils_Array::value('dbCache', $params, FALSE); $memCache = CRM_Utils_Array::value('memCache', $params, FALSE); $tplCache = CRM_Utils_Array::value('tplCache', $params, FALSE); $wordRplc = CRM_Utils_Array::value('wordRplc', $params, FALSE); - if ($session || $tempTable || $prevNext) { - CRM_Core_BAO_Cache::cleanup($session, $tempTable, $prevNext); + if ($session || $tempTable || $prevNext || $expired) { + CRM_Core_BAO_Cache::cleanup($session, $tempTable, $prevNext, $expired); } if ($jobLog) {