Merge pull request #66 from vus520/master

部分重构 Redisd 驱动
This commit is contained in:
Chino Chang
2016-05-09 21:05:29 +08:00
5 changed files with 57 additions and 49 deletions

View File

@@ -72,6 +72,7 @@ class Redisd
'persistent' => false,
'length' => 0,
'prefix' => '',
'serialize' => \Redis::SERIALIZER_PHP,
];
/**
@@ -106,12 +107,14 @@ class Redisd
* 一致Hash适合超高可用跨网络读取且从节点较多的情况本业务不考虑该需求
*
* @access public
* @param bool $master true 默认主写
* @param bool $master true 默认主写
* @return Redisd
*/
public function master($master = false)
public function master($master = true)
{
if (isset(self::$redis_rw_handler[$master])) {
return $this->handler = self::$redis_rw_handler[$master];
$this->handler = self::$redis_rw_handler[$master];
return $this;
}
//如果不为主则从配置的host剔除主并随机读从失败以后再随机选择从
@@ -130,17 +133,23 @@ class Redisd
$host = isset($parse['host']) ? $parse['host'] : $host;
$port = isset($parse['host']) ? $parse['port'] : $this->options['port'];
$this->handler->$func($host, $port, $this->options['timeout']);
if (null != $this->options['password']) {
$this->handler->auth($this->options['password']);
}
APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), 'info');
//发生错误则摘掉当前节点
try {
$error = $this->handler->getLastError();
$result = $this->handler->$func($host, $port, $this->options['timeout']);
if($result === false) {
$this->handler->getLastError();
}
if (null != $this->options['password']) {
$this->handler->auth($this->options['password']);
}
$this->handler->setOption(\Redis::OPT_SERIALIZER, $this->options['serialize']);
if(strlen($this->options['prefix'])) {
$this->handler->setOption(\Redis::OPT_PREFIX, $this->options['prefix']);
}
APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), Log::ALERT);
} catch (\RedisException $e) {
//phpredis throws a RedisException object if it can't reach the Redis server.
//That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded.
@@ -186,7 +195,8 @@ class Redisd
throw new Exception($e->getMessage(), $e->getCode());
}
return self::$redis_rw_handler[$master] = $this->handler;
self::$redis_rw_handler[$master] = $this->handler;
return $this;
}
/**
@@ -194,14 +204,15 @@ class Redisd
*
* @access public
* @param string $name 缓存key
* @param bool $master 指定主从节点,可以从主节点获取结果
* @return mixed
*/
public function get($name)
public function get($name, $master = false)
{
$this->master(false);
$this->master($master);
try {
$value = $this->handler->get($this->options['prefix'] . $name);
$value = $this->handler->get($name);
} catch (\RedisException $e) {
unset(self::$redis_rw_handler[0]);
@@ -211,13 +222,7 @@ class Redisd
Log::record($e->getMessage(), Log::ERROR);
}
$jsonData = null;
//如果是对象则进行反转
if (!empty($value) && false !== strpos("[{", $value[0])) {
$jsonData = $value ? json_decode($value, true) : $value;
}
return (null === $jsonData) ? $value : $jsonData;
return isset($value) ? $value : null;
}
/**
@@ -236,21 +241,12 @@ class Redisd
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
/**
* 兼容历史版本
* Redis不支持存储对象存入对象会转换成字符串
* 但在这里对所有数据做json_decode会有性能开销
*/
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if (null === $value) {
return $this->handler->delete($this->options['prefix'] . $name);
}
// $expire < 0 则等于ttl操作列为todo吧
try {
if (null === $value) {
return $this->handler->delete($name);
}
if (is_int($expire) && $expire) {
$result = $this->handler->setex($name, $expire, $value);
} else {
@@ -278,7 +274,7 @@ class Redisd
public function rm($name)
{
$this->master(true);
return $this->handler->delete($this->options['prefix'] . $name);
return $this->handler->delete($name);
}
/**
@@ -298,10 +294,12 @@ class Redisd
* 需要先执行 $redis->master() 连接到 DB
*
* @access public
* @return object
* @param bool $master 指定主从节点,可以从主节点获取结果
* @return \Redis
*/
public function handler()
public function handler($master = true)
{
$this->master($master);
return $this->handler;
}

View File

@@ -45,11 +45,11 @@ class Redis extends SessionHandler
throw new Exception('_NOT_SUPPERT_:redis');
}
$this->handler = new \Redis;
// 建立连接
$func = $this->config['persistent'] ? 'pconnect' : 'connect';
$this->config['timeout'] > 0 ?
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']) :
$this->handler->$func($this->config['host'], $this->config['port']);
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
if ('' != $this->config['password']) {
$this->handler->auth($this->config['password']);
}

View File

@@ -46,19 +46,19 @@ class redisdTest extends cacheTestCase
public function testStoreSpecialValues()
{
$redis = new \think\cache\driver\Redisd(['length' => 3]);
$redis = $this->getCacheInstance();
$redis->master(true);
$redis->handler()->setnx('key', 'value');
$value = $redis->handler()->get('key');
$this->assertEquals('value', $value);
$redis->handler()->hset('hash', 'key', 'value');
$value = $redis->handler()->hget('hash', 'key');
$this->assertEquals('value', $value);
$redis->master(true)->set('key', 'val');
$value = $redis->master(false)->get('key');
$this->assertEquals('val', $value);
$redis->master(true)->hset('hash', 'key', 'value');
$value = $redis->master(false)->hget('hash', 'key');
$redis->handler(true)->hset('hash', 'key', 'value');
$value = $redis->handler(false)->hget('hash', 'key');
$this->assertEquals('value', $value);
}

View File

@@ -160,12 +160,18 @@ class debugTest extends \PHPUnit_Framework_TestCase
*/
public function testDump()
{
if (strstr(PHP_VERSION, 'hhvm')) {
return ;
}
$var = [];
$var["key"] = "val";
$output = Debug::dump($var, false, $label = "label");
$array = explode("array", json_encode($output));
if (IS_WIN) {
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array));
} else if (IS_MAC) {
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
} else {
$this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
}

View File

@@ -122,7 +122,11 @@ class sessionTest extends \PHPUnit_Framework_TestCase
// PHP_SESSION_NONE
// PHP_SESSION_ACTIVE
// session_status()
$this->assertEquals(0, ini_get('session.auto_start'));
if (strstr(PHP_VERSION, 'hhvm')) {
$this->assertEquals('', ini_get('session.auto_start'));
}else{
$this->assertEquals(0, ini_get('session.auto_start'));
}
$this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));