Skip to content

Commit

Permalink
Handle LOADING exception during read. (#171)
Browse files Browse the repository at this point in the history
Handle LOADING state when reading from slave by reading from master and when reading from master by retrying after 1 second wait.
  • Loading branch information
colinmollenhour authored Dec 29, 2022
1 parent 6661e3f commit 841d59e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Cm/Cache/Backend/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,35 @@ protected function _setupReadWriteCluster($options)
public function load($id, $doNotTestCacheValidity = false)
{
if ($this->_slave) {
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
try {
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);

// Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
if ($this->_retryReadsOnMaster && $data === false) {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
// Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
if ($this->_retryReadsOnMaster && $data === false) {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
}
} catch (CredisException $e) {
// Always retry reads on master when dataset is loading on slave
if ($e->getMessage() === 'LOADING Redis is loading the dataset in memory') {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
} else {
throw $e;
}
}
} else {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
try {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
} catch (CredisException $e) {
// Retry once after 1 second when dataset is loading
if ($e->getMessage() === 'LOADING Redis is loading the dataset in memory') {
sleep(1);
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
} else {
throw $e;
}
}
}
if ($data === NULL || is_object($data)) {
if ($data === NULL || $data === FALSE || is_object($data)) {
return FALSE;
}

Expand Down

0 comments on commit 841d59e

Please sign in to comment.