This commit is contained in:
尘缘
2016-05-05 16:51:00 +08:00
34 changed files with 242 additions and 721 deletions

View File

@@ -11,15 +11,6 @@
namespace think;
/**
* Class Cache
*
* @package think
* @method static mixed get() get(string $name)
* @method static bool set() set(string $name, mixed $value, mixed $expire = null)
* @method static bool rm() rm(string $name, bool $expire = false)
* @method static bool clear() clear()
*/
class Cache
{
protected static $instance = [];
@@ -37,29 +28,93 @@ class Cache
* 连接缓存
* @access public
* @param array $options 配置数组
* @param bool|string $name 缓存连接标识 true 强制重新连接
* @return object
*/
public static function connect(array $options = [])
public static function connect(array $options = [], $name = false)
{
$md5 = md5(serialize($options));
if (!isset(self::$instance[$md5])) {
$type = !empty($options['type']) ? $options['type'] : 'File';
$type = !empty($options['type']) ? $options['type'] : 'File';
if (false === $name) {
$name = $type;
}
if (true === $name || !isset(self::$instance[$name])) {
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type);
unset($options['type']);
self::$instance[$md5] = new $class($options);
// 记录初始化信息
APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info');
if (true === $name) {
return new $class($options);
} else {
self::$instance[$name] = new $class($options);
}
}
self::$handler = self::$instance[$md5];
self::$handler = self::$instance[$name];
return self::$handler;
}
public static function __callStatic($method, $params)
/**
* 自动初始化缓存
* @access public
* @return void
*/
public static function init()
{
if (is_null(self::$handler)) {
// 自动初始化缓存
self::connect(Config::get('cache'));
}
return call_user_func_array([self::$handler, $method], $params);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存标识
* @return mixed
*/
public static function get($name)
{
self::init();
self::$readTimes++;
return self::$handler->get($name);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存标识
* @param mixed $value 存储数据
* @param int|null $expire 有效时间 0为永久
* @return boolean
*/
public static function set($name, $value, $expire = null)
{
self::init();
self::$writeTimes++;
return self::$handler->set($name, $value, $expire);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存标识
* @return boolean
*/
public static function rm($name)
{
self::init();
return self::$handler->rm($name);
}
/**
* 清除缓存
* @access public
* @return boolean
*/
public static function clear()
{
self::init();
return self::$handler->clear();
}
}

View File

@@ -18,9 +18,8 @@ use think\Exception;
* Apc缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Apc implements CacheInterface
class Apc
{
protected $options = [
'expire' => 0,
'prefix' => '',
@@ -53,7 +52,6 @@ class Apc implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
return apc_fetch($this->options['prefix'] . $name);
}
@@ -67,32 +65,11 @@ class Apc implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($result = apc_store($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = apc_fetch('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
apc_delete($key);
}
apc_store('__info__', $queue);
}
}
return $result;
return apc_store($name, $value, $expire);
}
/**

View File

@@ -1,50 +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>
// +----------------------------------------------------------------------
namespace think\cache\driver;
interface CacheInterface
{
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name);
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return boolean
*/
public function set($name, $value, $expire = null);
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name);
/**
* 清除缓存
* @access public
* @return boolean
*/
public function clear();
}

View File

@@ -1,165 +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>
// +----------------------------------------------------------------------
namespace think\cache\driver;
use think\Cache;
/**
* 数据库方式缓存驱动
* CREATE TABLE think_cache (
* cachekey varchar(255) NOT NULL,
* expire int(11) NOT NULL,
* data blob,
* datacrc int(32),
* UNIQUE KEY `cachekey` (`cachekey`)
* );
* @author liu21st <liu21st@gmail.com>
*/
class Db implements CacheInterface
{
protected $handler = null;
protected $options = [
'type' => '',
'dsn' => '',
'hostname' => '',
'hostport' => '',
'username' => '',
'password' => '',
'database' => '',
'charset' => '',
'table' => '',
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = \think\Db::connect((!empty($this->options['hostname']) || !empty($this->options['dsn'])) ? $this->options : []);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name)
{
Cache::$readTimes++;
$name = $this->options['prefix'] . addslashes($name);
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\' AND (`expire` =0 OR `expire`>' . time() . ') LIMIT 0,1');
if (false !== $result) {
$result = $result[0];
$content = $result['data'];
if (function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
}
$content = unserialize($content);
return $content;
} else {
return false;
}
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
* @return boolean
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
$data = serialize($value);
$name = $this->options['prefix'] . addslashes($name);
if (function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data, 3);
}
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
$result = $this->handler->query('select `cachekey` from `' . $this->options['table'] . '` where `cachekey`=\'' . $name . '\' limit 0,1');
if (!empty($result)) {
//更新记录
$result = $this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . $data . '\' ,expire=' . $expire . ' WHERE `cachekey`=\'' . $name . '\'');
} else {
//新增记录
$result = $this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`expire`) VALUES (\'' . $name . '\',\'' . $data . '\',' . $expire . ')');
}
if ($result) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'__info__\' AND `expire` =0 LIMIT 0,1');
$queue = xcache_get('__info__');
if (!$result) {
$this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`expire`) VALUES (\'__info__\',\'\',0)');
$queue = [];
} else {
$queue = unserialize($result[0]['data']);
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->execute('DELETE FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $key . '\'');
}
$this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . serialize($queue) . '\' ,expire=0 WHERE `cachekey`=\'__info__\'');
xcache_set('__info__', $queue);
}
return true;
} else {
return false;
}
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name)
{
$name = $this->options['prefix'] . addslashes($name);
return $this->handler->execute('DELETE FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\'');
}
/**
* 清除缓存
* @access public
* @return boolean
*/
public function clear()
{
return $this->handler->execute('TRUNCATE TABLE `' . $this->options['table'] . '`');
}
}

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class File implements CacheInterface
class File
{
protected $options = [
@@ -98,7 +98,6 @@ class File implements CacheInterface
if (!is_file($filename)) {
return false;
}
Cache::$readTimes++;
$content = file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
@@ -129,7 +128,6 @@ class File implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
@@ -142,25 +140,6 @@ class File implements CacheInterface
$data = "<?php\n//" . sprintf('%012d', $expire) . $data . "\n?>";
$result = file_put_contents($filename, $data);
if ($result) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue_file = dirname($filename) . '/__info__.php';
$queue = unserialize(file_get_contents($queue_file));
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->unlink($this->filename($key));
}
file_put_contents($queue_file, serialize($queue));
}
clearstatcache();
return true;
} else {

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class Lite implements CacheInterface
class Lite
{
protected $options = [
'prefix' => '',
@@ -61,7 +61,6 @@ class Lite implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$filename = $this->filename($name);
if (is_file($filename)) {
// 判断是否过期
@@ -87,7 +86,6 @@ class Lite implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
@@ -96,14 +94,7 @@ class Lite implements CacheInterface
$expire = 10 * 365 * 24 * 3600;
}
$filename = $this->filename($name);
// 缓存数据
/*
$dir = dirname($filename);
// 目录不存在则创建
if (!is_dir($dir))
mkdir($dir,0755,true);
*/
$ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
$ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
// 通过设置修改时间实现有效期
if ($ret) {
touch($filename, time() + $expire);

View File

@@ -14,7 +14,7 @@ namespace think\cache\driver;
use think\Cache;
use think\Exception;
class Memcache implements CacheInterface
class Memcache
{
protected $handler = null;
protected $options = [
@@ -65,7 +65,6 @@ class Memcache implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
return $this->handler->get($this->options['prefix'] . $name);
}
@@ -79,30 +78,11 @@ class Memcache implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($name, $value, 0, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->set('__info__', $queue);
}
return true;
}
return false;

View File

@@ -14,7 +14,7 @@ namespace think\cache\driver;
use think\Cache;
use think\Exception;
class Memcached implements CacheInterface
class Memcached
{
protected $handler = null;
protected $options = [
@@ -66,7 +66,6 @@ class Memcached implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
return $this->handler->get($this->options['prefix'] . $name);
}
@@ -80,31 +79,12 @@ class Memcached implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
$expire = 0 == $expire ? 0 : time() + $expire;
if ($this->handler->set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->set('__info__', $queue);
}
return true;
}
return false;

View File

@@ -19,7 +19,7 @@ use think\Exception;
* 要求安装phpredis扩展https://github.com/nicolasff/phpredis
* @author 尘缘 <130775@qq.com>
*/
class Redis implements CacheInterface
class Redis
{
protected $handler = null;
protected $options = [
@@ -64,7 +64,6 @@ class Redis implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$value = $this->handler->get($this->options['prefix'] . $name);
$jsonData = json_decode($value, true);
// 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
@@ -81,7 +80,6 @@ class Redis implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
@@ -93,24 +91,6 @@ class Redis implements CacheInterface
} else {
$result = $this->handler->set($name, $value);
}
if ($result && $this->options['length'] > 0) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
$queue = explode(',', $queue);
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->set('__info__', implode(',', $queue));
}
}
return $result;
}

View File

@@ -16,51 +16,51 @@ use think\Exception;
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=短连接,推荐长连接
],
单例获取:
$redis = \think\Cache::connect(Config::get('cache'));
$redis->master(true);
$redis->get('key');
配置参数:
'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=短连接,推荐长连接
],
单例获取:
$redis = \think\Cache::connect(Config::get('cache'));
$redis->master(true);
$redis->get('key');
*/
/**
* ThinkPHP Redis简单主从实现的高可用方案
*
*
* 扩展依赖https://github.com/phpredis/phpredis
*
*
* 一主一从的实践经验
* 1, A、B为主从正常情况下A写B读通过异步同步到B或者双写性能有损失
* 2, B挂则读写均落到A
* 3, A挂则尝试升级B为主并断开主从尝试写入(要求开启slave-read-only no)
* 4, 手工恢复A并加入B的从
*
*
* 优化建议
* 1key不宜过长value过大时请自行压缩
* 2gzcompress在php7下有兼容问题
*
*
* @todo
* 1, 增加对redisCluster的兼容
* 2, 增加tp5下的单元测试
*
*
* @author 尘缘 <130775@qq.com>
*/
class Redisd
{
protected static $redis_rw_handler;
protected static $redis_err_pool;
protected $options = [
'host' => '127.0.0.1',
'slave' => '',
@@ -72,10 +72,10 @@ class Redisd
'length' => 0,
'prefix' => '',
];
/**
* 为了在单次php请求中复用redis连接第一次获取的options会被缓存第二次使用不同的$options将会无效
*
*
* @param array $options 缓存参数
* @access public
*/
@@ -85,25 +85,25 @@ class Redisd
throw new Exception('_NOT_SUPPERT_:redis');
}
$this->options = $options = array_merge($this->options, $options);
$this->options ['func'] = $options ['persistent'] ? 'pconnect' : 'connect';
$this->options = $options = array_merge($this->options, $options);
$this->options['func'] = $options['persistent'] ? 'pconnect' : 'connect';
$host = explode(",", trim($this->options ['host'], ","));
$host = array_map("trim", $host);
$slave = explode(",", trim($this->options ['slave'], ","));
$host = explode(",", trim($this->options['host'], ","));
$host = array_map("trim", $host);
$slave = explode(",", trim($this->options['slave'], ","));
$slave = array_map("trim", $slave);
$this->options ["server_slave"] = empty($slave) ? $host : $slave;
$this->options ["servers"] = count($slave);
$this->options ["server_master"] = array_shift($host);
$this->options ["server_master_failover"] = $host;
$this->options["server_slave"] = empty($slave) ? $host : $slave;
$this->options["servers"] = count($slave);
$this->options["server_master"] = array_shift($host);
$this->options["server_master_failover"] = $host;
}
/**
* 主从选择器配置多个Host则自动启用读写分离默认主写随机从读
* 随机从读的场景适合读频繁且php与redis从位于单机的架构这样可以减少网络IO
* 一致Hash适合超高可用跨网络读取且从节点较多的情况本业务不考虑该需求
*
*
* @access public
* @param bool $master true 默认主写
*/
@@ -115,26 +115,26 @@ class Redisd
//如果不为主则从配置的host剔除主并随机读从失败以后再随机选择从
//另外一种方案是根据key的一致性hash选择不同的node但读写频繁的业务中可能打开大量的文件句柄
if(!$master && $this->options["servers"] > 1) {
if (!$master && $this->options["servers"] > 1) {
shuffle($this->options["server_slave"]);
$host = array_shift($this->options["server_slave"]);
}else{
} else {
$host = $this->options["server_master"];
}
$this->handler = new \Redis();
$func = $this->options ['func'];
$func = $this->options['func'];
$parse = parse_url($host);
$host = isset($parse['host']) ? $parse['host'] : $host;
$port = isset($parse['host']) ? $parse['port'] : $this->options ['port'];
$this->handler->$func($host, $port, $this->options ['timeout']);
$port = isset($parse['host']) ? $parse['port'] : $this->options['port'];
if ($this->options ['password'] != null) {
$this->handler->auth($this->options ['password']);
$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');
//发生错误则摘掉当前节点
@@ -143,21 +143,21 @@ class Redisd
} 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.
//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.
Log::record(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $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());
if ($master) {
if (!count($this->options["server_master_failover"])) {
throw new Exception("redisd master: no more server_master_failover. {$host} : " . $e->getMessage());
return false;
}
$this->options["server_master"] = array_shift($this->options["server_master_failover"]);
$this->master();
Log::record(sprintf("master is down, try server_master_failover : %s", $this->options["server_master"]), Log::ERROR);
//如果是slave断开主从升主需要手工同步新主的数据到旧主上
@@ -165,23 +165,24 @@ class Redisd
//$this->handler->slaveof();
} else {
//尝试failover如果有其它节点则进行其它节点的尝试
foreach ($this->options["server_slave"] as $k=>$v)
{
if (trim($v) == trim($host))
foreach ($this->options["server_slave"] as $k => $v) {
if (trim($v) == trim($host)) {
unset($this->options["server_slave"][$k]);
}
}
//如果无可用节点,则抛出异常
if(! count($this->options["server_slave"])) {
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} : " . $e->getMessage());
return false;
} else {
Log::record("salve {$host} is down, try another one.", Log::ALERT);
return $this->master(false);
}
}
} catch(\Exception $e) {
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
@@ -190,19 +191,17 @@ class Redisd
/**
* 读取缓存
*
*
* @access public
* @param string $name 缓存key
* @return mixed
*/
public function get($name)
{
Cache::$readTimes++;
$this->master(false);
try {
$value = $this->handler->get($this->options ['prefix'] . $name);
$value = $this->handler->get($this->options['prefix'] . $name);
} catch (\RedisException $e) {
unset(self::$redis_rw_handler[0]);
$this->master();
@@ -217,12 +216,12 @@ class Redisd
$jsonData = $value ? json_decode($value, true) : $value;
}
return ($jsonData === NULL) ? $value : $jsonData;
return (null === $jsonData) ? $value : $jsonData;
}
/**
* 写入缓存
*
*
* @access public
* @param string $name 缓存key
* @param mixed $value 缓存value
@@ -231,26 +230,24 @@ class Redisd
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
$this->master(true);
if (is_null($expire )) {
$expire = $this->options ['expire'];
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options ['prefix'] . $name;
$name = $this->options['prefix'] . $name;
/**
* 兼容历史版本
* Redis不支持存储对象存入对象会转换成字符串
* 但在这里对所有数据做json_decode会有性能开销
*/
$value = (is_object($value) || is_array($value )) ? json_encode($value) : $value;
if ($value === null) {
return $this->handler->delete($this->options ['prefix'] . $name);
$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 (is_int($expire) && $expire) {
@@ -268,62 +265,61 @@ class Redisd
return $result;
}
/**
* 返回句柄对象
* 需要先执行 $redis->master() 连接到 DB
*
*
* @access public
* @return object
*/
function handler()
public function handler()
{
return $this->handler;
}
/**
* 删除缓存
*
*
* @access public
* @param string $name 缓存变量名
* @return boolen
*/
public function rm($name)
{
Cache::$writeTimes++;
$this->master(true);
return $this->handler->delete($this->options ['prefix'] . $name);
return $this->handler->delete($this->options['prefix'] . $name);
}
/**
* 清除缓存
*
*
* @access public
* @return boolen
*/
public function clear()
{
Cache::$writeTimes++;
$this->master(true);
return $this->handler->flushDB ();
return $this->handler->flushDB();
}
/**
* 析构释放连接
*
*
* @access public
*/
public function __destruct()
{
//该方法仅在connect连接时有效
//当使用pconnect时连接会被重用连接的生命周期是fpm进程的生命周期而非一次php的执行。
//当使用pconnect时连接会被重用连接的生命周期是fpm进程的生命周期而非一次php的执行。
//如果代码中使用pconnect close的作用仅是使当前php不能再进行redis请求但无法真正关闭redis长连接连接在后续请求中仍然会被重用直至fpm进程生命周期结束。
try {
if(method_exists($this->handler, "close"))
$this->handler->close ();
if (method_exists($this->handler, "close")) {
$this->handler->close();
}
} catch (\Exception $e) {
}
}
}
}

View File

@@ -18,7 +18,7 @@ use think\Exception;
* SAE Memcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Sae implements CacheInterface
class Sae
{
protected $handler = null;
protected $options = [
@@ -58,7 +58,6 @@ class Sae implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
return $this->handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name);
}
@@ -72,16 +71,11 @@ class Sae implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($_SERVER['HTTP_APPVERSION'] . '/' . $name, $value, 0, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$this->queue($name);
}
return true;
}
return false;
@@ -126,32 +120,4 @@ class Sae implements CacheInterface
return $kv;
}
/**
* 队列缓存
* @access protected
* @param string $key 队列名
* @return mixed
*/
//[sae] 下重写queque队列缓存方法
protected function queue($key)
{
$queue_name = isset($this->options['queue_name']) ? $this->options['queue_name'] : 'think_queue';
$kv = $this->getKv();
$value = $kv->get($queue_name);
if (!$value) {
$value = [];
}
// 进列
if (false === array_search($key, $value)) {
array_push($value, $key);
}
if (count($value) > $this->options['length']) {
// 出列
$key = array_shift($value);
// 删除缓存
$this->rm($key);
}
return $kv->set($queue_name, $value);
}
}

View File

@@ -17,9 +17,8 @@ use think\Cache;
* Secache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Secache implements CacheInterface
class Secache
{
protected $handler = null;
protected $options = [
'project' => '',
@@ -55,7 +54,6 @@ class Secache implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$name = $this->options['prefix'] . $name;
$key = md5($name);
$this->handler->fetch($key, $return);
@@ -72,30 +70,9 @@ class Secache implements CacheInterface
*/
public function set($name, $value)
{
Cache::$writeTimes++;
$name = $this->options['prefix'] . $name;
$key = md5($name);
if ($result = $this->handler->store($key, $value)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->fetch(md5('__info__'));
if (!$queue) {
$queue = [];
}
if (false === array_search($key, $queue)) {
array_push($queue, $key);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->store(md5('__info__'), $queue);
}
}
return $result;
return $this->handler->store($key, $value);
}
/**

View File

@@ -56,7 +56,6 @@ class Sqlite implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$name = $this->options['prefix'] . sqlite_escape_string($name);
$sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1';
$result = sqlite_query($this->handler, $sql);
@@ -81,7 +80,6 @@ class Sqlite implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
$name = $this->options['prefix'] . sqlite_escape_string($name);
$value = sqlite_escape_string(serialize($value));
if (is_null($expire)) {
@@ -94,10 +92,6 @@ class Sqlite implements CacheInterface
}
$sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value,expire) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\')';
if (sqlite_query($this->handler, $sql)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$this->queue($name);
}
return true;
}
return false;

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 测试缓存类
* @author liu21st <liu21st@gmail.com>
*/
class Test implements CacheInterface
class Test
{
/**

View File

@@ -18,9 +18,8 @@ use think\Exception;
* Wincache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Wincache implements CacheInterface
class Wincache
{
protected $options = [
'prefix' => '',
'expire' => 0,
@@ -51,7 +50,6 @@ class Wincache implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$name = $this->options['prefix'] . $name;
return wincache_ucache_exists($name) ? wincache_ucache_get($name) : false;
}
@@ -66,30 +64,11 @@ class Wincache implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if (wincache_ucache_set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = wincache_ucache_get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
wincache_ucache_delete($key);
}
wincache_ucache_set('__info__', $queue);
}
return true;
}
return false;

View File

@@ -18,9 +18,8 @@ use think\Exception;
* Xcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Xcache implements CacheInterface
class Xcache
{
protected $options = [
'prefix' => '',
'expire' => 0,
@@ -50,7 +49,6 @@ class Xcache implements CacheInterface
*/
public function get($name)
{
Cache::$readTimes++;
$name = $this->options['prefix'] . $name;
if (xcache_isset($name)) {
return xcache_get($name);
@@ -68,30 +66,11 @@ class Xcache implements CacheInterface
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if (xcache_set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = xcache_get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
xcache_unset($key);
}
xcache_set('__info__', $queue);
}
return true;
}
return false;

View File

@@ -21,7 +21,7 @@ use think\exception\DbBindParamException;
use think\exception\PDOException;
use think\Log;
abstract class Connection implements ConnectionInterface
abstract class Connection
{
// PDO操作实例
protected $PDOStatement;
@@ -124,6 +124,38 @@ abstract class Connection implements ConnectionInterface
return call_user_func_array([$this->query, $method], $args);
}
/**
* 解析pdo连接的dsn信息
* @access protected
* @param array $config 连接信息
* @return string
*/
abstract protected function parseDsn($config);
/**
* 取得数据表的字段信息
* @access public
* @param string $tableName
* @return array
*/
abstract public function getFields($tableName);
/**
* 取得数据库的表信息
* @access public
* @param string $dbName
* @return array
*/
abstract public function getTables($dbName);
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
abstract protected function getExplain($sql);
/**
* 对返数据表字段信息进行大小写转换出来
* @access public

View File

@@ -1,47 +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>
// +----------------------------------------------------------------------
namespace think\db;
interface ConnectionInterface
{
/**
* 解析pdo连接的dsn信息
* @access public
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config);
/**
* 取得数据表的字段信息
* @access public
* @param string $tableName
* @return array
*/
public function getFields($tableName);
/**
* 取得数据库的表信息
* @access public
* @param string $dbName
* @return array
*/
public function getTables($dbName);
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql);
}

View File

@@ -22,11 +22,11 @@ class Mysql extends Connection
/**
* 解析pdo连接的dsn信息
* @access public
* @access protected
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config)
protected function parseDsn($config)
{
$dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -91,11 +91,11 @@ class Mysql extends Connection
/**
* SQL性能分析
* @access public
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql)
protected function getExplain($sql)
{
$pdo = $this->linkID->query("EXPLAIN " . $sql);
$result = $pdo->fetch(\PDO::FETCH_ASSOC);

View File

@@ -24,11 +24,11 @@ class Oracle extends Connection
/**
* 解析pdo连接的dsn信息
* @access public
* @access protected
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config)
protected function parseDsn($config)
{
$dsn = 'oci:dbname=';
if (!empty($config['hostname'])) {
@@ -139,11 +139,11 @@ class Oracle extends Connection
/**
* SQL性能分析
* @access public
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql)
protected function getExplain($sql)
{
return [];
}

View File

@@ -21,11 +21,11 @@ class Pgsql extends Connection
/**
* 解析pdo连接的dsn信息
* @access public
* @access protected
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config)
protected function parseDsn($config)
{
$dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -79,11 +79,11 @@ class Pgsql extends Connection
/**
* SQL性能分析
* @access public
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql)
protected function getExplain($sql)
{
return [];
}

View File

@@ -21,11 +21,11 @@ class Sqlite extends Connection
/**
* 解析pdo连接的dsn信息
* @access public
* @access protected
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config)
protected function parseDsn($config)
{
$dsn = 'sqlite:' . $config['database'];
return $dsn;
@@ -78,11 +78,11 @@ class Sqlite extends Connection
/**
* SQL性能分析
* @access public
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql)
protected function getExplain($sql)
{
return [];
}

View File

@@ -29,11 +29,11 @@ class Sqlsrv extends Connection
/**
* 解析pdo连接的dsn信息
* @access public
* @access protected
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config)
protected function parseDsn($config)
{
$dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -96,11 +96,11 @@ class Sqlsrv extends Connection
/**
* SQL性能分析
* @access public
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql)
protected function getExplain($sql)
{
return [];
}

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 本地化调试输出到文件
*/
class File implements LogInterface
class File
{
protected $config = [
'time_format' => ' c ',

View File

@@ -1,25 +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>
// +----------------------------------------------------------------------
namespace think\log\driver;
interface LogInterface
{
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = []);
}

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 调试输出到SAE
*/
class Sae implements LogInterface
class Sae
{
protected $config = [
'log_time_format' => ' c ',

View File

@@ -5,7 +5,7 @@
*/
namespace think\log\driver;
class Socket implements LogInterface
class Socket
{
public $port = 1116; //SocketLog 服务的http的端口号

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 模拟测试输出
*/
class Test implements LogInterface
class Test
{
/**
* 日志写入接口

View File

@@ -16,7 +16,7 @@ use think\Debug;
/**
* 页面Trace调试
*/
class Trace implements LogInterface
class Trace
{
protected $config = [
'trace_file' => '',

View File

@@ -13,7 +13,7 @@ namespace think\view\driver;
use think\Exception;
use think\Log;
class Php implements ViewInterface
class Php
{
// 模板引擎参数
protected $config = [

View File

@@ -14,7 +14,7 @@ use think\Exception;
use think\Log;
use think\Template;
class Think implements ViewInterface
class Think
{
// 模板引擎实例
private $template = null;

View File

@@ -1,34 +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>
// +----------------------------------------------------------------------
namespace think\view\driver;
interface ViewInterface
{
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $data 模板变量
* @return void
*/
public function fetch($template, $data = []);
/**
* 渲染模板内容
* @access public
* @param string $content 模板内容
* @param array $data 模板变量
* @return void
*/
public function display($content, $data = []);
}

View File

@@ -160,14 +160,4 @@ abstract class cacheTestCase extends \PHPUnit_Framework_TestCase
$this->assertNotNull(Cache::rm('a'));
}
public function testQueue()
{
$cache = $this->prepare();
$this->assertTrue($cache->set('1', '1'));
$this->assertTrue($cache->set('2', '2'));
$this->assertTrue($cache->set('3', '3'));
$this->assertEquals(1, $cache->get('1'));
$this->assertTrue($cache->set('4', '4'));
$this->assertFalse($cache->get('1'));
}
}

View File

@@ -25,7 +25,7 @@ class fileTest extends cacheTestCase
*/
protected function setUp()
{
\think\Cache::connect(['type' => 'File', 'path'=> CACHE_PATH]);
\think\Cache::connect(['type' => 'File', 'path' => CACHE_PATH]);
}
/**
@@ -39,19 +39,6 @@ class fileTest extends cacheTestCase
return $this->_cacheInstance;
}
// rewrite testQueue
public function testQueue()
{
$cache = $this->prepare();
$this->assertTrue($cache->set('1', '1'));
$this->assertTrue($cache->set('2', '2'));
$this->assertTrue($cache->set('3', '3'));
$this->assertEquals(1, $cache->get('1'));
$this->assertTrue($cache->set('4', '4'));
$this->assertTrue($cache->set('1', false));
$this->assertFalse($cache->get('1'));
}
// skip testExpire
public function testExpire()
{