改进redisd驱动

This commit is contained in:
thinkphp
2016-05-05 16:40:02 +08:00
parent c22b26026e
commit a134da6563

View File

@@ -16,23 +16,23 @@ use think\Exception;
use think\Log; use think\Log;
/** /**
配置参数: 配置参数:
'cache' => [ 'cache' => [
'type' => 'Redisd' 'type' => 'Redisd'
'host' => 'A:6379,B:6379', //redis服务器ip多台用逗号隔开读写分离开启时默认写A当A主挂时再尝试写B 'host' => 'A:6379,B:6379', //redis服务器ip多台用逗号隔开读写分离开启时默认写A当A主挂时再尝试写B
'slave' => 'B:6379,C:6379', //redis服务器ip多台用逗号隔开读写分离开启时所有IP随机读其中一台挂时尝试读其它节点可以配置权重 'slave' => 'B:6379,C:6379', //redis服务器ip多台用逗号隔开读写分离开启时所有IP随机读其中一台挂时尝试读其它节点可以配置权重
'port' => 6379, //默认的端口号 'port' => 6379, //默认的端口号
'password' => '', //AUTH认证密码当redis服务直接暴露在外网时推荐 'password' => '', //AUTH认证密码当redis服务直接暴露在外网时推荐
'timeout' => 10, //连接超时时间 'timeout' => 10, //连接超时时间
'expire' => false, //默认过期时间默认0为永不过期 'expire' => false, //默认过期时间默认0为永不过期
'prefix' => '', //缓存前缀,不宜过长 'prefix' => '', //缓存前缀,不宜过长
'persistent' => false, //是否长连接 false=短连接,推荐长连接 'persistent' => false, //是否长连接 false=短连接,推荐长连接
], ],
单例获取: 单例获取:
$redis = \think\Cache::connect(Config::get('cache')); $redis = \think\Cache::connect(Config::get('cache'));
$redis->master(true); $redis->master(true);
$redis->get('key'); $redis->get('key');
*/ */
/** /**
@@ -85,18 +85,18 @@ class Redisd
throw new Exception('_NOT_SUPPERT_:redis'); throw new Exception('_NOT_SUPPERT_:redis');
} }
$this->options = $options = array_merge($this->options, $options); $this->options = $options = array_merge($this->options, $options);
$this->options ['func'] = $options ['persistent'] ? 'pconnect' : 'connect'; $this->options['func'] = $options['persistent'] ? 'pconnect' : 'connect';
$host = explode(",", trim($this->options ['host'], ",")); $host = explode(",", trim($this->options['host'], ","));
$host = array_map("trim", $host); $host = array_map("trim", $host);
$slave = explode(",", trim($this->options ['slave'], ",")); $slave = explode(",", trim($this->options['slave'], ","));
$slave = array_map("trim", $slave); $slave = array_map("trim", $slave);
$this->options ["server_slave"] = empty($slave) ? $host : $slave; $this->options["server_slave"] = empty($slave) ? $host : $slave;
$this->options ["servers"] = count($slave); $this->options["servers"] = count($slave);
$this->options ["server_master"] = array_shift($host); $this->options["server_master"] = array_shift($host);
$this->options ["server_master_failover"] = $host; $this->options["server_master_failover"] = $host;
} }
/** /**
@@ -115,24 +115,24 @@ class Redisd
//如果不为主则从配置的host剔除主并随机读从失败以后再随机选择从 //如果不为主则从配置的host剔除主并随机读从失败以后再随机选择从
//另外一种方案是根据key的一致性hash选择不同的node但读写频繁的业务中可能打开大量的文件句柄 //另外一种方案是根据key的一致性hash选择不同的node但读写频繁的业务中可能打开大量的文件句柄
if(!$master && $this->options["servers"] > 1) { if (!$master && $this->options["servers"] > 1) {
shuffle($this->options["server_slave"]); shuffle($this->options["server_slave"]);
$host = array_shift($this->options["server_slave"]); $host = array_shift($this->options["server_slave"]);
}else{ } else {
$host = $this->options["server_master"]; $host = $this->options["server_master"];
} }
$this->handler = new \Redis(); $this->handler = new \Redis();
$func = $this->options ['func']; $func = $this->options['func'];
$parse = parse_url($host); $parse = parse_url($host);
$host = isset($parse['host']) ? $parse['host'] : $host; $host = isset($parse['host']) ? $parse['host'] : $host;
$port = isset($parse['host']) ? $parse['port'] : $this->options ['port']; $port = isset($parse['host']) ? $parse['port'] : $this->options['port'];
$this->handler->$func($host, $port, $this->options ['timeout']); $this->handler->$func($host, $port, $this->options['timeout']);
if ($this->options ['password'] != null) { if (null != $this->options['password']) {
$this->handler->auth($this->options ['password']); $this->handler->auth($this->options['password']);
} }
APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), 'info'); APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), 'info');
@@ -149,9 +149,9 @@ class Redisd
Log::record(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $e->getMessage()), Log::ALERT); Log::record(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $e->getMessage()), Log::ALERT);
//主节点挂了以后,尝试连接主备,断开主备的主从连接进行升主 //主节点挂了以后,尝试连接主备,断开主备的主从连接进行升主
if($master) { if ($master) {
if(! count($this->options["server_master_failover"])) { if (!count($this->options["server_master_failover"])) {
throw new Exception("redisd master: no more server_master_failover. {$host} : ".$e->getMessage()); throw new Exception("redisd master: no more server_master_failover. {$host} : " . $e->getMessage());
return false; return false;
} }
@@ -165,23 +165,24 @@ class Redisd
//$this->handler->slaveof(); //$this->handler->slaveof();
} else { } else {
//尝试failover如果有其它节点则进行其它节点的尝试 //尝试failover如果有其它节点则进行其它节点的尝试
foreach ($this->options["server_slave"] as $k=>$v) foreach ($this->options["server_slave"] as $k => $v) {
{ if (trim($v) == trim($host)) {
if (trim($v) == trim($host))
unset($this->options["server_slave"][$k]); unset($this->options["server_slave"][$k]);
}
} }
//如果无可用节点,则抛出异常 //如果无可用节点,则抛出异常
if(! count($this->options["server_slave"])) { if (!count($this->options["server_slave"])) {
Log::record("已无可用Redis读节点", Log::ERROR); Log::record("已无可用Redis读节点", Log::ERROR);
throw new Exception("redisd slave: no more server_slave. {$host} : ".$e->getMessage()); throw new Exception("redisd slave: no more server_slave. {$host} : " . $e->getMessage());
return false; return false;
} else { } else {
Log::record("salve {$host} is down, try another one.", Log::ALERT); Log::record("salve {$host} is down, try another one.", Log::ALERT);
return $this->master(false); return $this->master(false);
} }
} }
} catch(\Exception $e) { } catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode()); throw new Exception($e->getMessage(), $e->getCode());
} }
@@ -197,12 +198,10 @@ class Redisd
*/ */
public function get($name) public function get($name)
{ {
Cache::$readTimes++;
$this->master(false); $this->master(false);
try { try {
$value = $this->handler->get($this->options ['prefix'] . $name); $value = $this->handler->get($this->options['prefix'] . $name);
} catch (\RedisException $e) { } catch (\RedisException $e) {
unset(self::$redis_rw_handler[0]); unset(self::$redis_rw_handler[0]);
$this->master(); $this->master();
@@ -217,7 +216,7 @@ class Redisd
$jsonData = $value ? json_decode($value, true) : $value; $jsonData = $value ? json_decode($value, true) : $value;
} }
return ($jsonData === NULL) ? $value : $jsonData; return (null === $jsonData) ? $value : $jsonData;
} }
/** /**
@@ -231,24 +230,22 @@ class Redisd
*/ */
public function set($name, $value, $expire = null) public function set($name, $value, $expire = null)
{ {
Cache::$writeTimes++;
$this->master(true); $this->master(true);
if (is_null($expire )) { if (is_null($expire)) {
$expire = $this->options ['expire']; $expire = $this->options['expire'];
} }
$name = $this->options ['prefix'] . $name; $name = $this->options['prefix'] . $name;
/** /**
* 兼容历史版本 * 兼容历史版本
* Redis不支持存储对象存入对象会转换成字符串 * Redis不支持存储对象存入对象会转换成字符串
* 但在这里对所有数据做json_decode会有性能开销 * 但在这里对所有数据做json_decode会有性能开销
*/ */
$value = (is_object($value) || is_array($value )) ? json_encode($value) : $value; $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if ($value === null) { if (null === $value) {
return $this->handler->delete($this->options ['prefix'] . $name); return $this->handler->delete($this->options['prefix'] . $name);
} }
// $expire < 0 则等于ttl操作列为todo吧 // $expire < 0 则等于ttl操作列为todo吧
@@ -276,7 +273,7 @@ class Redisd
* @access public * @access public
* @return object * @return object
*/ */
function handler() public function handler()
{ {
return $this->handler; return $this->handler;
} }
@@ -290,9 +287,8 @@ class Redisd
*/ */
public function rm($name) public function rm($name)
{ {
Cache::$writeTimes++;
$this->master(true); $this->master(true);
return $this->handler->delete($this->options ['prefix'] . $name); return $this->handler->delete($this->options['prefix'] . $name);
} }
/** /**
@@ -303,10 +299,8 @@ class Redisd
*/ */
public function clear() public function clear()
{ {
Cache::$writeTimes++;
$this->master(true); $this->master(true);
return $this->handler->flushDB (); return $this->handler->flushDB();
} }
/** /**
@@ -321,8 +315,10 @@ class Redisd
//如果代码中使用pconnect close的作用仅是使当前php不能再进行redis请求但无法真正关闭redis长连接连接在后续请求中仍然会被重用直至fpm进程生命周期结束。 //如果代码中使用pconnect close的作用仅是使当前php不能再进行redis请求但无法真正关闭redis长连接连接在后续请求中仍然会被重用直至fpm进程生命周期结束。
try { try {
if(method_exists($this->handler, "close")) if (method_exists($this->handler, "close")) {
$this->handler->close (); $this->handler->close();
}
} catch (\Exception $e) { } catch (\Exception $e) {
} }
} }