Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flushCaches should respect permitCacheFlushMode. Also flush caches which have a NULL cache_date #21430

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,32 @@ protected static function flushCaches() {
// Someone else is kindly doing the refresh for us right now.
return;
}

// Get the list of expired smart groups that may need flushing
$params = [1 => [self::getCacheInvalidDateTime(), 'String']];
$groupsDAO = CRM_Core_DAO::executeQuery("SELECT id FROM civicrm_group WHERE cache_date <= %1", $params);
$groupsThatMayNeedToBeFlushedSQL = "SELECT id FROM civicrm_group WHERE (saved_search_id IS NOT NULL OR children <> '') AND (cache_date <= %1 OR cache_date IS NULL)";
$groupsDAO = CRM_Core_DAO::executeQuery($groupsThatMayNeedToBeFlushedSQL, $params);
$expiredGroups = [];
while ($groupsDAO->fetch()) {
$expiredGroups[] = $groupsDAO->id;
}
if (!empty($expiredGroups)) {
$expiredGroups = implode(',', $expiredGroups);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_group_contact_cache WHERE group_id IN ({$expiredGroups})");
if (empty($expiredGroups)) {
// There are no expired smart groups to flush
return;
}

$expiredGroupsCSV = implode(',', $expiredGroups);
$flushSQLParams = [1 => [$expiredGroupsCSV, 'CommaSeparatedIntegers']];
// Now check if we actually have any entries in the smart groups to flush
$groupsHaveEntriesToFlushSQL = 'SELECT group_id FROM civicrm_group_contact_cache gc WHERE group_id IN (%1) LIMIT 1';
$groupsHaveEntriesToFlush = (bool) CRM_Core_DAO::singleValueQuery($groupsHaveEntriesToFlushSQL, $flushSQLParams);

if ($groupsHaveEntriesToFlush) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_group_contact_cache WHERE group_id IN (%1)", [1 => [$expiredGroupsCSV, 'CommaSeparatedIntegers']]);

// Clear these out without resetting them because we are not building caches here, only clearing them,
// so the state is 'as if they had never been built'.
CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = NULL WHERE id IN ({$expiredGroups})");
CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = NULL WHERE id IN (%1)", [1 => [$expiredGroupsCSV, 'CommaSeparatedIntegers']]);
}
$lock->release();
}
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ public function testAddCreateIndividual(int $version): void {
* @throws \CRM_Core_Exception
*/
public function testCreateIndividualNoCacheClear(): void {

$contact = $this->callAPISuccess('contact', 'create', $this->_params);
$groupID = $this->groupCreate();

$smartGroupParams = ['form_values' => ['contact_type' => ['IN' => ['Household']]]];
$savedSearch = CRM_Contact_BAO_SavedSearch::create($smartGroupParams);
$groupID = $this->groupCreate(['saved_search_id' => $savedSearch->id]);

$this->putGroupContactCacheInClearableState($groupID, $contact);

Expand Down