Skip to content

Commit

Permalink
Rewrite subTypeInfo to use caching mechanism
Browse files Browse the repository at this point in the history
This seems to be getting a lot of cache misses on prod - presumably due to a lack of sub types
so fixing to use 'modern caching';
  • Loading branch information
eileenmcnaughton committed Jul 27, 2019
1 parent 0d3ea1d commit 670f518
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
64 changes: 22 additions & 42 deletions CRM/Contact/BAO/ContactType.php
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions CRM/Utils/System.php
Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion Civi/Core/Container.php
Expand Up @@ -159,6 +159,7 @@ public function createContainer() {
'navigation' => 'navigation',
'customData' => 'custom data',
'fields' => 'contact fields',
'contactSubTypes' => 'contactSubTypes',
];
foreach ($basicCaches as $cacheSvc => $cacheGrp) {
$definitionParams = [
Expand All @@ -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';
}
Expand Down

0 comments on commit 670f518

Please sign in to comment.