Skip to content

Commit 60c824e

Browse files
committed
failover
1 parent 6154f2d commit 60c824e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ Cache::config('redis', [
3232
'server' => ['10.10.10.10:6379', '10.10.10.20:6379', '10.10.10.30:6379']
3333
]);
3434
```
35+
36+
## Slave failover / distribution
37+
38+
Slave failover / distibution can be configured by `failover` configuration key. See [related chapter](https://github.com/phpredis/phpredis/blob/master/cluster.markdown#automatic-slave-failover--distribution) in phpredis cluster readme.
39+
40+
Possible values are:
41+
42+
- *none* (default)
43+
- *error*
44+
- *distribute*
45+
- *slaves*

src/Cache/Engine/RedisClusterEngine.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class RedisClusterEngine extends RedisEngine
4141
'probability' => 100,
4242
'server' => [],
4343
'timeout' => 2,
44-
'read_timeout' => 2
44+
'read_timeout' => 2,
45+
'failover' => 'none'
4546
];
4647

4748
/**
@@ -65,6 +66,23 @@ protected function _connect()
6566
{
6667
try {
6768
$this->_Redis = new \RedisCluster($this->_config['name'], $this->_config['server'], $this->_config['timeout'], $this->_config['read_timeout'], $this->_config['persistent']);
69+
70+
switch ($this->_config['failover']) {
71+
case 'error':
72+
$this->_Redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_ERROR);
73+
break;
74+
75+
case 'distribute':
76+
$this->_Redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE);
77+
break;
78+
79+
case 'slaves':
80+
$this->_Redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE_SLAVES);
81+
break;
82+
83+
default:
84+
$this->_Redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_NONE);
85+
}
6886
} catch (\RedisClusterException $e) {
6987
return false;
7088
}

0 commit comments

Comments
 (0)