Skip to content

Commit

Permalink
Further caching fix - use metadata cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 17, 2022
1 parent 783653b commit d36378f
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions CRM/Core/BAO/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public static function add(&$params, $ids = []) {

$optionValue->id = $id;
$optionValue->save();
Civi::cache('metadata')->flush();
CRM_Core_PseudoConstant::flush();

CRM_Utils_Hook::post($op, 'OptionValue', $id, $optionValue);
Expand Down Expand Up @@ -477,24 +478,24 @@ public static function updateOptionWeights($opGroupId, $opWeights) {
* an array of array of values for this option group
*/
public static function getOptionValuesArray($optionGroupID) {
global $tsLocale;
// check if we can get the field values from the system cache
$cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
$cache = CRM_Utils_Cache::singleton();
$optionValues = $cache->get($cacheKey);
if (empty($optionValues)) {
$dao = new CRM_Core_DAO_OptionValue();
$dao->option_group_id = $optionGroupID;
$dao->orderBy('weight ASC, label ASC');
$dao->find();

$optionValues = [];
while ($dao->fetch()) {
$optionValues[$dao->id] = [];
CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]);
}

$cache->set($cacheKey, $optionValues);
$cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}_$tsLocale";
if (Civi::cache('metadata')->has($cacheKey)) {
return Civi::cache('metadata')->get($cacheKey);
}
$dao = new CRM_Core_DAO_OptionValue();
$dao->option_group_id = $optionGroupID;
$dao->orderBy('weight ASC, label ASC');
$dao->find();

$optionValues = [];
while ($dao->fetch()) {
$optionValues[$dao->id] = [];
CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]);
}

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

return $optionValues;
}
Expand Down

0 comments on commit d36378f

Please sign in to comment.