From: Eileen McNaughton Date: Thu, 18 Aug 2022 09:25:42 +0000 (+1200) Subject: Clean up another cache usage to use metadata X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d5fa49d9b37bd3bb516336c85a8fc45df2bb3a87;p=civicrm-core.git Clean up another cache usage to use metadata --- diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 34d440128d..2f70600ba3 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -266,6 +266,7 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { $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); diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index ccbe32f8ad..04e989436c 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -110,30 +110,28 @@ class CRM_Core_OptionGroup { * 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` @@ -144,7 +142,7 @@ WHERE v.option_group_id = g.id 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; @@ -155,7 +153,7 @@ WHERE v.option_group_id = g.id $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) { @@ -172,7 +170,6 @@ WHERE v.option_group_id = g.id // call option value hook CRM_Utils_Hook::optionValues($var, $name); - self::$_cache[$cacheKey] = $var; $cache->set($cacheKey, $var); return $var; diff --git a/CRM/Extension/Manager/Report.php b/CRM/Extension/Manager/Report.php index da14a38b21..06160d34fb 100644 --- a/CRM/Extension/Manager/Report.php +++ b/CRM/Extension/Manager/Report.php @@ -37,7 +37,7 @@ class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base { * * @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.')); @@ -67,7 +67,7 @@ class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base { 'is_active' => 1, ]; - $optionValue = CRM_Core_BAO_OptionValue::add($params); + CRM_Core_BAO_OptionValue::add($params); } /** @@ -108,14 +108,14 @@ class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base { * @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); } } diff --git a/CRM/Extension/Manager/Search.php b/CRM/Extension/Manager/Search.php index 13ec3bca1e..2f3910896c 100644 --- a/CRM/Extension/Manager/Search.php +++ b/CRM/Extension/Manager/Search.php @@ -105,15 +105,15 @@ class CRM_Extension_Manager_Search extends CRM_Extension_Manager_Base { /** * @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); } } diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index 6641cde5ad..2e53265144 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -293,7 +293,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { /** * 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', @@ -304,13 +304,13 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { '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']); } /**