diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 6ae0f1da..e31d38f7 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -115,4 +115,14 @@ class Redis return $this->handler->flushDB(); } + /** + * 返回句柄对象,可执行其它高级方法 + * + * @access public + * @return object + */ + public function handler() + { + return $this->handler; + } } diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index a11fe810..f4ac7281 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -18,21 +18,21 @@ use think\Log; /** 配置参数: 'cache' => [ -'type' => 'Redisd' -'host' => 'A:6379,B:6379', //redis服务器ip,多台用逗号隔开;读写分离开启时,默认写A,当A主挂时,再尝试写B -'slave' => 'B:6379,C:6379', //redis服务器ip,多台用逗号隔开;读写分离开启时,所有IP随机读,其中一台挂时,尝试读其它节点,可以配置权重 -'port' => 6379, //默认的端口号 -'password' => '', //AUTH认证密码,当redis服务直接暴露在外网时推荐 -'timeout' => 10, //连接超时时间 -'expire' => false, //默认过期时间,默认0为永不过期 -'prefix' => '', //缓存前缀,不宜过长 -'persistent' => false, //是否长连接 false=短连接,推荐长连接 + 'type' => 'Redisd' + 'host' => 'A:6379,B:6379', //redis服务器ip,多台用逗号隔开;读写分离开启时,默认写A,当A主挂时,再尝试写B + 'slave' => 'B:6379,C:6379', //redis服务器ip,多台用逗号隔开;读写分离开启时,所有IP随机读,其中一台挂时,尝试读其它节点,可以配置权重 + 'port' => 6379, //默认的端口号 + 'password' => '', //AUTH认证密码,当redis服务直接暴露在外网时推荐 + 'timeout' => 10, //连接超时时间 + 'expire' => false, //默认过期时间,默认为永不过期 + 'prefix' => '', //缓存前缀,不宜过长 + 'persistent' => false, //是否长连接 false=短连接,推荐长连接 ], 单例获取: -$redis = \think\Cache::connect(Config::get('cache')); -$redis->master(true); -$redis->get('key'); + $redis = \think\Cache::connect(Config::get('cache')); + $redis->master(true)->setnx('key'); + $redis->master(false)->get('key'); */ /** @@ -60,6 +60,7 @@ class Redisd { protected static $redis_rw_handler; protected static $redis_err_pool; + protected $handler = null; protected $options = [ 'host' => '127.0.0.1', @@ -146,12 +147,12 @@ class Redisd //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. - 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 (!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; } @@ -169,16 +170,15 @@ class Redisd if (trim($v) == trim($host)) { unset($this->options["server_slave"][$k]); } - } //如果无可用节点,则抛出异常 if (!count($this->options["server_slave"])) { 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; } 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); } } @@ -186,7 +186,7 @@ class Redisd 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); } catch (\RedisException $e) { unset(self::$redis_rw_handler[0]); + $this->master(); - $this->get($name); + return $this->get($name); } catch (\Exception $e) { Log::record($e->getMessage(), Log::ERROR); } @@ -257,8 +258,9 @@ class Redisd } } catch (\RedisException $e) { unset(self::$redis_rw_handler[1]); + $this->master(true); - $this->set($name, $value, $expire); + return $this->set($name, $value, $expire); } catch (\Exception $e) { Log::record($e->getMessage()); } @@ -266,18 +268,6 @@ class Redisd return $result; } - /** - * 返回句柄对象 - * 需要先执行 $redis->master() 连接到 DB - * - * @access public - * @return object - */ - public function handler() - { - return $this->handler; - } - /** * 删除缓存 * @@ -302,6 +292,18 @@ class Redisd $this->master(true); return $this->handler->flushDB(); } + + /** + * 返回句柄对象,可执行其它高级方法 + * 需要先执行 $redis->master() 连接到 DB + * + * @access public + * @return object + */ + public function handler() + { + return $this->handler; + } /** * 析构释放连接 diff --git a/tests/thinkphp/library/think/cache/driver/redisTest.php b/tests/thinkphp/library/think/cache/driver/redisTest.php index 577cbe2a..c55ab828 100644 --- a/tests/thinkphp/library/think/cache/driver/redisTest.php +++ b/tests/thinkphp/library/think/cache/driver/redisTest.php @@ -41,10 +41,23 @@ class redisTest extends cacheTestCase $cache = $this->prepare(); $this->assertEquals('string_test', $cache->get('string_test')); $this->assertEquals(11, $cache->get('number_test')); + $result = $cache->get('array_test'); + $this->assertEquals('array_test', $result['array_test']); } 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() diff --git a/tests/thinkphp/library/think/cache/driver/redisdTest.php b/tests/thinkphp/library/think/cache/driver/redisdTest.php index 51692065..96541299 100644 --- a/tests/thinkphp/library/think/cache/driver/redisdTest.php +++ b/tests/thinkphp/library/think/cache/driver/redisdTest.php @@ -9,13 +9,12 @@ // | Author: liu21st // +---------------------------------------------------------------------- +namespace tests\thinkphp\library\think\cache\driver; + /** * Redisd缓存驱动测试 * @author 尘缘 <130775@qq.com> */ - -namespace tests\thinkphp\library\think\cache\driver; - class redisdTest extends cacheTestCase { private $_cacheInstance = null; @@ -31,7 +30,7 @@ class redisdTest extends cacheTestCase protected function getCacheInstance() { if (null === $this->_cacheInstance) { - $this->_cacheInstance = new \think\cache\driver\Redisd(['length' => 3]); + $this->_cacheInstance = new \think\cache\driver\Redisd(); } return $this->_cacheInstance; } @@ -47,6 +46,20 @@ class redisdTest extends cacheTestCase 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()