Skip to content

Commit

Permalink
Clean up another cache usage to use metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 19, 2022
1 parent 5493e78 commit d5fa49d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
1 change: 1 addition & 0 deletions CRM/Core/BAO/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public static function del($optionValueId) {
$hookParams = ['id' => $optionValueId];
CRM_Utils_Hook::pre('delete', 'OptionValue', $optionValueId, $hookParams);
if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) {
Civi::cache('metadata')->flush();
CRM_Core_PseudoConstant::flush();
$optionValue->delete();
CRM_Utils_Hook::post('delete', 'OptionValue', $optionValueId, $optionValue);
Expand Down
25 changes: 11 additions & 14 deletions CRM/Core/OptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,28 @@ public static function &valuesCommon(
* The values as specified by the params
*/
public static function &values(
$name, $flip = FALSE, $grouping = FALSE,
string $name, $flip = FALSE, $grouping = FALSE,
$localize = FALSE, $condition = NULL,
$labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value',
$orderBy = 'weight'
) {
$cache = CRM_Utils_Cache::singleton();

if (self::isDomainOptionGroup($name)) {
$cacheKey = self::createCacheKey($name, CRM_Core_I18n::getLocale(), $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy, CRM_Core_Config::domainID());
}
else {
$cacheKey = self::createCacheKey($name, CRM_Core_I18n::getLocale(), $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy);
}

$cache = Civi::cache('metadata');
if (!$fresh) {
// Fetch from static var
if (array_key_exists($cacheKey, self::$_cache)) {
return self::$_cache[$cacheKey];
}
// Fetch from main cache
self::$_cache[$cacheKey] = $cache->get($cacheKey);
if (self::$_cache[$cacheKey] !== NULL) {
return self::$_cache[$cacheKey];
if ($cache->has($cacheKey)) {
$result = $cache->get($cacheKey);
return $result;
}
}
else {
CRM_Core_Error::deprecatedWarning('do not call to flush cache');
}

$query = "
SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as `grouping`
Expand All @@ -144,7 +142,7 @@ public static function &values(
AND g.is_active = 1 ";

if ($onlyActive) {
$query .= " AND v.is_active = 1 ";
$query .= ' AND v.is_active = 1 ';
// Only show options for enabled components
$componentClause = ' v.component_id IS NULL ';
$enabledComponents = CRM_Core_Config::singleton()->enableComponents;
Expand All @@ -155,7 +153,7 @@ public static function &values(
$query .= " AND ($componentClause) ";
}
if (self::isDomainOptionGroup($name)) {
$query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
$query .= ' AND v.domain_id = ' . CRM_Core_Config::domainID();
}

if ($condition) {
Expand All @@ -172,7 +170,6 @@ public static function &values(
// call option value hook
CRM_Utils_Hook::optionValues($var, $name);

self::$_cache[$cacheKey] = $var;
$cache->set($cacheKey, $var);

return $var;
Expand Down
8 changes: 4 additions & 4 deletions CRM/Extension/Manager/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getGroupId() {
*
* @throws CRM_Core_Exception
*/
public function onPreInstall(CRM_Extension_Info $info) {
public function onPreInstall(CRM_Extension_Info $info): void {
$customReports = $this->getCustomReportsByName();
if (array_key_exists($info->key, $customReports)) {
throw new CRM_Core_Exception(ts('This report is already registered.'));
Expand Down Expand Up @@ -67,7 +67,7 @@ public function onPreInstall(CRM_Extension_Info $info) {
'is_active' => 1,
];

$optionValue = CRM_Core_BAO_OptionValue::add($params);
CRM_Core_BAO_OptionValue::add($params);
}

/**
Expand Down Expand Up @@ -108,14 +108,14 @@ public function onPreEnable(CRM_Extension_Info $info) {
* @return array
*/
public function getCustomReportsByName() {
return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE);
}

/**
* @return array
*/
public function getCustomReportsById() {
return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE);
}

}
8 changes: 4 additions & 4 deletions CRM/Extension/Manager/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ public function onPreEnable(CRM_Extension_Info $info) {
/**
* @return array
*/
protected function getCustomSearchesByName() {
return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
protected function getCustomSearchesByName(): array {
return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE);
}

/**
* @return array
*/
protected function getCustomSearchesById() {
return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
protected function getCustomSearchesById(): array {
return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE);
}

}
12 changes: 6 additions & 6 deletions tests/phpunit/api/v3/OptionValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function testCreateOptionNoName() {
/**
* Check that pseudoconstant reflects new value added.
*/
public function testCRM11876CreateOptionPseudoConstantUpdated() {
public function testCRM11876CreateOptionPseudoConstantUpdated(): void {
$optionGroupID = $this->callAPISuccess('option_group', 'getvalue', [
'name' => 'payment_instrument',
'return' => 'id',
Expand All @@ -304,13 +304,13 @@ public function testCRM11876CreateOptionPseudoConstantUpdated() {
'label' => $newOption,
]);

$fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
$this->assertTrue(in_array($newOption, $fields['values']));
$fields = $this->callAPISuccess('Contribution', 'getoptions', ['field' => 'payment_instrument_id']);
$this->assertContains($newOption, $fields['values']);

$this->callAPISuccess('option_value', 'delete', ['id' => $apiResult['id']]);
$this->callAPISuccess('OptionValue', 'delete', ['id' => $apiResult['id']]);

$fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
$this->assertFalse(in_array($newOption, $fields['values']));
$fields = $this->callAPISuccess('Contribution', 'getoptions', ['field' => 'payment_instrument_id']);
$this->assertNotContains($newOption, $fields['values']);
}

/**
Expand Down

0 comments on commit d5fa49d

Please sign in to comment.