Skip to content

Commit

Permalink
Merge pull request #19930 from eileenmcnaughton/acl_setting
Browse files Browse the repository at this point in the history
dev/core#2477 Add setting to allow opportunistic cache flushing for acls
  • Loading branch information
seamuslee001 authored Mar 28, 2021
2 parents 5bf266b + 6776968 commit 17a063b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
15 changes: 14 additions & 1 deletion CRM/ACL/BAO/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,23 @@ public static function deleteEntry($id) {
CRM_Core_DAO::executeQuery($query, $params);
}

/**
* Do an opportunistic cache refresh if the site is configured for these.
*
* Sites that use acls and do not run the acl cache clearing cron job should
* refresh the caches on demand. The user session will be forced to wait
* and this is a common source of deadlocks, so it is less ideal.
*/
public static function opportunisticCacheFlush(): void {
if (Civi::settings()->get('acl_cache_refresh_mode') === 'opportunistic') {
self::resetCache();
}
}

/**
* Deletes all the cache entries.
*/
public static function resetCache() {
public static function resetCache(): void {
if (!CRM_Core_Config::isPermitCacheFlushMode()) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public static function getAddressShareContactNames(&$addresses) {
* likely affect user experience in unexpected ways. Existing behaviour retained
* ... reluctantly.
*/
public static function clearContactCaches($isEmptyPrevNextTable = FALSE) {
public static function clearContactCaches($isEmptyPrevNextTable = FALSE): void {
if (!CRM_Core_Config::isPermitCacheFlushMode()) {
return;
}
Expand All @@ -876,8 +876,8 @@ public static function clearContactCaches($isEmptyPrevNextTable = FALSE) {
Civi::service('prevnext')->deleteItem();
CRM_Core_BAO_PrevNextCache::deleteItem();
}
// clear acl cache if any.
CRM_ACL_BAO_Cache::resetCache();

CRM_ACL_BAO_Cache::opportunisticCacheFlush();
CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
}

Expand Down
32 changes: 26 additions & 6 deletions api/v3/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,21 +711,41 @@ function civicrm_api3_job_group_rebuild($params) {
/**
* Flush smart groups caches.
*
* This job purges aged smart group cache data (based on the timeout value). Sites can decide whether they want this
* job and / or the group cache rebuild job to run. In some cases performance is better when old caches are cleared out
* prior to any attempt to rebuild them. Also, many sites are very happy to have caches built on demand, provided the
* user is not having to wait for deadlocks to clear when invalidating them.
* This job purges aged smart group cache data (based on the timeout value).
* Sites can decide whether they want this job and / or the group cache rebuild
* job to run. In some cases performance is better when old caches are cleared
* out prior to any attempt to rebuild them. Also, many sites are very happy to
* have caches built on demand, provided the user is not having to wait for
* deadlocks to clear when invalidating them.
*
* @param array $params
*
* @return array
* @throws \API_Exception
* @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_job_group_cache_flush($params) {
function civicrm_api3_job_group_cache_flush(array $params): array {
CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
return civicrm_api3_create_success();
}

/**
* Flush acl caches.
*
* This job flushes the acl cache. For many sites it is better to do
* this by cron (or not at all if acls are not used) than whenever
* a contact is edited.
*
* @param array $params
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_job_acl_cache_flush(array $params): array {
CRM_ACL_BAO_Cache::resetCache();
return civicrm_api3_create_success();
}

/**
* Check for CiviCRM software updates.
*
Expand Down
19 changes: 18 additions & 1 deletion settings/Core.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,26 @@
'pseudoconstant' => [
'callback' => 'CRM_Contact_BAO_GroupContactCache::getModes',
],
'description' => ts('Should the smart groups be by cron jobs or user actions'),
'description' => ts('Should the smart groups be flushed by cron jobs or user actions'),
'help_text' => ts('In "Opportunistic Flush" mode, caches are flushed in response to user actions; this mode is broadly compatible but may add latency during form-submissions. In "Cron Flush" mode, you should schedule a cron job to flush caches; this can improve latency on form-submissions but requires more setup.'),
],
'acl_cache_refresh_mode' => [
'group_name' => 'CiviCRM Preferences',
'group' => 'core',
'name' => 'acl_cache_refresh_mode',
'type' => 'String',
'html_type' => 'radio',
'default' => 'opportunistic',
'add' => '5.37.0',
'title' => ts('ACL Group Refresh Mode'),
'is_domain' => 1,
'is_contact' => 0,
'pseudoconstant' => [
'callback' => 'CRM_Contact_BAO_GroupContactCache::getModes',
],
'description' => ts('Should the acl cache be by cron jobs or user actions'),
'help_text' => ts('In "Opportunistic Flush" mode, caches are flushed in response to user actions; this mode is broadly compatible but may add latency during form-submissions. In "Cron Flush" mode, you should schedule a cron job to flush caches if your site uses ACLs; this can improve latency on form-submissions but requires more setup.'),
],
'installed' => [
'bootstrap_comment' => 'This is a boot setting which may be loaded during bootstrap. Defaults are loaded via SettingsBag::getSystemDefaults().',
'group_name' => 'CiviCRM Preferences',
Expand Down

0 comments on commit 17a063b

Please sign in to comment.