mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
改进Cache类 及 驱动 改进Connection类及驱动
This commit is contained in:
@@ -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::$hander->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::$hander->set($name, $value, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @return boolean
|
||||
*/
|
||||
public static function rm($name)
|
||||
{
|
||||
self::init();
|
||||
return self::$hander->rm($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return boolean
|
||||
*/
|
||||
public static function clear()
|
||||
{
|
||||
self::init();
|
||||
return self::$hander->clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
27
library/think/cache/driver/Apc.php
vendored
27
library/think/cache/driver/Apc.php
vendored
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
50
library/think/cache/driver/CacheInterface.php
vendored
50
library/think/cache/driver/CacheInterface.php
vendored
@@ -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();
|
||||
|
||||
}
|
||||
165
library/think/cache/driver/Db.php
vendored
165
library/think/cache/driver/Db.php
vendored
@@ -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'] . '`');
|
||||
}
|
||||
|
||||
}
|
||||
23
library/think/cache/driver/File.php
vendored
23
library/think/cache/driver/File.php
vendored
@@ -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 {
|
||||
|
||||
13
library/think/cache/driver/Lite.php
vendored
13
library/think/cache/driver/Lite.php
vendored
@@ -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);
|
||||
|
||||
22
library/think/cache/driver/Memcache.php
vendored
22
library/think/cache/driver/Memcache.php
vendored
@@ -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;
|
||||
|
||||
22
library/think/cache/driver/Memcached.php
vendored
22
library/think/cache/driver/Memcached.php
vendored
@@ -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;
|
||||
|
||||
22
library/think/cache/driver/Redis.php
vendored
22
library/think/cache/driver/Redis.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
36
library/think/cache/driver/Sae.php
vendored
36
library/think/cache/driver/Sae.php
vendored
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
27
library/think/cache/driver/Secache.php
vendored
27
library/think/cache/driver/Secache.php
vendored
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
6
library/think/cache/driver/Sqlite.php
vendored
6
library/think/cache/driver/Sqlite.php
vendored
@@ -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;
|
||||
|
||||
2
library/think/cache/driver/Test.php
vendored
2
library/think/cache/driver/Test.php
vendored
@@ -17,7 +17,7 @@ use think\Cache;
|
||||
* 测试缓存类
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Test implements CacheInterface
|
||||
class Test
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
23
library/think/cache/driver/Wincache.php
vendored
23
library/think/cache/driver/Wincache.php
vendored
@@ -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;
|
||||
|
||||
23
library/think/cache/driver/Xcache.php
vendored
23
library/think/cache/driver/Xcache.php
vendored
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace think\log\driver;
|
||||
/**
|
||||
* 本地化调试输出到文件
|
||||
*/
|
||||
class File implements LogInterface
|
||||
class File
|
||||
{
|
||||
protected $config = [
|
||||
'time_format' => ' c ',
|
||||
|
||||
@@ -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 = []);
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace think\log\driver;
|
||||
/**
|
||||
* 调试输出到SAE
|
||||
*/
|
||||
class Sae implements LogInterface
|
||||
class Sae
|
||||
{
|
||||
protected $config = [
|
||||
'log_time_format' => ' c ',
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
namespace think\log\driver;
|
||||
|
||||
class Socket implements LogInterface
|
||||
class Socket
|
||||
{
|
||||
public $port = 1116; //SocketLog 服务的http的端口号
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace think\log\driver;
|
||||
/**
|
||||
* 模拟测试输出
|
||||
*/
|
||||
class Test implements LogInterface
|
||||
class Test
|
||||
{
|
||||
/**
|
||||
* 日志写入接口
|
||||
|
||||
@@ -16,7 +16,7 @@ use think\Debug;
|
||||
/**
|
||||
* 页面Trace调试
|
||||
*/
|
||||
class Trace implements LogInterface
|
||||
class Trace
|
||||
{
|
||||
protected $config = [
|
||||
'trace_file' => '',
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace think\view\driver;
|
||||
use think\Exception;
|
||||
use think\Log;
|
||||
|
||||
class Php implements ViewInterface
|
||||
class Php
|
||||
{
|
||||
// 模板引擎参数
|
||||
protected $config = [
|
||||
|
||||
@@ -14,7 +14,7 @@ use think\Exception;
|
||||
use think\Log;
|
||||
use think\Template;
|
||||
|
||||
class Think implements ViewInterface
|
||||
class Think
|
||||
{
|
||||
// 模板引擎实例
|
||||
private $template = null;
|
||||
|
||||
@@ -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 = []);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user