From e78d0c1edbcaaa9f03dad964d2cd70893d0325ab Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 6 Sep 2017 14:29:15 +1200 Subject: [PATCH] CRM-21109 only clear caches once on cli script, consolidate code --- CRM/ACL/BAO/Cache.php | 3 +++ CRM/Contact/BAO/Contact.php | 8 +------- CRM/Contact/BAO/Contact/Utils.php | 21 ++++++++++++--------- CRM/Contact/BAO/GroupContact.php | 26 ++------------------------ CRM/Contact/BAO/GroupContactCache.php | 2 +- CRM/Contact/Form/Merge.php | 2 +- CRM/Contact/Import/Form/Preview.php | 2 +- CRM/Contact/Import/Parser/Contact.php | 7 +++---- CRM/Core/Config.php | 20 ++++++++++++++++++++ CRM/Dedupe/Merger.php | 4 +--- bin/cli.class.php | 3 +++ 11 files changed, 48 insertions(+), 50 deletions(-) diff --git a/CRM/ACL/BAO/Cache.php b/CRM/ACL/BAO/Cache.php index 392d15f48a66..1bb9bf5ca0c8 100644 --- a/CRM/ACL/BAO/Cache.php +++ b/CRM/ACL/BAO/Cache.php @@ -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; diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 161c12e38c49..bf03bed75943 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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) { diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index 024ef8504203..89d8394b6214 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -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(); } diff --git a/CRM/Contact/BAO/GroupContact.php b/CRM/Contact/BAO/GroupContact.php index 8833d1958d48..73bf59b76b8a 100644 --- a/CRM/Contact/BAO/GroupContact.php +++ b/CRM/Contact/BAO/GroupContact.php @@ -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); @@ -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); diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index 3840a0f2840f..a66c54b4b674 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -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 diff --git a/CRM/Contact/Form/Merge.php b/CRM/Contact/Form/Merge.php index 6381852a5792..133f1ca78659 100644 --- a/CRM/Contact/Form/Merge.php +++ b/CRM/Contact/Form/Merge.php @@ -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; diff --git a/CRM/Contact/Import/Form/Preview.php b/CRM/Contact/Import/Form/Preview.php index 26c8e14641b0..ca8aff581822 100644 --- a/CRM/Contact/Import/Form/Preview.php +++ b/CRM/Contact/Import/Form/Preview.php @@ -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); diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 7c4816d30c7a..3546e204e8e0 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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, diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 2a61e5eb30a4..89dc4fe74d44 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -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 = (bool) $enabled; + } + } diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index d52d84340b4d..b2628ab3760a 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -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)) { diff --git a/bin/cli.class.php b/bin/cli.class.php index 195792554074..7f432deb9309 100644 --- a/bin/cli.class.php +++ b/bin/cli.class.php @@ -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'; @@ -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']);