From 73b8877be57e9656fbee35e0c5102a7035408515 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 28 May 2021 13:58:27 +1200 Subject: [PATCH] [REF] Extract code to transfer groups from temp table to cache This continues efforts to have the 'worker functions' accept an array of groups, not just a single --- CRM/Contact/BAO/GroupContactCache.php | 51 +++++++++++++++++---------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index 5d7394428d..f263ad861e 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -381,25 +381,7 @@ WHERE id IN ( $groupIDs ) ->setCategory('gccache') ->setMemory(); self::buildGroupContactTempTable([$groupID], $groupContactsTempTable); - $tempTable = $groupContactsTempTable->getName(); - // Don't call clearGroupContactCache as we don't want to clear the cache dates - // The will get updated by updateCacheTime() below and not clearing the dates reduces - // the chance that loadAll() will try and rebuild at the same time. - $clearCacheQuery = " - DELETE g - FROM civicrm_group_contact_cache g - WHERE g.group_id = %1 "; - $params = [ - 1 => [$groupID, 'Integer'], - ]; - CRM_Core_DAO::executeQuery($clearCacheQuery, $params); - - CRM_Core_DAO::executeQuery( - "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id) - SELECT DISTINCT contact_id, group_id FROM $tempTable - "); - $groupContactsTempTable->drop(); - self::updateCacheTime([$groupID], TRUE); + self::updateCacheFromTempTable($groupContactsTempTable, [$groupID]); $lock->release(); } } @@ -811,4 +793,35 @@ AND civicrm_group_contact.group_id = $groupID "; return $processGroupIDs; } + /** + * Transfer the contact ids to the group cache table and update the cache time. + * + * @param \CRM_Utils_SQL_TempTable $groupContactsTempTable + * @param array $groupIDs + */ + private static function updateCacheFromTempTable(CRM_Utils_SQL_TempTable $groupContactsTempTable, array $groupIDs): void { + $tempTable = $groupContactsTempTable->getName(); + + // Don't call clearGroupContactCache as we don't want to clear the cache dates + // The will get updated by updateCacheTime() below and not clearing the dates reduces + // the chance that loadAll() will try and rebuild at the same time. + $clearCacheQuery = ' + DELETE g + FROM civicrm_group_contact_cache g + WHERE g.group_id IN (%1) '; + $params = [ + 1 => [implode(',', $groupIDs), 'CommaSeparatedIntegers'], + ]; + CRM_Core_DAO::executeQuery($clearCacheQuery, $params); + + CRM_Core_DAO::executeQuery( + "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id) + SELECT DISTINCT contact_id, group_id FROM $tempTable + "); + $groupContactsTempTable->drop(); + foreach ($groupIDs as $groupID) { + self::updateCacheTime([$groupID], TRUE); + } + } + } -- 2.25.1