feat: 本次提交更新内容如下

php有问题,覆盖下
This commit is contained in:
笔记本里的永平
2025-07-07 14:52:56 +08:00
parent c29fbd1bbb
commit c9a3aaab58
338 changed files with 70315 additions and 12737 deletions

View File

@@ -12,6 +12,7 @@
namespace think\cache\driver;
use think\cache\Driver;
use think\Container;
/**
* 文件类型缓存类
@@ -23,14 +24,16 @@ class File extends Driver
'expire' => 0,
'cache_subdir' => true,
'prefix' => '',
'path' => CACHE_PATH,
'path' => '',
'hash_type' => 'md5',
'data_compress' => false,
'serialize' => true,
];
protected $expire;
/**
* 构函数
* 构函数
* @param array $options
*/
public function __construct($options = [])
@@ -38,9 +41,13 @@ class File extends Driver
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if (substr($this->options['path'], -1) != DS) {
$this->options['path'] .= DS;
if (empty($this->options['path'])) {
$this->options['path'] = Container::get('app')->getRuntimePath() . 'cache' . DIRECTORY_SEPARATOR;
} elseif (substr($this->options['path'], -1) != DIRECTORY_SEPARATOR) {
$this->options['path'] .= DIRECTORY_SEPARATOR;
}
$this->init();
}
@@ -52,11 +59,13 @@ class File extends Driver
private function init()
{
// 创建项目缓存目录
if (!is_dir($this->options['path'])) {
if (mkdir($this->options['path'], 0755, true)) {
try {
if (!is_dir($this->options['path']) && mkdir($this->options['path'], 0755, true)) {
return true;
}
} catch (\Exception $e) {
}
return false;
}
@@ -69,62 +78,77 @@ class File extends Driver
*/
protected function getCacheKey($name, $auto = false)
{
$name = md5($name);
$name = hash($this->options['hash_type'], $name);
if ($this->options['cache_subdir']) {
// 使用子目录
$name = substr($name, 0, 2) . DS . substr($name, 2);
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . substr($name, 2);
}
if ($this->options['prefix']) {
$name = $this->options['prefix'] . DS . $name;
$name = $this->options['prefix'] . DIRECTORY_SEPARATOR . $name;
}
$filename = $this->options['path'] . $name . '.php';
$dir = dirname($filename);
if ($auto && !is_dir($dir)) {
mkdir($dir, 0755, true);
try {
mkdir($dir, 0755, true);
} catch (\Exception $e) {
}
}
return $filename;
}
/**
* 判断缓存是否存在
* @access public
* @param string $name 缓存变量名
* @param string $name 缓存变量名
* @return bool
*/
public function has($name)
{
return $this->get($name) ? true : false;
return false !== $this->get($name) ? true : false;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = false)
{
$this->readTimes++;
$filename = $this->getCacheKey($name);
if (!is_file($filename)) {
return $default;
}
$content = file_get_contents($filename);
$this->expire = null;
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && time() > filemtime($filename) + $expire) {
//缓存过期删除缓存文件
$this->unlink($filename);
return $default;
}
$this->expire = $expire;
$content = substr($content, 32);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
}
$content = unserialize($content);
return $content;
return $this->unserialize($content);
} else {
return $default;
}
@@ -133,30 +157,36 @@ class File extends Driver
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\DateTime $expire 有效时间(秒)
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int|\DateTime $expire 有效时间 0为永久
* @return boolean
*/
public function set($name, $value, $expire = null)
{
$this->writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
if ($expire instanceof \DateTime) {
$expire = $expire->getTimestamp() - time();
}
$expire = $this->getExpireTime($expire);
$filename = $this->getCacheKey($name, true);
if ($this->tag && !is_file($filename)) {
$first = true;
}
$data = serialize($value);
$data = $this->serialize($value);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data, 3);
}
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
$result = file_put_contents($filename, $data);
if ($result) {
isset($first) && $this->setTagItem($filename);
clearstatcache();
@@ -169,8 +199,8 @@ class File extends Driver
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc($name, $step = 1)
@@ -189,8 +219,8 @@ class File extends Driver
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec($name, $step = 1)
@@ -209,14 +239,15 @@ class File extends Driver
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name)
{
$filename = $this->getCacheKey($name);
$this->writeTimes++;
try {
return $this->unlink($filename);
return $this->unlink($this->getCacheKey($name));
} catch (\Exception $e) {
}
}
@@ -224,7 +255,7 @@ class File extends Driver
/**
* 清除缓存
* @access public
* @param string $tag 标签名
* @param string $tag 标签名
* @return boolean
*/
public function clear($tag = null)
@@ -235,27 +266,35 @@ class File extends Driver
foreach ($keys as $key) {
$this->unlink($key);
}
$this->rm('tag_' . md5($tag));
$this->rm($this->getTagKey($tag));
return true;
}
$files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DS : '') . '*');
$this->writeTimes++;
$files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DIRECTORY_SEPARATOR : '') . '*');
foreach ($files as $path) {
if (is_dir($path)) {
$matches = glob($path . '/*.php');
$matches = glob($path . DIRECTORY_SEPARATOR . '*.php');
if (is_array($matches)) {
array_map('unlink', $matches);
array_map(function ($v) {
$this->unlink($v);
}, $matches);
}
rmdir($path);
} else {
unlink($path);
$this->unlink($path);
}
}
return true;
}
/**
* 判断文件是否存在后,删除
* @param $path
* @access private
* @param string $path
* @return bool
* @author byron sampson <xiaobo.sun@qq.com>
* @return boolean