Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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';
eileenmcnaughton committed Aug 2, 2019
1 parent f126dd0 commit 50a8e6f
Showing 3 changed files with 27 additions and 47 deletions.
70 changes: 24 additions & 46 deletions CRM/Contact/BAO/ContactType.php
Original file line number Diff line number Diff line change
@@ -149,65 +149,45 @@ public static function basicTypePairs($all = FALSE, $key = 'name') {
* ..
* @param bool $all
* @param bool $ignoreCache
* @param bool $reset
*
* @return array
* 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];
}

public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE) {
$argString = $all ? 'CRM_CT_STI_1_' : 'CRM_CT_STI_0_';
if (!empty($contactType)) {
$contactType = (array) $contactType;
$argString .= implode('_', $contactType);
}
if (!Civi::cache('contactSubTypes')->has($argString) || $ignoreCache) {
$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);
}

/**
@@ -680,9 +660,7 @@ public static function add(&$params) {
CRM_Core_BAO_Navigation::add($navigation);
}
CRM_Core_BAO_Navigation::resetNavigation();

// reset the cache after adding
self::subTypeInfo(NULL, FALSE, FALSE, TRUE);
Civi::cache('contactSubTypes')->clear();

return $contactType;
}
1 change: 1 addition & 0 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
@@ -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();
}
3 changes: 2 additions & 1 deletion Civi/Core/Container.php
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit 50a8e6f

Please sign in to comment.