Skip to content

Commit

Permalink
CRM-21109 only clear caches once on cli script, consolidate code
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 6, 2017
1 parent 8005297 commit 154d0e0
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CRM/ACL/BAO/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public static function updateEntry($id) {
* Deletes all the cache entries.
*/
public static function resetCache() {
if (!CRM_Core_Config::isPermitCacheFlushMode()) {
return;
}
// reset any static caching
self::$_cache = NULL;

Expand Down
8 changes: 1 addition & 7 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,7 @@ public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE
'name'
);

if (!$config->doNotResetCache) {
// Note: doNotResetCache flag is currently set by import contact process and merging,
// since resetting and
// rebuilding cache could be expensive (for many contacts). We might come out with better
// approach in future.
CRM_Contact_BAO_Contact_Utils::clearContactCaches($contact->id);
}
CRM_Contact_BAO_Contact_Utils::clearContactCaches();

if ($invokeHooks) {
if ($isEdit) {
Expand Down
21 changes: 12 additions & 9 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,18 +897,21 @@ public static function getAddressShareContactNames(&$addresses) {
* caches, but are backing off from this with every release. Compromise between ease of coding versus
* performance versus being accurate at that very instant
*
* @param $contactID
* The contactID that was edited / deleted.
* @param bool $isEmptyPrevNextTable
* Should the civicrm_prev_next table be cleared of any contact entries.
* This is currently done from import but not other places and would
* likely affect user experience in unexpected ways. Existing behaviour retained
* ... reluctantly.
*/
public static function clearContactCaches($contactID = NULL) {
// clear acl cache if any.
CRM_ACL_BAO_Cache::resetCache();

if (empty($contactID)) {
// also clear prev/next dedupe cache - if no contactID passed in
public static function clearContactCaches($isEmptyPrevNextTable = FALSE) {
if (!CRM_Core_Config::isPermitCacheFlushMode()) {
return;
}
if ($isEmptyPrevNextTable) {
CRM_Core_BAO_PrevNextCache::deleteItem();
}

// clear acl cache if any.
CRM_ACL_BAO_Cache::resetCache();
CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
}

Expand Down
26 changes: 2 additions & 24 deletions CRM/Contact/BAO/GroupContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,7 @@ public static function addContactsToGroup(
list($numContactsAdded, $numContactsNotAdded)
= self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking);

// also reset the acl cache
$config = CRM_Core_Config::singleton();
if (!$config->doNotResetCache) {
CRM_ACL_BAO_Cache::resetCache();
}

// reset the group contact cache for all group(s)
// if this group is being used as a smart group
CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
CRM_Contact_BAO_Contact_Utils::clearContactCaches();

CRM_Utils_Hook::post('create', 'GroupContact', $groupId, $contactIds);

Expand Down Expand Up @@ -245,21 +237,7 @@ public static function removeContactsFromGroup(
}
}

// also reset the acl cache
$config = CRM_Core_Config::singleton();
if (!$config->doNotResetCache) {
CRM_ACL_BAO_Cache::resetCache();
}

// reset the group contact cache for all group(s)
// if this group is being used as a smart group
// @todo consider what to do here - it feels like we should either
// 1) just invalidate the specific group's cache(& perhaps any parents) & let cron do it's thing or
// possibly clear this specific groups cache, or just call opportunisticCacheFlush() - which would have the
// same effect as the remove call. The reservation about that is that it is no more aggressive for the group that
// we know is altered than for all the others, or perhaps, more the point with it's parents & groups that use it in
// their criteria.
CRM_Contact_BAO_GroupContactCache::remove();
CRM_Contact_BAO_Contact_Utils::clearContactCaches();

CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds);

Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public static function updateCacheTime($groupID, $processed) {
* @param bool $onceOnly
* run the function exactly once for all groups.
*/
public static function remove($groupIDs = NULL, $onceOnly = TRUE) {
protected static function remove($groupIDs = NULL, $onceOnly = TRUE) {
static $invoked = FALSE;

// typically this needs to happy only once per instance
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function preProcess() {

// get user info of main contact.
$config = CRM_Core_Config::singleton();
$config->doNotResetCache = 1;
CRM_Core_Config::setPermitCacheFlushMode(TRUE);

$mainUfId = CRM_Core_BAO_UFMatch::getUFId($this->_cid);
$mainUser = NULL;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function postProcess() {

// Clear all caches, forcing any searches to recheck the ACLs or group membership as the import
// may have changed it.
CRM_Contact_BAO_Contact_Utils::clearContactCaches();
CRM_Contact_BAO_Contact_Utils::clearContactCaches(TRUE);

// add all the necessary variables to the form
$importJob->setFormVariables($this);
Expand Down
7 changes: 3 additions & 4 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1717,11 +1717,10 @@ public function createContact(&$formatted, &$contactFields, $onDuplicate, $conta
$this->formatParams($formatted, $onDuplicate, (int) $contactId);
}

// pass doNotResetCache flag since resetting and rebuilding cache could be expensive.
$config = CRM_Core_Config::singleton();
$config->doNotResetCache = 1;
// Resetting and rebuilding cache could be expensive.
CRM_Core_Config::setPermitCacheFlushMode(TRUE);
$cid = CRM_Contact_BAO_Contact::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
$config->doNotResetCache = 0;
CRM_Core_Config::setPermitCacheFlushMode(FALSE);

$contact = array(
'contact_id' => $cid,
Expand Down
20 changes: 20 additions & 0 deletions CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,24 @@ public function handleFirstRun() {
Civi::settings()->set('installed', 1);
}

/**
* Is the system permitted to flush caches at the moment.
*
* This can be turned off by the
*/
static public function isPermitCacheFlushMode() {
return !CRM_Core_Config::singleton()->doNotResetCache;
}

/**
* Is the system permitted to flush caches at the moment.
*
* This can be turned off by the
*
* @param bool $enabled
*/
static public function setPermitCacheFlushMode($enabled) {
CRM_Core_Config::singleton()->doNotResetCache = $enabled ? 0 : 1;
}

}
4 changes: 1 addition & 3 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,7 @@ public static function merge($dupePairs = array(), $cacheParams = array(), $mode
$resultStats = array('merged' => array(), 'skipped' => array());

// we don't want dupe caching to get reset after every-merge, and therefore set the
// doNotResetCache flag
$config = CRM_Core_Config::singleton();
$config->doNotResetCache = 1;
CRM_Core_Config::setPermitCacheFlushMode(TRUE);
$deletedContacts = array();

while (!empty($dupePairs)) {
Expand Down
3 changes: 3 additions & 0 deletions bin/cli.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function _accessing_from_cli() {
public function callApi() {
require_once 'api/api.php';

CRM_Core_Config::setPermitCacheFlushMode(FALSE);
// CRM-9822 -'execute' action always goes thru Job api and always writes to log
if ($this->_action != 'execute' && $this->_joblog) {
require_once 'CRM/Core/JobManager.php';
Expand All @@ -111,6 +112,8 @@ public function callApi() {
$this->_params['auth'] = FALSE;
$result = civicrm_api($this->_entity, $this->_action, $this->_params);
}
CRM_Core_Config::setPermitCacheFlushMode(TRUE);
CRM_Contact_BAO_Contact_Utils::clearContactCaches();

if (!empty($result['is_error'])) {
$this->_log($result['error_message']);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public function testGroupClause() {
$this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $householdID, 'status' => 'Added'));

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_group_contact_cache");

$sql = CRM_Contact_BAO_Query::getQuery(
array(array('group', 'IN', array($groupID), 0, 0)),
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public function testCreateIndividualNoCacheClear() {

// Rinse & repeat, but with the option.
$this->putGroupContactCacheInClearableState($groupID, $contact);
$this->callAPISuccess('contact', 'create', array('id' => $contact['id'], 'options' => array('do_not_reset_cache' => 1)));
CRM_Core_Config::setPermitCacheFlushMode(TRUE);
$this->callAPISuccess('contact', 'create', array('id' => $contact['id']));
$this->assertEquals(1, CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_group_contact_cache"));
CRM_Core_Config::setPermitCacheFlushMode(FALSE);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testReportTemplateGetRowsContactSummary() {
$result = $this->callAPIAndDocument('report_template', 'getrows', array(
'report_id' => 'contact/summary',
'options' => array('metadata' => array('labels', 'title')),
), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows');
), __FUNCTION__, __FILE__, $description, 'Getrows');
$this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);

//the second part of this test has been commented out because it relied on the db being reset to
Expand Down Expand Up @@ -470,7 +470,7 @@ public function setUpPopulatedSmartGroup() {
}

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_group_contact_cache");
return $groupID;
}

Expand Down Expand Up @@ -510,7 +510,8 @@ public function setUpPopulatedGroup($returnAddedContact = FALSE) {
}

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_group_contact_cache");


if ($returnAddedContact) {
return array($groupID, $individualID);
Expand Down

0 comments on commit 154d0e0

Please sign in to comment.