From 435fc552134692f8b205a2f6b5d4b0f1a34391db Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 30 Jul 2019 07:55:34 +1000 Subject: [PATCH] [REF] Add in cleanup function to prevnext service and utilise in cleanup cache situation Set TTL on prevNextSQL Cache to be 2 days so cleanup code works as previously Switch to using a class constant for the number of cache days --- CRM/Campaign/Selector/Search.php | 3 ++- CRM/Contact/Selector.php | 3 ++- CRM/Core/BAO/PrevNextCache.php | 17 +---------------- CRM/Core/PrevNextCache/Interface.php | 5 +++++ CRM/Core/PrevNextCache/Redis.php | 8 ++++++++ CRM/Core/PrevNextCache/Sql.php | 26 ++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CRM/Campaign/Selector/Search.php b/CRM/Campaign/Selector/Search.php index 1342053314..de64a22cbf 100644 --- a/CRM/Campaign/Selector/Search.php +++ b/CRM/Campaign/Selector/Search.php @@ -296,7 +296,8 @@ FROM {$sql['from']} if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) { // SQL-backed prevnext cache uses an extra record for pruning the cache. - Civi::cache('prevNextCache')->set($cacheKey, $cacheKey); + // Also ensure that caches stay alive for 2 days a per previous code. + Civi::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql::cacheDays); } } } diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index ab894330c7..5533cb1bb5 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1063,7 +1063,8 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) { // SQL-backed prevnext cache uses an extra record for pruning the cache. - Civi::cache('prevNextCache')->set($cacheKey, $cacheKey); + // Also ensure that caches stay alive for 2 days as per previous code + Civi::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql::cacheDays); } } diff --git a/CRM/Core/BAO/PrevNextCache.php b/CRM/Core/BAO/PrevNextCache.php index 165b130e92..bcc8e650f8 100644 --- a/CRM/Core/BAO/PrevNextCache.php +++ b/CRM/Core/BAO/PrevNextCache.php @@ -467,22 +467,7 @@ WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2) } public static function cleanupCache() { - // clean up all prev next caches older than $cacheTimeIntervalDays days - $cacheTimeIntervalDays = 2; - - // first find all the cacheKeys that match this - $sql = " -DELETE pn, c -FROM civicrm_cache c -INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cachekey -WHERE c.group_name = %1 -AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) -"; - $params = [ - 1 => [CRM_Utils_Cache::cleanKey('CiviCRM Search PrevNextCache'), 'String'], - 2 => [$cacheTimeIntervalDays, 'Integer'], - ]; - CRM_Core_DAO::executeQuery($sql, $params); + Civi::service('prevnext')->cleanup(); } /** diff --git a/CRM/Core/PrevNextCache/Interface.php b/CRM/Core/PrevNextCache/Interface.php index c110007109..f0b72aa9c9 100644 --- a/CRM/Core/PrevNextCache/Interface.php +++ b/CRM/Core/PrevNextCache/Interface.php @@ -130,4 +130,9 @@ interface CRM_Core_PrevNextCache_Interface { */ public function fetch($cacheKey, $offset, $rowCount); + /** + * Remove items from prev/next cache no longer current + */ + public function cleanup(); + } diff --git a/CRM/Core/PrevNextCache/Redis.php b/CRM/Core/PrevNextCache/Redis.php index a08d9ba0c6..340f5c60a3 100644 --- a/CRM/Core/PrevNextCache/Redis.php +++ b/CRM/Core/PrevNextCache/Redis.php @@ -252,4 +252,12 @@ class CRM_Core_PrevNextCache_Redis implements CRM_Core_PrevNextCache_Interface { return [$allKey, $dataKey, $selKey, $maxScore]; } + /** + * @inheritDoc + */ + public function cleanup() { + // Redis already handles cleaning up stale keys. + return; + } + } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index 86e1034ff3..6784826561 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -32,6 +32,12 @@ */ class CRM_Core_PrevNextCache_Sql implements CRM_Core_PrevNextCache_Interface { + /** + * @var int + * Days for cache to llast forr + */ + const cacheDays = 2; + /** * Store the results of a SQL query in the cache. * @param string $cacheKey @@ -272,4 +278,24 @@ ORDER BY id return $cids; } + /** + * @inheritDoc + */ + public function cleanup() { + // clean up all prev next caches older than $cacheTimeIntervalDays days + // first find all the cacheKeys that match this + $sql = " + DELETE pn, c + FROM civicrm_cache c + INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cachekey + WHERE c.group_name = %1 + AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) + "; + $params = [ + 1 => [CRM_Core_BAO_Cache::cleanKey('CiviCRM Search PrevNextCache'), 'String'], + 2 => [self::cacheDays, 'Integer'], + ]; + CRM_Core_DAO::executeQuery($sql, $params); + } + } -- 2.25.1