内核更新
This commit is contained in:
159
core/library/think/cache/Driver.php
vendored
Normal file
159
core/library/think/cache/Driver.php
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* 缓存基础类
|
||||
*/
|
||||
abstract class Driver
|
||||
{
|
||||
protected $options = [];
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function has($name);
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get($name, $default = false);
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function set($name, $value, $expire = null);
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
abstract public function inc($name, $step = 1);
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
abstract public function dec($name, $step = 1);
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function rm($name);
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function clear($tag = null);
|
||||
|
||||
/**
|
||||
* 获取实际的缓存标识
|
||||
* @access public
|
||||
* @param string $name 缓存名
|
||||
* @return string
|
||||
*/
|
||||
protected function getCacheKey($name)
|
||||
{
|
||||
return $this->options['prefix'] . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string $name 标签名
|
||||
* @param string|array $keys 缓存标识
|
||||
* @param bool $overlay 是否覆盖
|
||||
* @return $this
|
||||
*/
|
||||
public function tag($name, $keys = null, $overlay = false)
|
||||
{
|
||||
if (is_null($keys)) {
|
||||
$this->tag = $name;
|
||||
} else {
|
||||
$key = 'tag_' . md5($name);
|
||||
if (is_string($keys)) {
|
||||
$keys = explode(',', $keys);
|
||||
}
|
||||
$keys = array_map([$this, 'getCacheKey'], $keys);
|
||||
if ($overlay) {
|
||||
$value = $keys;
|
||||
} else {
|
||||
$value = array_unique(array_merge($this->getTagItem($name), $keys));
|
||||
}
|
||||
$this->set($key, implode(',', $value));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @return void
|
||||
*/
|
||||
protected function setTagItem($name)
|
||||
{
|
||||
if ($this->tag) {
|
||||
$key = 'tag_' . md5($this->tag);
|
||||
$this->tag = null;
|
||||
if ($this->has($key)) {
|
||||
$value = $this->get($key);
|
||||
$value .= ',' . $name;
|
||||
} else {
|
||||
$value = $name;
|
||||
}
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签包含的缓存标识
|
||||
* @access public
|
||||
* @param string $tag 缓存标签
|
||||
* @return array
|
||||
*/
|
||||
protected function getTagItem($tag)
|
||||
{
|
||||
$key = 'tag_' . md5($tag);
|
||||
$value = $this->get($key);
|
||||
if ($value) {
|
||||
return explode(',', $value);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
37
core/library/think/cache/driver/File.php
vendored
37
core/library/think/cache/driver/File.php
vendored
@@ -11,11 +11,13 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* 文件类型缓存类
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class File
|
||||
class File extends Driver
|
||||
{
|
||||
protected $options = [
|
||||
'expire' => 0,
|
||||
@@ -58,16 +60,16 @@ class File
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access private
|
||||
* @access protected
|
||||
* @param string $name 缓存变量名
|
||||
* @return string
|
||||
*/
|
||||
private function filename($name)
|
||||
protected function getCacheKey($name)
|
||||
{
|
||||
$name = md5($name);
|
||||
if ($this->options['cache_subdir']) {
|
||||
// 使用子目录
|
||||
$name = substr($md5, 0, 2) . DS . substr($md5, 2);
|
||||
$name = substr($name, 0, 2) . DS . substr($name, 2);
|
||||
}
|
||||
if ($this->options['prefix']) {
|
||||
$name = $this->options['prefix'] . DS . $name;
|
||||
@@ -88,7 +90,7 @@ class File
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
$filename = $this->getCacheKey($name);
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
@@ -101,7 +103,7 @@ class File
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
$filename = $this->getCacheKey($name);
|
||||
if (!is_file($filename)) {
|
||||
return $default;
|
||||
}
|
||||
@@ -138,8 +140,11 @@ class File
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$filename = $this->filename($name);
|
||||
$data = serialize($value);
|
||||
$filename = $this->getCacheKey($name);
|
||||
if ($this->tag && !is_file($filename)) {
|
||||
$first = true;
|
||||
}
|
||||
$data = serialize($value);
|
||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||
//数据压缩
|
||||
$data = gzcompress($data, 3);
|
||||
@@ -147,6 +152,7 @@ class File
|
||||
$data = "<?php\n//" . sprintf('%012d', $expire) . $data . "\n?>";
|
||||
$result = file_put_contents($filename, $data);
|
||||
if ($result) {
|
||||
isset($first) && $this->setTagItem($filename);
|
||||
clearstatcache();
|
||||
return true;
|
||||
} else {
|
||||
@@ -196,16 +202,26 @@ class File
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
return $this->unlink($this->filename($name));
|
||||
return $this->unlink($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
$this->unlink($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
$fileLsit = (array) glob($this->options['path'] . '*');
|
||||
foreach ($fileLsit as $path) {
|
||||
is_file($path) && unlink($path);
|
||||
@@ -224,4 +240,5 @@ class File
|
||||
{
|
||||
return is_file($path) && unlink($path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
38
core/library/think/cache/driver/Lite.php
vendored
38
core/library/think/cache/driver/Lite.php
vendored
@@ -11,11 +11,13 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* 文件类型缓存类
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Lite
|
||||
class Lite extends Driver
|
||||
{
|
||||
protected $options = [
|
||||
'prefix' => '',
|
||||
@@ -42,11 +44,11 @@ class Lite
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access private
|
||||
* @access protected
|
||||
* @param string $name 缓存变量名
|
||||
* @return string
|
||||
*/
|
||||
private function filename($name)
|
||||
protected function getCacheKey($name)
|
||||
{
|
||||
return $this->options['path'] . $this->options['prefix'] . md5($name) . '.php';
|
||||
}
|
||||
@@ -59,7 +61,7 @@ class Lite
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
$filename = $this->getCacheKey($name);
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
@@ -72,7 +74,7 @@ class Lite
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
$filename = $this->getCacheKey($name);
|
||||
if (is_file($filename)) {
|
||||
// 判断是否过期
|
||||
$mtime = filemtime($filename);
|
||||
@@ -104,10 +106,14 @@ class Lite
|
||||
if (0 === $expire) {
|
||||
$expire = 10 * 365 * 24 * 3600;
|
||||
}
|
||||
$filename = $this->filename($name);
|
||||
$ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
|
||||
$filename = $this->getCacheKey($name);
|
||||
if ($this->tag && !is_file($filename)) {
|
||||
$first = true;
|
||||
}
|
||||
$ret = file_put_contents($filename, ("<?php return " . var_export($value, true) . ";"));
|
||||
// 通过设置修改时间实现有效期
|
||||
if ($ret) {
|
||||
isset($first) && $this->setTagItem($filename);
|
||||
touch($filename, $_SERVER['REQUEST_TIME'] + $expire);
|
||||
}
|
||||
return $ret;
|
||||
@@ -155,18 +161,26 @@ class Lite
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
return unlink($this->filename($name));
|
||||
return unlink($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return bool
|
||||
* @internal param string $name 缓存变量名
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
$filename = $this->filename('*');
|
||||
array_map("unlink", glob($filename));
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
unlink($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
array_map("unlink", glob($this->options['path'] . $this->options['prefix'] . '*.php'));
|
||||
}
|
||||
}
|
||||
|
||||
45
core/library/think/cache/driver/Memcache.php
vendored
45
core/library/think/cache/driver/Memcache.php
vendored
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
use think\Exception;
|
||||
|
||||
class Memcache
|
||||
class Memcache extends Driver
|
||||
{
|
||||
protected $handler = null;
|
||||
protected $options = [
|
||||
@@ -63,8 +64,8 @@ class Memcache
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->get($name) ? true : false;
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->get($key) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +77,7 @@ class Memcache
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$result = $this->handler->get($this->options['prefix'] . $name);
|
||||
$result = $this->handler->get($this->getCacheKey($name));
|
||||
return false !== $result ? $result : $default;
|
||||
}
|
||||
|
||||
@@ -93,8 +94,12 @@ class Memcache
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if ($this->handler->set($name, $value, 0, $expire)) {
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
$key = $this->getCacheKey($name);
|
||||
if ($this->handler->set($key, $value, 0, $expire)) {
|
||||
isset($first) && $this->setTagItem($key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -109,8 +114,8 @@ class Memcache
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->increment($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->increment($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,9 +127,9 @@ class Memcache
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
$value = $this->handler->get($name) - $step;
|
||||
$res = $this->handler->set($name, $value);
|
||||
$key = $this->getCacheKey($name);
|
||||
$value = $this->handler->get($key) - $step;
|
||||
$res = $this->handler->set($key, $value);
|
||||
if (!$res) {
|
||||
return false;
|
||||
} else {
|
||||
@@ -140,19 +145,29 @@ class Memcache
|
||||
*/
|
||||
public function rm($name, $ttl = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
$key = $this->getCacheKey($name);
|
||||
return false === $ttl ?
|
||||
$this->handler->delete($name) :
|
||||
$this->handler->delete($name, $ttl);
|
||||
$this->handler->delete($key) :
|
||||
$this->handler->delete($key, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return bool
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
$this->handler->delete($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
return $this->handler->flush();
|
||||
}
|
||||
}
|
||||
|
||||
44
core/library/think/cache/driver/Memcached.php
vendored
44
core/library/think/cache/driver/Memcached.php
vendored
@@ -11,7 +11,9 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
class Memcached
|
||||
use think\cache\Driver;
|
||||
|
||||
class Memcached extends Driver
|
||||
{
|
||||
protected $handler;
|
||||
protected $options = [
|
||||
@@ -68,8 +70,8 @@ class Memcached
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->get($name) ? true : false;
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->get($key) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +83,7 @@ class Memcached
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$result = $this->handler->get($this->options['prefix'] . $name);
|
||||
$result = $this->handler->get($this->getCacheKey($name));
|
||||
return false !== $result ? $result : $default;
|
||||
}
|
||||
|
||||
@@ -98,9 +100,13 @@ class Memcached
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = 0 == $expire ? 0 : $_SERVER['REQUEST_TIME'] + $expire;
|
||||
if ($this->handler->set($name, $value, $expire)) {
|
||||
if ($this->handler->set($key, $value, $expire)) {
|
||||
isset($first) && $this->setTagItem($key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -115,8 +121,8 @@ class Memcached
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->increment($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->increment($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,9 +134,9 @@ class Memcached
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
$value = $this->handler->get($name) - $step;
|
||||
$res = $this->handler->set($name, $value);
|
||||
$key = $this->getCacheKey($name);
|
||||
$value = $this->handler->get($key) - $step;
|
||||
$res = $this->handler->set($key, $value);
|
||||
if (!$res) {
|
||||
return false;
|
||||
} else {
|
||||
@@ -146,19 +152,27 @@ class Memcached
|
||||
*/
|
||||
public function rm($name, $ttl = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
$key = $this->getCacheKey($name);
|
||||
return false === $ttl ?
|
||||
$this->handler->delete($name) :
|
||||
$this->handler->delete($name, $ttl);
|
||||
$this->handler->delete($key) :
|
||||
$this->handler->delete($key, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return bool
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
$this->handler->deleteMulti($keys);
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
return $this->handler->flush();
|
||||
}
|
||||
}
|
||||
|
||||
40
core/library/think/cache/driver/Redis.php
vendored
40
core/library/think/cache/driver/Redis.php
vendored
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
|
||||
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
|
||||
@@ -18,7 +20,7 @@ namespace think\cache\driver;
|
||||
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
|
||||
* @author 尘缘 <130775@qq.com>
|
||||
*/
|
||||
class Redis
|
||||
class Redis extends Driver
|
||||
{
|
||||
protected $handler = null;
|
||||
protected $options = [
|
||||
@@ -61,7 +63,7 @@ class Redis
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->handler->get($this->options['prefix'] . $name) ? true : false;
|
||||
return $this->handler->get($this->getCacheKey($name)) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +75,7 @@ class Redis
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$value = $this->handler->get($this->options['prefix'] . $name);
|
||||
$value = $this->handler->get($this->getCacheKey($name));
|
||||
if (is_null($value)) {
|
||||
return $default;
|
||||
}
|
||||
@@ -95,14 +97,18 @@ class Redis
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
$key = $this->getCacheKey($name);
|
||||
//对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
|
||||
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
|
||||
if (is_int($expire) && $expire) {
|
||||
$result = $this->handler->setex($name, $expire, $value);
|
||||
$result = $this->handler->setex($key, $expire, $value);
|
||||
} else {
|
||||
$result = $this->handler->set($name, $value);
|
||||
$result = $this->handler->set($key, $value);
|
||||
}
|
||||
isset($first) && $this->setTagItem($key);
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -115,8 +121,8 @@ class Redis
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->incrby($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->incrby($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,8 +134,8 @@ class Redis
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->decrby($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return $this->handler->decrby($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,16 +146,26 @@ class Redis
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
return $this->handler->delete($this->options['prefix'] . $name);
|
||||
return $this->handler->delete($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
$this->handler->delete($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
return $this->handler->flushDB();
|
||||
}
|
||||
|
||||
|
||||
41
core/library/think/cache/driver/Sqlite.php
vendored
41
core/library/think/cache/driver/Sqlite.php
vendored
@@ -11,13 +11,14 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Sqlite缓存驱动
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Sqlite
|
||||
class Sqlite extends Driver
|
||||
{
|
||||
|
||||
protected $options = [
|
||||
@@ -46,6 +47,17 @@ class Sqlite
|
||||
$this->handler = $func($this->options['db']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际的缓存标识
|
||||
* @access public
|
||||
* @param string $name 缓存名
|
||||
* @return string
|
||||
*/
|
||||
protected function getCacheKey($name)
|
||||
{
|
||||
return $this->options['prefix'] . sqlite_escape_string($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
@@ -54,7 +66,7 @@ class Sqlite
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . sqlite_escape_string($name);
|
||||
$name = $this->getCacheKey($name);
|
||||
$sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1';
|
||||
$result = sqlite_query($this->handler, $sql);
|
||||
return sqlite_num_rows($result);
|
||||
@@ -69,7 +81,7 @@ class Sqlite
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . sqlite_escape_string($name);
|
||||
$name = $this->getCacheKey($name);
|
||||
$sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1';
|
||||
$result = sqlite_query($this->handler, $sql);
|
||||
if (sqlite_num_rows($result)) {
|
||||
@@ -93,7 +105,7 @@ class Sqlite
|
||||
*/
|
||||
public function set($name, $value, $expire = null)
|
||||
{
|
||||
$name = $this->options['prefix'] . sqlite_escape_string($name);
|
||||
$name = $this->getCacheKey($name);
|
||||
$value = sqlite_escape_string(serialize($value));
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
@@ -103,7 +115,13 @@ class Sqlite
|
||||
//数据压缩
|
||||
$value = gzcompress($value, 3);
|
||||
}
|
||||
$sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value,expire) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\')';
|
||||
if ($this->tag) {
|
||||
$tag = $this->tag;
|
||||
$this->tag = null;
|
||||
} else {
|
||||
$tag = '';
|
||||
}
|
||||
$sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value, expire, tag) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\', \'' . $tag . '\')';
|
||||
if (sqlite_query($this->handler, $sql)) {
|
||||
return true;
|
||||
}
|
||||
@@ -152,7 +170,7 @@ class Sqlite
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . sqlite_escape_string($name);
|
||||
$name = $this->getCacheKey($name);
|
||||
$sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\'';
|
||||
sqlite_query($this->handler, $sql);
|
||||
return true;
|
||||
@@ -161,12 +179,19 @@ class Sqlite
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
$name = sqlite_escape_string($tag);
|
||||
$sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE tag=\'' . $name . '\'';
|
||||
sqlite_query($this->handler, $sql);
|
||||
return true;
|
||||
}
|
||||
$sql = 'DELETE FROM ' . $this->options['table'];
|
||||
sqlite_query($this->handler, $sql);
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
46
core/library/think/cache/driver/Wincache.php
vendored
46
core/library/think/cache/driver/Wincache.php
vendored
@@ -11,19 +11,22 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Wincache缓存驱动
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Wincache
|
||||
class Wincache extends Driver
|
||||
{
|
||||
protected $options = [
|
||||
'prefix' => '',
|
||||
'expire' => 0,
|
||||
];
|
||||
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @param array $options 缓存参数
|
||||
@@ -48,8 +51,8 @@ class Wincache
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return wincache_ucache_exists($name);
|
||||
$key = $this->getCacheKey($name);
|
||||
return wincache_ucache_exists($key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +64,8 @@ class Wincache
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return wincache_ucache_exists($name) ? wincache_ucache_get($name) : $default;
|
||||
$key = $this->getCacheKey($name);
|
||||
return wincache_ucache_exists($key) ? wincache_ucache_get($key) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,8 +81,12 @@ class Wincache
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if (wincache_ucache_set($name, $value, $expire)) {
|
||||
$key = $this->getCacheKey($name);
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
if (wincache_ucache_set($key, $value, $expire)) {
|
||||
isset($first) && $this->setTagItem($key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -94,8 +101,8 @@ class Wincache
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return wincache_ucache_inc($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return wincache_ucache_inc($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,8 +114,8 @@ class Wincache
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return wincache_ucache_dec($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return wincache_ucache_dec($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,16 +126,27 @@ class Wincache
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
return wincache_ucache_delete($this->options['prefix'] . $name);
|
||||
return wincache_ucache_delete($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
return;
|
||||
if ($tag) {
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
wincache_ucache_delete($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
} else {
|
||||
return wincache_ucache_clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
41
core/library/think/cache/driver/Xcache.php
vendored
41
core/library/think/cache/driver/Xcache.php
vendored
@@ -11,13 +11,14 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Xcache缓存驱动
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Xcache
|
||||
class Xcache extends Driver
|
||||
{
|
||||
protected $options = [
|
||||
'prefix' => '',
|
||||
@@ -48,8 +49,8 @@ class Xcache
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return xcache_isset($name);
|
||||
$key = $this->getCacheKey($name);
|
||||
return xcache_isset($key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +62,8 @@ class Xcache
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return xcache_isset($name) ? xcache_get($name) : $default;
|
||||
$key = $this->getCacheKey($name);
|
||||
return xcache_isset($key) ? xcache_get($key) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,8 +79,12 @@ class Xcache
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if (xcache_set($name, $value, $expire)) {
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
$key = $this->getCacheKey($name);
|
||||
if (xcache_set($key, $value, $expire)) {
|
||||
isset($first) && $this->setTagItem($key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -94,8 +99,8 @@ class Xcache
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return xcache_inc($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return xcache_inc($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,8 +112,8 @@ class Xcache
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return xcache_dec($name, $step);
|
||||
$key = $this->getCacheKey($name);
|
||||
return xcache_dec($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,16 +124,26 @@ class Xcache
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
return xcache_unset($this->options['prefix'] . $name);
|
||||
return xcache_unset($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear()
|
||||
public function clear($tag = null)
|
||||
{
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
foreach ($keys as $key) {
|
||||
xcache_unset($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
if (function_exists('xcache_unset_by_prefix')) {
|
||||
return xcache_unset_by_prefix($this->options['prefix']);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user