Skip to content

Commit 326adc3

Browse files
committed
Added 'retry_reads_on_master' option to support optional read replay on master when slave returns empty result
1 parent 91d949e commit 326adc3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Cm/Cache/Backend/Redis.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
111111
*/
112112
protected $_luaMaxCStack = 5000;
113113

114+
/**
115+
* If 'retry_reads_on_master' is truthy then reads will be retried against master when slave returns "(nil)" value
116+
*
117+
* @var boolean
118+
*/
119+
protected $_retryReadsOnMaster = false;
120+
114121
/**
115122
* @var stdClass
116123
*/
@@ -339,6 +346,10 @@ public function __construct($options = array())
339346
$this->_luaMaxCStack = (int) $options['lua_max_c_stack'];
340347
}
341348

349+
if (isset($options['retry_reads_on_master'])) {
350+
$this->_retryReadsOnMaster = (bool) $options['retry_reads_on_master'];
351+
}
352+
342353
if (isset($options['auto_expire_lifetime'])) {
343354
$this->_autoExpireLifetime = (int) $options['auto_expire_lifetime'];
344355
}
@@ -441,6 +452,11 @@ public function load($id, $doNotTestCacheValidity = false)
441452
{
442453
if ($this->_slave) {
443454
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
455+
456+
// Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
457+
if ($this->_retryReadsOnMaster && $data === false) {
458+
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
459+
}
444460
} else {
445461
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
446462
}

0 commit comments

Comments
 (0)