Skip to content

Commit

Permalink
(dev/core#217) CRM_Contact_Selector::rebuildPreNextCache - Use PrevNe…
Browse files Browse the repository at this point in the history
…xtCache::fillWithArray()
  • Loading branch information
totten committed Jun 30, 2018
1 parent 9992836 commit f1e68af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
17 changes: 8 additions & 9 deletions CRM/Contact/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,18 +1085,17 @@ public function rebuildPreNextCache($start, $end, $sort, $cacheKey) {
$dao = CRM_Core_DAO::executeQuery($sql);

// build insert query, note that currently we build cache for 500 (self::CACHE_SIZE) contact records at a time, hence below approach
$insertValues = array();
$rows = [];
while ($dao->fetch()) {
$insertValues[] = "('civicrm_contact', {$dao->contact_id}, {$dao->contact_id}, '{$cacheKey}', '" . CRM_Core_DAO::escapeString($dao->sort_name) . "')";
$rows[] = [
'entity_table' => 'civicrm_contact',
'entity_id1' => $dao->contact_id,
'entity_id2' => $dao->contact_id,
'data' => $dao->sort_name,
];
}

//update pre/next cache using single insert query
if (!empty($insertValues)) {
$sql = 'INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES
' . implode(',', $insertValues);

$result = CRM_Core_DAO::executeQuery($sql);
}
Civi::service('prevnext')->fillWithArray($cacheKey, $rows);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions CRM/Core/PrevNextCache/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ interface CRM_Core_PrevNextCache_Interface {
/**
* Store the results of a SQL query in the cache.
*
* @param string $cacheKey
* @param string $sql
* A SQL query. The query *MUST* be a SELECT statement which yields
* the following columns (in order): entity_table, entity_id1, entity_id2, cacheKey, data
* @return bool
*/
public function fillWithSql($cacheKey, $sql);

/**
* Store the contents of an array in the cache.
*
* @param string $cacheKey
* @param array $rows
* A list of cache records. Each record should have keys:
* - entity_table
* - entity_id1
* - entity_id2
* - data
* @return bool
*/
public function fillWithArray($cacheKey, $rows);

/**
* Fetch a list of contacts from the prev/next cache for displaying a search results page
*
Expand Down
4 changes: 4 additions & 0 deletions CRM/Core/PrevNextCache/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function fillWithSql($cacheKey, $sql) {
throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__);
}

public function fillWithArray($cacheKey, $rows) {
throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__);
}

public function fetch($cacheKey, $offset, $rowCount, $includeContactIds, $queryBao) {
throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__);
}
Expand Down
21 changes: 21 additions & 0 deletions CRM/Core/PrevNextCache/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ public function fillWithSql($cacheKey, $sql) {
return TRUE;
}

public function fillWithArray($cacheKey, $rows) {
if (empty($rows)) {
return;
}

$insert = CRM_Utils_SQL_Insert::into('civicrm_prevnext_cache')
->columns([
'entity_table',
'entity_id1',
'entity_id2',
'cacheKey',
'data'
]);

foreach ($rows as &$row) {
$insert->row($row + ['cacheKey' => $cacheKey]);
}

CRM_Core_DAO::executeQuery($insert->toSQL());
}

/**
* Fetch a list of contacts from the prev/next cache for displaying a search results page
*
Expand Down

0 comments on commit f1e68af

Please sign in to comment.