内核更新
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user