mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
13
library/think/cache/driver/Redis.php
vendored
13
library/think/cache/driver/Redis.php
vendored
@@ -15,7 +15,9 @@ use think\Cache;
|
|||||||
use think\Exception;
|
use think\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis缓存驱动
|
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
|
||||||
|
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
|
||||||
|
*
|
||||||
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
|
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
|
||||||
* @author 尘缘 <130775@qq.com>
|
* @author 尘缘 <130775@qq.com>
|
||||||
*/
|
*/
|
||||||
@@ -26,8 +28,8 @@ class Redis
|
|||||||
'host' => '127.0.0.1',
|
'host' => '127.0.0.1',
|
||||||
'port' => 6379,
|
'port' => 6379,
|
||||||
'password' => '',
|
'password' => '',
|
||||||
'timeout' => false,
|
'timeout' => 0,
|
||||||
'expire' => false,
|
'expire' => 0,
|
||||||
'persistent' => false,
|
'persistent' => false,
|
||||||
'length' => 0,
|
'length' => 0,
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
@@ -48,9 +50,8 @@ class Redis
|
|||||||
}
|
}
|
||||||
$func = $this->options['persistent'] ? 'pconnect' : 'connect';
|
$func = $this->options['persistent'] ? 'pconnect' : 'connect';
|
||||||
$this->handler = new \Redis;
|
$this->handler = new \Redis;
|
||||||
false === $this->options['timeout'] ?
|
|
||||||
$this->handler->$func($this->options['host'], $this->options['port']) :
|
|
||||||
$this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
|
$this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
|
||||||
|
|
||||||
if ('' != $this->options['password']) {
|
if ('' != $this->options['password']) {
|
||||||
$this->handler->auth($this->options['password']);
|
$this->handler->auth($this->options['password']);
|
||||||
}
|
}
|
||||||
@@ -86,7 +87,7 @@ class Redis
|
|||||||
$name = $this->options['prefix'] . $name;
|
$name = $this->options['prefix'] . $name;
|
||||||
//对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
|
//对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
|
||||||
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
|
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
|
||||||
if (is_int($expire)) {
|
if (is_int($expire) && $expire) {
|
||||||
$result = $this->handler->setex($name, $expire, $value);
|
$result = $this->handler->setex($name, $expire, $value);
|
||||||
} else {
|
} else {
|
||||||
$result = $this->handler->set($name, $value);
|
$result = $this->handler->set($name, $value);
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ abstract class cacheTestCase extends \PHPUnit_Framework_TestCase
|
|||||||
$array = $cache->get('array_test');
|
$array = $cache->get('array_test');
|
||||||
$this->assertArrayHasKey('array_test', $array);
|
$this->assertArrayHasKey('array_test', $array);
|
||||||
$this->assertEquals('array_test', $array['array_test']);
|
$this->assertEquals('array_test', $array['array_test']);
|
||||||
|
|
||||||
|
$result = $cache->set('no_expire', 1, 0);
|
||||||
|
$this->assertTrue($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Author: liu21st <liu21st@gmail.com>
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库缓存驱动测试
|
|
||||||
* @author mahuan <mahuan@d1web.top>
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace tests\thinkphp\library\think\cache\driver;
|
|
||||||
|
|
||||||
class dbTest extends cacheTestCase
|
|
||||||
{
|
|
||||||
private $_cacheInstance = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 基境缓存类型
|
|
||||||
*/
|
|
||||||
protected function setUp()
|
|
||||||
{
|
|
||||||
//数据库缓存测试因为缺少数据库单元测试所以暂时跳过
|
|
||||||
$this->markTestSkipped("暂时跳过测试。");
|
|
||||||
\think\Cache::connect(array('type' => 'db', 'expire' => 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return DbCache
|
|
||||||
*/
|
|
||||||
protected function getCacheInstance()
|
|
||||||
{
|
|
||||||
if (null === $this->_cacheInstance) {
|
|
||||||
$this->_cacheInstance = new \think\cache\driver\Db();
|
|
||||||
}
|
|
||||||
return $this->_cacheInstance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user