mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-10 00:22:48 +08:00
62
library/think/cache/driver/Redisd.php
vendored
62
library/think/cache/driver/Redisd.php
vendored
@@ -72,6 +72,7 @@ class Redisd
|
|||||||
'persistent' => false,
|
'persistent' => false,
|
||||||
'length' => 0,
|
'length' => 0,
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
|
'serialize' => \Redis::SERIALIZER_PHP,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,11 +108,13 @@ class Redisd
|
|||||||
*
|
*
|
||||||
* @access public
|
* @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])) {
|
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剔除主,并随机读从,失败以后再随机选择从
|
//如果不为主,则从配置的host剔除主,并随机读从,失败以后再随机选择从
|
||||||
@@ -130,17 +133,23 @@ class Redisd
|
|||||||
$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']);
|
//发生错误则摘掉当前节点
|
||||||
|
try {
|
||||||
|
$result = $this->handler->$func($host, $port, $this->options['timeout']);
|
||||||
|
if($result === false) {
|
||||||
|
$this->handler->getLastError();
|
||||||
|
}
|
||||||
|
|
||||||
if (null != $this->options['password']) {
|
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');
|
$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);
|
||||||
try {
|
|
||||||
$error = $this->handler->getLastError();
|
|
||||||
} catch (\RedisException $e) {
|
} catch (\RedisException $e) {
|
||||||
//phpredis throws a RedisException object if it can't reach the Redis server.
|
//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.
|
//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());
|
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
|
* @access public
|
||||||
* @param string $name 缓存key
|
* @param string $name 缓存key
|
||||||
|
* @param bool $master 指定主从节点,可以从主节点获取结果
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $master = false)
|
||||||
{
|
{
|
||||||
$this->master(false);
|
$this->master($master);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$value = $this->handler->get($this->options['prefix'] . $name);
|
$value = $this->handler->get($name);
|
||||||
} catch (\RedisException $e) {
|
} catch (\RedisException $e) {
|
||||||
unset(self::$redis_rw_handler[0]);
|
unset(self::$redis_rw_handler[0]);
|
||||||
|
|
||||||
@@ -211,13 +222,7 @@ class Redisd
|
|||||||
Log::record($e->getMessage(), Log::ERROR);
|
Log::record($e->getMessage(), Log::ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
$jsonData = null;
|
return isset($value) ? $value : null;
|
||||||
//如果是对象则进行反转
|
|
||||||
if (!empty($value) && false !== strpos("[{", $value[0])) {
|
|
||||||
$jsonData = $value ? json_decode($value, true) : $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (null === $jsonData) ? $value : $jsonData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -236,21 +241,12 @@ class Redisd
|
|||||||
if (is_null($expire)) {
|
if (is_null($expire)) {
|
||||||
$expire = $this->options['expire'];
|
$expire = $this->options['expire'];
|
||||||
}
|
}
|
||||||
$name = $this->options['prefix'] . $name;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 兼容历史版本
|
|
||||||
* Redis不支持存储对象,存入对象会转换成字符串
|
|
||||||
* 但在这里,对所有数据做json_decode会有性能开销
|
|
||||||
*/
|
|
||||||
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
|
|
||||||
|
|
||||||
|
try {
|
||||||
if (null === $value) {
|
if (null === $value) {
|
||||||
return $this->handler->delete($this->options['prefix'] . $name);
|
return $this->handler->delete($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// $expire < 0 则等于ttl操作,列为todo吧
|
|
||||||
try {
|
|
||||||
if (is_int($expire) && $expire) {
|
if (is_int($expire) && $expire) {
|
||||||
$result = $this->handler->setex($name, $expire, $value);
|
$result = $this->handler->setex($name, $expire, $value);
|
||||||
} else {
|
} else {
|
||||||
@@ -278,7 +274,7 @@ class Redisd
|
|||||||
public function rm($name)
|
public function rm($name)
|
||||||
{
|
{
|
||||||
$this->master(true);
|
$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
|
* 需要先执行 $redis->master() 连接到 DB
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @return object
|
* @param bool $master 指定主从节点,可以从主节点获取结果
|
||||||
|
* @return \Redis
|
||||||
*/
|
*/
|
||||||
public function handler()
|
public function handler($master = true)
|
||||||
{
|
{
|
||||||
|
$this->master($master);
|
||||||
return $this->handler;
|
return $this->handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ class Redis extends SessionHandler
|
|||||||
throw new Exception('_NOT_SUPPERT_:redis');
|
throw new Exception('_NOT_SUPPERT_:redis');
|
||||||
}
|
}
|
||||||
$this->handler = new \Redis;
|
$this->handler = new \Redis;
|
||||||
|
|
||||||
// 建立连接
|
// 建立连接
|
||||||
$func = $this->config['persistent'] ? 'pconnect' : 'connect';
|
$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->config['timeout']) :
|
|
||||||
$this->handler->$func($this->config['host'], $this->config['port']);
|
|
||||||
if ('' != $this->config['password']) {
|
if ('' != $this->config['password']) {
|
||||||
$this->handler->auth($this->config['password']);
|
$this->handler->auth($this->config['password']);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,19 +46,19 @@ class redisdTest extends cacheTestCase
|
|||||||
|
|
||||||
public function testStoreSpecialValues()
|
public function testStoreSpecialValues()
|
||||||
{
|
{
|
||||||
$redis = new \think\cache\driver\Redisd(['length' => 3]);
|
$redis = $this->getCacheInstance();
|
||||||
$redis->master(true);
|
$redis->master(true);
|
||||||
|
|
||||||
$redis->handler()->setnx('key', 'value');
|
$redis->handler()->setnx('key', 'value');
|
||||||
$value = $redis->handler()->get('key');
|
$value = $redis->handler()->get('key');
|
||||||
$this->assertEquals('value', $value);
|
$this->assertEquals('value', $value);
|
||||||
|
|
||||||
$redis->handler()->hset('hash', 'key', 'value');
|
$redis->master(true)->set('key', 'val');
|
||||||
$value = $redis->handler()->hget('hash', 'key');
|
$value = $redis->master(false)->get('key');
|
||||||
$this->assertEquals('value', $value);
|
$this->assertEquals('val', $value);
|
||||||
|
|
||||||
$redis->master(true)->hset('hash', 'key', 'value');
|
$redis->handler(true)->hset('hash', 'key', 'value');
|
||||||
$value = $redis->master(false)->hget('hash', 'key');
|
$value = $redis->handler(false)->hget('hash', 'key');
|
||||||
$this->assertEquals('value', $value);
|
$this->assertEquals('value', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -160,12 +160,18 @@ class debugTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testDump()
|
public function testDump()
|
||||||
{
|
{
|
||||||
|
if (strstr(PHP_VERSION, 'hhvm')) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
$var = [];
|
$var = [];
|
||||||
$var["key"] = "val";
|
$var["key"] = "val";
|
||||||
$output = Debug::dump($var, false, $label = "label");
|
$output = Debug::dump($var, false, $label = "label");
|
||||||
$array = explode("array", json_encode($output));
|
$array = explode("array", json_encode($output));
|
||||||
if (IS_WIN) {
|
if (IS_WIN) {
|
||||||
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array));
|
$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 {
|
} else {
|
||||||
$this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
|
$this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,7 +122,11 @@ class sessionTest extends \PHPUnit_Framework_TestCase
|
|||||||
// PHP_SESSION_NONE
|
// PHP_SESSION_NONE
|
||||||
// PHP_SESSION_ACTIVE
|
// PHP_SESSION_ACTIVE
|
||||||
// session_status()
|
// session_status()
|
||||||
|
if (strstr(PHP_VERSION, 'hhvm')) {
|
||||||
|
$this->assertEquals('', ini_get('session.auto_start'));
|
||||||
|
}else{
|
||||||
$this->assertEquals(0, ini_get('session.auto_start'));
|
$this->assertEquals(0, ini_get('session.auto_start'));
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
|
$this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user