Skip to content

Commit

Permalink
Convert custom data and CiviCRM Search PrevNextCache search groups
Browse files Browse the repository at this point in the history
Streamline caches calls as per Monish's comments

Split prevNextCache out on its own as it needs to be SQLbacked all the time for the moment whilst clearing is hard coded
  • Loading branch information
seamuslee001 committed Jun 14, 2019
1 parent 3e03283 commit 93a63a5
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 49 deletions.
3 changes: 1 addition & 2 deletions CRM/ACL/BAO/ACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,7 @@ public static function retrieve(&$params, &$defaults) {
*/
public static function setIsActive($id, $is_active) {
// note this also resets any ACL cache
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

return CRM_Core_DAO::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
}
Expand Down
3 changes: 1 addition & 2 deletions CRM/ACL/Form/ACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ public static function formRule($params) {
*/
public function postProcess() {
// note this also resets any ACL cache
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

if ($this->_action & CRM_Core_Action::DELETE) {
CRM_ACL_BAO_ACL::del($this->_id);
Expand Down
6 changes: 2 additions & 4 deletions CRM/Admin/Form/Setting/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ public function postProcess() {

//cache contact fields retaining localized titles
//though we changed localization, so reseting cache.
$contactCache = Civi::cache('fields');
$contactCache->flush();
Civi::cache('fields')->flush();

//CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
$navigationCache = Civi::cache('navigation');
$navigationCache->flush();
Civi::cache('navigation')->flush();

// we do this only to initialize monetary decimal point and thousand separator
$config = CRM_Core_Config::singleton();
Expand Down
2 changes: 1 addition & 1 deletion CRM/Campaign/Selector/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function buildPrevNextCache($sort) {

if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
// SQL-backed prevnext cache uses an extra record for pruning the cache.
CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
Civi::cache('prevNextCache')->set($cacheKey, $cacheKey);
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,13 +1317,7 @@ public static function importableFields(
$cacheKeyString .= $checkPermission ? '_1' : '_0';
$cacheKeyString .= '_' . CRM_Core_Config::domainID() . '_';

$fields = CRM_Utils_Array::value($cacheKeyString, self::$_importableFields);

$cache = Civi::cache('fields');
if (!$fields) {
// check if we can retrieve from database cache
$fields = $cache->get($cacheKeyString);
}
$fields = CRM_Utils_Array::value($cacheKeyString, self::$_importableFields) ?: Civi::cache('fields')->get($cacheKeyString);

if (!$fields) {
$fields = CRM_Contact_DAO_Contact::import();
Expand Down Expand Up @@ -1461,7 +1455,7 @@ public static function importableFields(
//Sorting fields in alphabetical order(CRM-1507)
$fields = CRM_Utils_Array::crmArraySortByField($fields, 'title');

$cache->set($cacheKeyString, $fields);
Civi::cache('fields')->set($cacheKeyString, $fields);
}

self::$_importableFields[$cacheKeyString] = $fields;
Expand Down Expand Up @@ -1519,8 +1513,7 @@ public static function &exportableFields($contactType = 'Individual', $status =
}

// check if we can retrieve from database cache
$cache = Civi::cache('fields');
$fields = $cache->get($cacheKeyString);
$fields = Civi::cache('fields')->get($cacheKeyString);

if (!$fields) {
$fields = CRM_Contact_DAO_Contact::export();
Expand Down Expand Up @@ -1710,7 +1703,7 @@ public static function &exportableFields($contactType = 'Individual', $status =
}
}

$cache->set($cacheKeyString, $fields);
Civi::cache('fields')->set($cacheKeyString, $fields);
}
self::$_exportableFields[$cacheKeyString] = $fields;
}
Expand Down
13 changes: 5 additions & 8 deletions CRM/Contact/BAO/GroupNestingCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public static function update() {
}

// this tree stuff is quite useful, so lets store it in the cache
$cache = Civi::cache('groups');
$cache->set('nestable tree hierarchy', $tree);
Civi::cache('groups')->set('nestable tree hierarchy', $tree);
}

/**
Expand Down Expand Up @@ -154,12 +153,11 @@ public static function isCyclic(&$tree, $id) {
* @return array
*/
public static function getPotentialCandidates($id, &$groups) {
$cache = Civi::cache('groups');
$tree = $cache->get('nestable tree hierarchy');
$tree = Civi::cache('groups')->get('nestable tree hierarchy');

if ($tree === NULL) {
self::update();
$tree = $cache->get('nestable tree hierarchy');
$tree = Civi::cache('groups')->get('nestable tree hierarchy');
}

$potential = $groups;
Expand Down Expand Up @@ -221,12 +219,11 @@ public static function getAll(&$all, &$tree, $id, $token) {
* @return string
*/
public static function json() {
$cache = Civi::cache('groups');
$tree = $cache->get('nestable tree hierarchy');
$tree = Civi::cache('groups')->get('nestable tree hierarchy');

if ($tree === NULL) {
self::update();
$tree = $cache->get('nestable tree hierarchy');
$tree = Civi::cache('groups')->get('nestable tree hierarchy');
}

// get all the groups
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ public function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = self::C

if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
// SQL-backed prevnext cache uses an extra record for pruning the cache.
CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
Civi::cache('prevNextCache')->set($cacheKey, $cacheKey);
}
}

Expand Down
3 changes: 0 additions & 3 deletions CRM/Core/BAO/Cache/Psr16.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ public static function clearDBCache() {
*/
public static function getLegacyGroups() {
return [
// Core
'CiviCRM Search PrevNextCache',
'custom data',

// Universe

Expand Down
5 changes: 2 additions & 3 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,8 @@ public static function &getFields(
if (!self::$_importFields) {
self::$_importFields = array();
}
$cache = Civi::cache('fields');
// check if we can retrieve from database cache
$fields = $cache->get("custom importableFields $cacheKey");
$fields = Civi::cache('fields')->get("custom importableFields $cacheKey");

if ($fields === NULL) {
$cfTable = self::getTableName();
Expand Down Expand Up @@ -677,7 +676,7 @@ public static function &getFields(
self::getOptionsForField($fields[$dao->id], $dao->option_group_name);

}
$cache->set("custom importableFields $cacheKey", $fields);
Civi::cache('fields')->set("custom importableFields $cacheKey", $fields);
}
self::$_importFields[$cacheKey] = $fields;
}
Expand Down
7 changes: 3 additions & 4 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ public static function retrieve(&$params, &$defaults) {
*/
public static function setIsActive($id, $is_active) {
// reset the cache
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

if (!$is_active) {
CRM_Core_BAO_UFField::setUFFieldStatus($id, $is_active);
Expand Down Expand Up @@ -1819,7 +1818,7 @@ public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form = NU

// fetch submitted custom field values later use to set as a default values
if ($qfKey) {
$submittedValues = CRM_Core_BAO_Cache::getItem('custom data', $qfKey);
$submittedValues = Civi::cache('customData')->get($qfKey);
}

foreach ($groupTree as $key => $value) {
Expand Down Expand Up @@ -1878,7 +1877,7 @@ public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form = NU
if (count($formValues)) {
$qf = $form->get('qfKey');
$form->assign('qfKey', $qf);
CRM_Core_BAO_Cache::setItem($formValues, 'custom data', $qf);
Civi::cache('customData')->set($qf, $formValues);
}

// hack for field type File
Expand Down
5 changes: 2 additions & 3 deletions CRM/Core/BAO/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ public static function getNavigationList() {
$config = CRM_Core_Config::singleton();

// check if we can retrieve from database cache
$cache = Civi::cache('navigation');
$navigations = $cache->get($cacheKeyString);
$navigations = Civi::cache('navigation')->get($cacheKeyString);

if (!$navigations) {
$domainID = CRM_Core_Config::domainID();
Expand All @@ -187,7 +186,7 @@ public static function getNavigationList() {
$navigations = [];
self::_getNavigationLabel($pidGroups[''], $navigations);

$cache->set($cacheKeyString, $navigations);
Civi::cache('navigation')->set($cacheKeyString, $navigations);
}
return $navigations;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/PrevNextCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public static function cleanupCache() {
AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
";
$params = [
1 => ['CiviCRM Search PrevNextCache', 'String'],
1 => [CRM_Core_BAO_Cache::cleanKey('CiviCRM Search PrevNextCache'), 'String'],
2 => [$cacheTimeIntervalDays, 'Integer'],
];
CRM_Core_DAO::executeQuery($sql, $params);
Expand Down
3 changes: 1 addition & 2 deletions CRM/Custom/Form/ChangeFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ public function postProcess() {
$customField->save();

// Reset cache for custom fields
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

CRM_Core_Session::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
[1 => $this->_values['label'], 2 => $dstHtmlType]
Expand Down
3 changes: 1 addition & 2 deletions CRM/Custom/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,7 @@ public function postProcess() {
$this->_id = $customField->id;

// reset the cache
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

$msg = '<p>' . ts("Custom field '%1' has been saved.", [1 => $customField->label]) . '</p>';

Expand Down
3 changes: 1 addition & 2 deletions CRM/Custom/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ public function postProcess() {
$group = CRM_Core_BAO_CustomGroup::create($params);

// reset the cache
$cache = Civi::cache('fields');
$cache->flush();
Civi::cache('fields')->flush();

if ($this->_action & CRM_Core_Action::UPDATE) {
CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group->title]), ts('Saved'), 'success');
Expand Down
11 changes: 11 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function createContainer() {
'fields' => 'contact fields',
'navigation' => 'navigation',
'groups' => 'contact groups',
'customData' => 'custom data',
];
foreach ($basicCaches as $cacheSvc => $cacheGrp) {
$container->setDefinition("cache.{$cacheSvc}", new Definition(
Expand All @@ -172,6 +173,16 @@ public function createContainer() {
))->setFactory('CRM_Utils_Cache::create');
}

$container->setDefinition("cache.prevNextCache", new Definition(
'CRM_Utils_Cache_Interface',
[
[
'name' => 'CiviCRM Search PrevNextCache',
'type' => ['SqlGroup'],
],
]
))->setFactory('CRM_Utils_Cache::create');

$container->setDefinition('sql_triggers', new Definition(
'Civi\Core\SqlTriggers',
[]
Expand Down

0 comments on commit 93a63a5

Please sign in to comment.