Skip to content

Commit

Permalink
Merge pull request #19287 from eileenmcnaughton/group2
Browse files Browse the repository at this point in the history
Stop using refresh_date in civicrm_group table
  • Loading branch information
mattwire authored Jan 7, 2021
2 parents 215b898 + 826096b commit ed89dcb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
32 changes: 8 additions & 24 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function check($groupIDs) {
* @return string
* the sql query which lists the groups that need to be refreshed
*/
public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE) {
public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE): string {
$smartGroupCacheTimeoutDateTime = self::getCacheInvalidDateTime();

$query = "
Expand All @@ -78,7 +78,6 @@ public static function groupRefreshedClause($groupIDClause = NULL, $includeHidde
AND (
g.cache_date IS NULL
OR cache_date <= $smartGroupCacheTimeoutDateTime
OR NOW() >= g.refresh_date
)";

if (!$includeHiddenGroups) {
Expand Down Expand Up @@ -152,7 +151,7 @@ public static function loadAll($groupIDs = NULL, $limit = 0) {
$limitClause = $orderClause = NULL;
if ($limit > 0) {
$limitClause = " LIMIT 0, $limit";
$orderClause = " ORDER BY g.cache_date, g.refresh_date";
$orderClause = " ORDER BY g.cache_date";
}
// We ignore hidden groups and disabled groups
$query .= "
Expand All @@ -177,12 +176,10 @@ public static function loadAll($groupIDs = NULL, $limit = 0) {

if (!empty($refreshGroupIDs)) {
$refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs));
$time = self::getRefreshDateTime();
$query = "
UPDATE civicrm_group g
SET g.refresh_date = $time
SET g.cache_date = NOW()
WHERE g.id IN ( {$refreshGroupIDString} )
AND g.refresh_date IS NULL
";
CRM_Core_DAO::executeQuery($query);
}
Expand Down Expand Up @@ -252,17 +249,15 @@ public static function updateCacheTime($groupID, $processed) {
if ($processed) {
// also update the group with cache date information
$now = date('YmdHis');
$refresh = 'null';
}
else {
$now = 'null';
$refresh = 'null';
}

$groupIDs = implode(',', $groupID);
$sql = "
UPDATE civicrm_group
SET cache_date = $now, refresh_date = $refresh
SET cache_date = $now
WHERE id IN ( $groupIDs )
";
CRM_Core_DAO::executeQuery($sql);
Expand All @@ -284,7 +279,7 @@ public static function clearGroupContactCache($groupID) {

$update = "
UPDATE civicrm_group g
SET cache_date = null, refresh_date = null
SET cache_date = null
WHERE id = %1 ";

$params = [
Expand Down Expand Up @@ -327,7 +322,7 @@ protected static function flushCaches() {

// 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, refresh_date = NULL WHERE id IN ({$expiredGroups})");
CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = NULL WHERE id IN ({$expiredGroups})");
}
$lock->release();
}
Expand Down Expand Up @@ -381,7 +376,7 @@ public static function opportunisticCacheFlush() {
public static function deterministicCacheFlush() {
if (self::smartGroupCacheTimeout() == 0) {
CRM_Core_DAO::executeQuery("TRUNCATE civicrm_group_contact_cache");
CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = NULL, refresh_date = NULL");
CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = NULL");
}
else {
self::flushCaches();
Expand Down Expand Up @@ -669,24 +664,13 @@ public static function getCacheInvalidDateTime() {
return date('YmdHis', strtotime("-" . self::smartGroupCacheTimeout() . " Minutes"));
}

/**
* Get the date when the cache should be refreshed from.
*
* Ie. now + the offset & we will delete anything prior to then.
*
* @return string
*/
public static function getRefreshDateTime() {
return date('YmdHis', strtotime("+ " . self::smartGroupCacheTimeout() . " Minutes"));
}

/**
* Invalidates the smart group cache for a particular group
* @param int $groupID - Group to invalidate
*/
public static function invalidateGroupContactCache($groupID) {
CRM_Core_DAO::executeQuery("UPDATE civicrm_group
SET cache_date = NULL, refresh_date = NULL
SET cache_date = NULL
WHERE id = %1", [
1 => [$groupID, 'Positive'],
]);
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ protected function assertCacheRefreshed($group) {
);

$afterGroup = $this->callAPISuccessGetSingle('Group', ['id' => $group->id]);
$this->assertTrue(empty($afterGroup['cache_date']), 'refresh date should not be set as the cache is not built');
$this->assertTrue(empty($afterGroup['refresh_date']), 'refresh date should not be set as the cache is not built');
$this->assertTrue(empty($afterGroup['cache_date']), 'cache date should not be set as the cache is not built');
}

/**
Expand Down

0 comments on commit ed89dcb

Please sign in to comment.