diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 8ee3011f2779..b2316c4d40e6 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -155,59 +155,39 @@ public static function basicTypePairs($all = FALSE, $key = 'name') { * Array of sub type information */ public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE, $reset = FALSE) { - static $_cache = NULL; - - if ($reset === TRUE) { - $_cache = NULL; - } - - if ($_cache === NULL) { - $_cache = []; - } - if ($contactType && !is_array($contactType)) { - $contactType = [$contactType]; - } - $argString = $all ? 'CRM_CT_STI_1_' : 'CRM_CT_STI_0_'; if (!empty($contactType)) { - $argString .= implode('_', $contactType); + $argString .= implode('_', (array) $contactType); } + if (!Civi::cache('contactSubTypes')->has($argString) || $ignoreCache || $reset) { + $ctWHERE = ''; + if (!empty($contactType)) { + $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')"; + } - if ((!array_key_exists($argString, $_cache)) || $ignoreCache) { - $cache = CRM_Utils_Cache::singleton(); - $_cache[$argString] = $cache->get($argString); - if (!$_cache[$argString] || $ignoreCache) { - $_cache[$argString] = []; - - $ctWHERE = ''; - if (!empty($contactType)) { - $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')"; - } - - $sql = " + $sql = " SELECT subtype.*, parent.name as parent, parent.label as parent_label FROM civicrm_contact_type subtype INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE} "; - if ($all === FALSE) { - $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id"; - } - $dao = CRM_Core_DAO::executeQuery($sql, [], - FALSE, 'CRM_Contact_DAO_ContactType' - ); - while ($dao->fetch()) { - $value = []; - CRM_Core_DAO::storeValues($dao, $value); - $value['parent'] = $dao->parent; - $value['parent_label'] = $dao->parent_label; - $_cache[$argString][$dao->name] = $value; - } - - $cache->set($argString, $_cache[$argString]); + if ($all === FALSE) { + $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id"; } + $dao = CRM_Core_DAO::executeQuery($sql, [], + FALSE, 'CRM_Contact_DAO_ContactType' + ); + $values = []; + while ($dao->fetch()) { + $value = []; + CRM_Core_DAO::storeValues($dao, $value); + $value['parent'] = $dao->parent; + $value['parent_label'] = $dao->parent_label; + $values[$dao->name] = $value; + } + Civi::cache('contactSubTypes')->set($argString, $values); } - return $_cache[$argString]; + return Civi::cache('contactSubTypes')->get($argString); } /** diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 622e1aae9acc..40675d4f79ac 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -1442,6 +1442,7 @@ public static function flushCache() { Civi::cache('groups')->flush(); Civi::cache('navigation')->flush(); Civi::cache('customData')->flush(); + Civi::cache('contactSubTypes')->clear(); CRM_Extension_System::singleton()->getCache()->flush(); CRM_Cxn_CiviCxnHttp::singleton()->getCache()->flush(); } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 9c78f0b3126c..704c9cf370f7 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -159,6 +159,7 @@ public function createContainer() { 'navigation' => 'navigation', 'customData' => 'custom data', 'fields' => 'contact fields', + 'contactSubTypes' => 'contactSubTypes', ]; foreach ($basicCaches as $cacheSvc => $cacheGrp) { $definitionParams = [ @@ -168,7 +169,7 @@ public function createContainer() { // For Caches that we don't really care about the ttl for and/or maybe accessed // fairly often we use the fastArrayDecorator which improves reads and writes, these // caches should also not have concurrency risk. - $fastArrayCaches = ['groups', 'navigation', 'customData', 'fields']; + $fastArrayCaches = ['groups', 'navigation', 'customData', 'fields', 'contactSubTypes']; if (in_array($cacheSvc, $fastArrayCaches)) { $definitionParams['withArray'] = 'fast'; }