mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 07:02:47 +08:00
改进Cache类 及 驱动 改进Connection类及驱动
This commit is contained in:
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;
|
||||
|
||||
Reference in New Issue
Block a user