Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#4501 Redis - fix ttl on prev-next cache #27115

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions CRM/Core/PrevNextCache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class CRM_Core_PrevNextCache_Redis implements CRM_Core_PrevNextCache_Interface {

const TTL = 21600;
private const TTL = 21600;

/**
* @var Redis
Expand All @@ -45,28 +45,53 @@ public function __construct($settings) {
$this->prefix .= \CRM_Utils_Cache::DELIMITER . 'prevnext' . \CRM_Utils_Cache::DELIMITER;
}

/**
* Get the time-to-live.
*
* This is likely to be made configurable in future.
*
* @return int
*/
public function getTTL() : int {
return self::TTL;
}

public function fillWithSql($cacheKey, $sql, $sqlParams = []) {
$dao = CRM_Core_DAO::executeQuery($sql, $sqlParams, FALSE);

[$allKey, $dataKey, , $maxScore] = $this->initCacheKey($cacheKey);

$first = TRUE;
while ($dao->fetch()) {
[, $entity_id, $data] = array_values($dao->toArray());
$maxScore++;
$this->redis->zAdd($allKey, $maxScore, $entity_id);
if ($first) {
$this->redis->expire($allKey, $this->getTTL());
}
$this->redis->hSet($dataKey, $entity_id, $data);
if ($first) {
$this->redis->expire($dataKey, $this->getTTL());
}
$first = FALSE;
}

return TRUE;
}

public function fillWithArray($cacheKey, $rows) {
[$allKey, $dataKey, , $maxScore] = $this->initCacheKey($cacheKey);

$first = TRUE;
foreach ($rows as $row) {
$maxScore++;
$this->redis->zAdd($allKey, $maxScore, $row['entity_id1']);
if ($first) {
$this->redis->expire($allKey, $this->getTTL());
}
$this->redis->hSet($dataKey, $row['entity_id1'], $row['data']);
if ($first) {
$this->redis->expire($dataKey, $this->getTTL());
}
$first = FALSE;
}

return TRUE;
Expand All @@ -82,14 +107,19 @@ public function markSelection($cacheKey, $action, $ids = NULL) {
$selKey = $this->key($cacheKey, 'sel');

if ($action === 'select') {
$first = TRUE;
foreach ((array) $ids as $id) {
$score = $this->redis->zScore($allKey, $id);
$this->redis->zAdd($selKey, $score, $id);
if ($first) {
$this->redis->expire($selKey, $this->getTTL());
}
$first = FALSE;
}
}
elseif ($action === 'unselect' && $ids === NULL) {
$this->redis->del($selKey);
$this->redis->expire($selKey, self::TTL);
$this->redis->expire($selKey, $this->getTTL());
}
elseif ($action === 'unselect' && $ids !== NULL) {
foreach ((array) $ids as $id) {
Expand All @@ -112,16 +142,14 @@ public function getSelection($cacheKey, $action = 'get') {
}
return [$cacheKey => $result];
}
elseif ($action === 'getall') {
if ($action === 'getall') {
$result = [];
foreach ($this->redis->zRange($allKey, 0, -1) as $entity_id) {
$result[$entity_id] = 1;
}
return [$cacheKey => $result];
}
else {
throw new \CRM_Core_Exception("Unrecognized action: $action");
}
throw new \CRM_Core_Exception("Unrecognized action: $action");
}

public function getPositions($cacheKey, $id1) {
Expand Down Expand Up @@ -173,9 +201,12 @@ public function deleteItem($id = NULL, $cacheKey = NULL) {
}
elseif ($id !== NULL && $cacheKey !== NULL) {
// Delete a specific contact, within a specific cache.
$this->redis->zRem($this->key($cacheKey, 'all'), $id);
$this->redis->zRem($this->key($cacheKey, 'sel'), $id);
$this->redis->hDel($this->key($cacheKey, 'data'), $id);
$deleted = $this->redis->zRem($this->key($cacheKey, 'all'), $id);
if ($deleted) {
// If they were in the 'all' key they might be in the more specific 'sel' and 'data' keys.
$this->redis->zRem($this->key($cacheKey, 'sel'), $id);
$this->redis->hDel($this->key($cacheKey, 'data'), $id);
}
}
elseif ($id !== NULL && $cacheKey === NULL) {
// Delete a specific contact, across all prevnext caches.
Expand Down Expand Up @@ -228,10 +259,6 @@ private function initCacheKey($cacheKey) {
$selKey = $this->key($cacheKey, 'sel');
$dataKey = $this->key($cacheKey, 'data');

$this->redis->expire($allKey, self::TTL);
$this->redis->expire($dataKey, self::TTL);
$this->redis->expire($selKey, self::TTL);

$maxScore = 0;
foreach ($this->redis->zRange($allKey, -1, -1, TRUE) as $lastElem => $lastScore) {
$maxScore = $lastScore;
Expand Down