Skip to content

Commit

Permalink
Merge pull request #134 from davidalger/verify-slave-load-from-master
Browse files Browse the repository at this point in the history
Added 'retry_reads_on_master' option for when read slaves are used
  • Loading branch information
colinmollenhour committed Sep 24, 2018
2 parents 91d949e + 326adc3 commit cc941a5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cm/Cache/Backend/Redis.php
Expand Up @@ -111,6 +111,13 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
*/
protected $_luaMaxCStack = 5000;

/**
* If 'retry_reads_on_master' is truthy then reads will be retried against master when slave returns "(nil)" value
*
* @var boolean
*/
protected $_retryReadsOnMaster = false;

/**
* @var stdClass
*/
Expand Down Expand Up @@ -339,6 +346,10 @@ public function __construct($options = array())
$this->_luaMaxCStack = (int) $options['lua_max_c_stack'];
}

if (isset($options['retry_reads_on_master'])) {
$this->_retryReadsOnMaster = (bool) $options['retry_reads_on_master'];
}

if (isset($options['auto_expire_lifetime'])) {
$this->_autoExpireLifetime = (int) $options['auto_expire_lifetime'];
}
Expand Down Expand Up @@ -441,6 +452,11 @@ public function load($id, $doNotTestCacheValidity = false)
{
if ($this->_slave) {
$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);
}
} else {
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
}
Expand Down

0 comments on commit cc941a5

Please sign in to comment.