Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some more legacy caches to standardise cache container #14487

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/ACL/BAO/ACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public static function retrieve(&$params, &$defaults) {
*/
public static function setIsActive($id, $is_active) {
// note this also resets any ACL cache
CRM_Core_BAO_Cache::deleteGroup('contact fields');
Civi::cache('fields')->flush();

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

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

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

//CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
CRM_Core_BAO_Cache::deleteGroup('navigation');
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten agreed that the effect of

Civi::cache('prevNextCache')->set($cacheKey, $cacheKey);

is a clumsy way of saying


Civi::cache('prevNextCache')->set($cacheKey,  TRUE);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear - I support setting the value in the same way it was set before. But Eileen had a question about whether it was meaningful to do set($cacheKey,$cacheKey), and (conceptually) these could be the same (although I'd make no assumption that they're actually equivalent in practice).

Also, note that CRM_Core_BAO_PrevNextCache::cleanupCache() specifically relies on this cache being stored in SQL. I'd recommend skeptical eye about using a cache-backend that is dynamically-chosen (as in prevNextCache' => 'CiviCRM Search PrevNextCache' ... 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'] in Container.php below) when there are hard-coded bits that assume this cache is SQL-backed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten good point have just pushed up change to ensure prevNextCache is sql Backed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten should CRM_Core_BAO_PrevNextCache:::cleanupCache() be aware of the backed provider of prev next service?

}
}
}
Expand Down
13 changes: 4 additions & 9 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,12 +1317,7 @@ public static function importableFields(
$cacheKeyString .= $checkPermission ? '_1' : '_0';
$cacheKeyString .= '_' . CRM_Core_Config::domainID() . '_';

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

if (!$fields) {
// check if we can retrieve from database cache
$fields = CRM_Core_BAO_Cache::getItem('contact fields', $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 @@ -1460,7 +1455,7 @@ public static function importableFields(
//Sorting fields in alphabetical order(CRM-1507)
$fields = CRM_Utils_Array::crmArraySortByField($fields, 'title');

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

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

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

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

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

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

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

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

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

if ($tree === NULL) {
self::update();
$tree = CRM_Core_BAO_Cache::getItem('contact groups', '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
6 changes: 0 additions & 6 deletions CRM/Core/BAO/Cache/Psr16.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ public static function clearDBCache() {
*/
public static function getLegacyGroups() {
$groups = [
// Core
'CiviCRM Search PrevNextCache',
'contact fields',
'navigation',
'contact groups',
'custom data',

// Universe

Expand Down
9 changes: 2 additions & 7 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();
}

// check if we can retrieve from database cache
$fields = CRM_Core_BAO_Cache::getItem('contact fields', "custom importableFields $cacheKey");
$fields = Civi::cache('fields')->get("custom importableFields $cacheKey");

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

}

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

if (!$is_active) {
CRM_Core_BAO_UFField::setUFFieldStatus($id, $is_active);
Expand Down Expand Up @@ -1818,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 @@ -1877,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
4 changes: 2 additions & 2 deletions CRM/Core/BAO/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function getNavigationList() {
$config = CRM_Core_Config::singleton();

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

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

CRM_Core_BAO_Cache::setItem($navigations, 'navigation', $cacheKeyString);
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
2 changes: 1 addition & 1 deletion CRM/Custom/Form/ChangeFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function postProcess() {
$customField->save();

// Reset cache for custom fields
CRM_Core_BAO_Cache::deleteGroup('contact fields');
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
2 changes: 1 addition & 1 deletion CRM/Custom/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ public function postProcess() {
$this->_id = $customField->id;

// reset the cache
CRM_Core_BAO_Cache::deleteGroup('contact fields');
Civi::cache('fields')->flush();

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

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

// reset the cache
CRM_Core_BAO_Cache::deleteGroup('contact fields');
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
2 changes: 1 addition & 1 deletion CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ public static function flushCache() {
// also reset the various static memory caches

// reset the memory or array cache
CRM_Core_BAO_Cache::deleteGroup('contact fields', NULL, FALSE);
Civi::cache('fields')->flush();

// reset ACL cache
CRM_ACL_BAO_Cache::resetCache();
Expand Down
14 changes: 14 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public function createContainer() {
'checks' => 'checks',
'session' => 'CiviCRM Session',
'long' => 'long',
'fields' => 'contact fields',
'navigation' => 'navigation',
'groups' => 'contact groups',
'customData' => 'custom data',
];
foreach ($basicCaches as $cacheSvc => $cacheGrp) {
$container->setDefinition("cache.{$cacheSvc}", new Definition(
Expand All @@ -169,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