mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
Merge branch 'master' of https://github.com/top-think/framework
This commit is contained in:
10
library/think/cache/driver/Redis.php
vendored
10
library/think/cache/driver/Redis.php
vendored
@@ -115,4 +115,14 @@ class Redis
|
|||||||
return $this->handler->flushDB();
|
return $this->handler->flushDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回句柄对象,可执行其它高级方法
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function handler()
|
||||||
|
{
|
||||||
|
return $this->handler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
66
library/think/cache/driver/Redisd.php
vendored
66
library/think/cache/driver/Redisd.php
vendored
@@ -18,21 +18,21 @@ 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, //默认过期时间,默认为永不过期
|
||||||
'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)->setnx('key');
|
||||||
$redis->get('key');
|
$redis->master(false)->get('key');
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,6 +60,7 @@ class Redisd
|
|||||||
{
|
{
|
||||||
protected static $redis_rw_handler;
|
protected static $redis_rw_handler;
|
||||||
protected static $redis_err_pool;
|
protected static $redis_err_pool;
|
||||||
|
protected $handler = null;
|
||||||
|
|
||||||
protected $options = [
|
protected $options = [
|
||||||
'host' => '127.0.0.1',
|
'host' => '127.0.0.1',
|
||||||
@@ -146,12 +147,12 @@ class Redisd
|
|||||||
//In any other problematic case that does not involve an unreachable server
|
//In any other problematic case that does not involve an unreachable server
|
||||||
//(such as a key not existing, an invalid command, etc), phpredis will return FALSE.
|
//(such as a key not existing, an invalid command, etc), phpredis will return FALSE.
|
||||||
|
|
||||||
Log::record(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $e->getMessage()), Log::ALERT);
|
Log::record(sprintf("redisd->%s:%s:%s:%s", $master ? "master" : "salve", $host, $port, $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}:{$port} : " . $e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,16 +170,15 @@ class Redisd
|
|||||||
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}:{$port} : " . $e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
Log::record("salve {$host} is down, try another one.", Log::ALERT);
|
Log::record("salve {$host}:{$port} is down, try another one.", Log::ALERT);
|
||||||
return $this->master(false);
|
return $this->master(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,7 +186,7 @@ class Redisd
|
|||||||
throw new Exception($e->getMessage(), $e->getCode());
|
throw new Exception($e->getMessage(), $e->getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$redis_rw_handler[$master] = $this->handler;
|
return self::$redis_rw_handler[$master] = $this->handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,8 +204,9 @@ class Redisd
|
|||||||
$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();
|
||||||
$this->get($name);
|
return $this->get($name);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::record($e->getMessage(), Log::ERROR);
|
Log::record($e->getMessage(), Log::ERROR);
|
||||||
}
|
}
|
||||||
@@ -257,8 +258,9 @@ class Redisd
|
|||||||
}
|
}
|
||||||
} catch (\RedisException $e) {
|
} catch (\RedisException $e) {
|
||||||
unset(self::$redis_rw_handler[1]);
|
unset(self::$redis_rw_handler[1]);
|
||||||
|
|
||||||
$this->master(true);
|
$this->master(true);
|
||||||
$this->set($name, $value, $expire);
|
return $this->set($name, $value, $expire);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::record($e->getMessage());
|
Log::record($e->getMessage());
|
||||||
}
|
}
|
||||||
@@ -266,18 +268,6 @@ class Redisd
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回句柄对象
|
|
||||||
* 需要先执行 $redis->master() 连接到 DB
|
|
||||||
*
|
|
||||||
* @access public
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
public function handler()
|
|
||||||
{
|
|
||||||
return $this->handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除缓存
|
* 删除缓存
|
||||||
*
|
*
|
||||||
@@ -302,6 +292,18 @@ class Redisd
|
|||||||
$this->master(true);
|
$this->master(true);
|
||||||
return $this->handler->flushDB();
|
return $this->handler->flushDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回句柄对象,可执行其它高级方法
|
||||||
|
* 需要先执行 $redis->master() 连接到 DB
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function handler()
|
||||||
|
{
|
||||||
|
return $this->handler;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 析构释放连接
|
* 析构释放连接
|
||||||
|
|||||||
@@ -41,10 +41,23 @@ class redisTest extends cacheTestCase
|
|||||||
$cache = $this->prepare();
|
$cache = $this->prepare();
|
||||||
$this->assertEquals('string_test', $cache->get('string_test'));
|
$this->assertEquals('string_test', $cache->get('string_test'));
|
||||||
$this->assertEquals(11, $cache->get('number_test'));
|
$this->assertEquals(11, $cache->get('number_test'));
|
||||||
|
$result = $cache->get('array_test');
|
||||||
|
$this->assertEquals('array_test', $result['array_test']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStoreSpecialValues()
|
public function testStoreSpecialValues()
|
||||||
{
|
{
|
||||||
|
$redis = new \think\cache\driver\Redis(['length' => 3]);
|
||||||
|
$redis->set('key', 'value');
|
||||||
|
$redis->get('key');
|
||||||
|
|
||||||
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExpire()
|
public function testExpire()
|
||||||
|
|||||||
@@ -9,13 +9,12 @@
|
|||||||
// | Author: liu21st <liu21st@gmail.com>
|
// | Author: liu21st <liu21st@gmail.com>
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace tests\thinkphp\library\think\cache\driver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redisd缓存驱动测试
|
* Redisd缓存驱动测试
|
||||||
* @author 尘缘 <130775@qq.com>
|
* @author 尘缘 <130775@qq.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace tests\thinkphp\library\think\cache\driver;
|
|
||||||
|
|
||||||
class redisdTest extends cacheTestCase
|
class redisdTest extends cacheTestCase
|
||||||
{
|
{
|
||||||
private $_cacheInstance = null;
|
private $_cacheInstance = null;
|
||||||
@@ -31,7 +30,7 @@ class redisdTest extends cacheTestCase
|
|||||||
protected function getCacheInstance()
|
protected function getCacheInstance()
|
||||||
{
|
{
|
||||||
if (null === $this->_cacheInstance) {
|
if (null === $this->_cacheInstance) {
|
||||||
$this->_cacheInstance = new \think\cache\driver\Redisd(['length' => 3]);
|
$this->_cacheInstance = new \think\cache\driver\Redisd();
|
||||||
}
|
}
|
||||||
return $this->_cacheInstance;
|
return $this->_cacheInstance;
|
||||||
}
|
}
|
||||||
@@ -47,6 +46,20 @@ class redisdTest extends cacheTestCase
|
|||||||
|
|
||||||
public function testStoreSpecialValues()
|
public function testStoreSpecialValues()
|
||||||
{
|
{
|
||||||
|
$redis = new \think\cache\driver\Redisd(['length' => 3]);
|
||||||
|
$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)->hset('hash', 'key', 'value');
|
||||||
|
$value = $redis->master(false)->hget('hash', 'key');
|
||||||
|
$this->assertEquals('value', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExpire()
|
public function testExpire()
|
||||||
|
|||||||
Reference in New Issue
Block a user