取消vendor文件夹的排除
This commit is contained in:
1 parent
55d4d7b9c8
commit
1eafa8f5c9
1279 files changed
+277858
-1
No files matched your search
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache;
|
||||
|
||||
use Closure;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use think\contract\CacheHandlerInterface;
|
||||
use think\exception\InvalidArgumentException;
|
||||
use throwable;
|
||||
|
||||
/**
|
||||
* 缓存基础类
|
||||
*/
|
||||
abstract class Driver implements CacheInterface, CacheHandlerInterface
|
||||
{
|
||||
/**
|
||||
* 驱动句柄
|
||||
* @var object
|
||||
*/
|
||||
protected $handler = null;
|
||||
|
||||
/**
|
||||
* 缓存读取次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $readTimes = 0;
|
||||
|
||||
/**
|
||||
* 缓存写入次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $writeTimes = 0;
|
||||
|
||||
/**
|
||||
* 缓存参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @var array
|
||||
*/
|
||||
protected $tag = [];
|
||||
|
||||
/**
|
||||
* 获取有效期
|
||||
* @access protected
|
||||
* @param integer|DateTimeInterface|DateInterval $expire 有效期
|
||||
* @return int
|
||||
*/
|
||||
protected function getExpireTime($expire): int
|
||||
{
|
||||
if ($expire instanceof DateTimeInterface) {
|
||||
$expire = $expire->getTimestamp() - time();
|
||||
} elseif ($expire instanceof DateInterval) {
|
||||
$expire = DateTime::createFromFormat('U', (string) time())
|
||||
->add($expire)
|
||||
->format('U') - time();
|
||||
}
|
||||
|
||||
return (int) $expire;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际的缓存标识
|
||||
* @access public
|
||||
* @param string $name 缓存名
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheKey(string $name): string
|
||||
{
|
||||
return $this->options['prefix'] . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存并删除
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return mixed
|
||||
*/
|
||||
public function pull(string $name)
|
||||
{
|
||||
$result = $this->get($name, false);
|
||||
|
||||
if ($result) {
|
||||
$this->delete($name);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加(数组)缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $name, $value): void
|
||||
{
|
||||
$item = $this->get($name, []);
|
||||
|
||||
if (!is_array($item)) {
|
||||
throw new InvalidArgumentException('only array cache can be push');
|
||||
}
|
||||
|
||||
$item[] = $value;
|
||||
|
||||
if (count($item) > 1000) {
|
||||
array_shift($item);
|
||||
}
|
||||
|
||||
$item = array_unique($item);
|
||||
|
||||
$this->set($name, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不存在则写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return mixed
|
||||
*/
|
||||
public function remember(string $name, $value, $expire = null)
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
return $this->get($name);
|
||||
}
|
||||
|
||||
$time = time();
|
||||
|
||||
while ($time + 5 > time() && $this->has($name . '_lock')) {
|
||||
// 存在锁定则等待
|
||||
usleep(200000);
|
||||
}
|
||||
|
||||
try {
|
||||
// 锁定
|
||||
$this->set($name . '_lock', true);
|
||||
|
||||
if ($value instanceof Closure) {
|
||||
// 获取缓存数据
|
||||
$value = $value();
|
||||
}
|
||||
|
||||
// 缓存数据
|
||||
$this->set($name, $value, $expire);
|
||||
|
||||
// 解锁
|
||||
$this->delete($name . '_lock');
|
||||
} catch (Exception | throwable $e) {
|
||||
$this->delete($name . '_lock');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string|array $name 标签名
|
||||
* @return TagSet
|
||||
*/
|
||||
public function tag($name): TagSet
|
||||
{
|
||||
$name = (array) $name;
|
||||
$key = implode('-', $name);
|
||||
|
||||
if (!isset($this->tag[$key])) {
|
||||
$name = array_map(function ($val) {
|
||||
return $this->getTagKey($val);
|
||||
}, $name);
|
||||
$this->tag[$key] = new TagSet($name, $this);
|
||||
}
|
||||
|
||||
return $this->tag[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签包含的缓存标识
|
||||
* @access public
|
||||
* @param string $tag 标签标识
|
||||
* @return array
|
||||
*/
|
||||
public function getTagItems(string $tag): array
|
||||
{
|
||||
return $this->get($tag, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际标签名
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return string
|
||||
*/
|
||||
public function getTagKey(string $tag): string
|
||||
{
|
||||
return $this->options['tag_prefix'] . md5($tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化数据
|
||||
* @access protected
|
||||
* @param mixed $data 缓存数据
|
||||
* @return string
|
||||
*/
|
||||
protected function serialize($data): string
|
||||
{
|
||||
if (is_numeric($data)) {
|
||||
return (string) $data;
|
||||
}
|
||||
|
||||
$serialize = $this->options['serialize'][0] ?? "\Opis\Closure\serialize";
|
||||
|
||||
return $serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化数据
|
||||
* @access protected
|
||||
* @param string $data 缓存数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function unserialize(string $data)
|
||||
{
|
||||
if (is_numeric($data)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$unserialize = $this->options['serialize'][1] ?? "\Opis\Closure\unserialize";
|
||||
|
||||
return $unserialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回句柄对象,可执行其它高级方法
|
||||
*
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function handler()
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存读取次数
|
||||
* @access public
|
||||
* @return int
|
||||
*/
|
||||
public function getReadTimes(): int
|
||||
{
|
||||
return $this->readTimes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存写入次数
|
||||
* @access public
|
||||
* @return int
|
||||
*/
|
||||
public function getWriteTimes(): int
|
||||
{
|
||||
return $this->writeTimes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return iterable
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getMultiple($keys, $default = null): iterable
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$result[$key] = $this->get($key, $default);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param iterable $values 缓存数据
|
||||
* @param null|int|\DateInterval $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function setMultiple($values, $ttl = null): bool
|
||||
{
|
||||
foreach ($values as $key => $val) {
|
||||
$result = $this->set($key, $val, $ttl);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function deleteMultiple($keys): bool
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$result = $this->delete($key);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
return call_user_func_array([$this->handler, $method], $args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache;
|
||||
|
||||
/**
|
||||
* 标签集合
|
||||
*/
|
||||
class TagSet
|
||||
{
|
||||
/**
|
||||
* 标签的缓存Key
|
||||
* @var array
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* 缓存句柄
|
||||
* @var Driver
|
||||
*/
|
||||
protected $handler;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $tag 缓存标签
|
||||
* @param Driver $cache 缓存对象
|
||||
*/
|
||||
public function __construct(array $tag, Driver $cache)
|
||||
{
|
||||
$this->tag = $tag;
|
||||
$this->handler = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set(string $name, $value, $expire = null): bool
|
||||
{
|
||||
$this->handler->set($name, $value, $expire);
|
||||
|
||||
$this->append($name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加缓存标识到标签
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return void
|
||||
*/
|
||||
public function append(string $name): void
|
||||
{
|
||||
$name = $this->handler->getCacheKey($name);
|
||||
|
||||
foreach ($this->tag as $tag) {
|
||||
$this->handler->push($tag, $name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param iterable $values 缓存数据
|
||||
* @param null|int|\DateInterval $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function setMultiple($values, $ttl = null): bool
|
||||
{
|
||||
foreach ($values as $key => $val) {
|
||||
$result = $this->set($key, $val, $ttl);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不存在则写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return mixed
|
||||
*/
|
||||
public function remember(string $name, $value, $expire = null)
|
||||
{
|
||||
$result = $this->handler->remember($name, $value, $expire);
|
||||
|
||||
$this->append($name);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
// 指定标签清除
|
||||
foreach ($this->tag as $tag) {
|
||||
$names = $this->handler->getTagItems($tag);
|
||||
|
||||
$this->handler->clearTag($names);
|
||||
$this->handler->delete($tag);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use FilesystemIterator;
|
||||
use think\App;
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* 文件缓存类
|
||||
*/
|
||||
class File extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'expire' => 0,
|
||||
'cache_subdir' => true,
|
||||
'prefix' => '',
|
||||
'path' => '',
|
||||
'hash_type' => 'md5',
|
||||
'data_compress' => false,
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @param App $app
|
||||
* @param array $options 参数
|
||||
*/
|
||||
public function __construct(App $app, array $options = [])
|
||||
{
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if (empty($this->options['path'])) {
|
||||
$this->options['path'] = $app->getRuntimePath() . 'cache';
|
||||
}
|
||||
|
||||
if (substr($this->options['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->options['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheKey(string $name): string
|
||||
{
|
||||
$name = hash($this->options['hash_type'], $name);
|
||||
|
||||
if ($this->options['cache_subdir']) {
|
||||
// 使用子目录
|
||||
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . substr($name, 2);
|
||||
}
|
||||
|
||||
if ($this->options['prefix']) {
|
||||
$name = $this->options['prefix'] . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
|
||||
return $this->options['path'] . $name . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存数据
|
||||
* @param string $name 缓存标识名
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getRaw(string $name)
|
||||
{
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
if (!is_file($filename)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = @file_get_contents($filename);
|
||||
|
||||
if (false !== $content) {
|
||||
$expire = (int) substr($content, 8, 12);
|
||||
if (0 != $expire && time() - $expire > filemtime($filename)) {
|
||||
//缓存过期删除缓存文件
|
||||
$this->unlink($filename);
|
||||
return;
|
||||
}
|
||||
|
||||
$content = substr($content, 32);
|
||||
|
||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||
//启用数据压缩
|
||||
$content = gzuncompress($content);
|
||||
}
|
||||
|
||||
return ['content' => $content, 'expire' => $expire];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
return $this->getRaw($name) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$raw = $this->getRaw($name);
|
||||
|
||||
return is_null($raw) ? $default : $this->unserialize($raw['content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int|\DateTime $expire 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
$dir = dirname($filename);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
try {
|
||||
mkdir($dir, 0755, true);
|
||||
} catch (\Exception $e) {
|
||||
// 创建失败
|
||||
}
|
||||
}
|
||||
|
||||
$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) {
|
||||
clearstatcache();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
if ($raw = $this->getRaw($name)) {
|
||||
$value = $this->unserialize($raw['content']) + $step;
|
||||
$expire = $raw['expire'];
|
||||
} else {
|
||||
$value = $step;
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->set($name, $value, $expire) ? $value : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
return $this->inc($name, -$step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->unlink($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$dirname = $this->options['path'] . $this->options['prefix'];
|
||||
|
||||
$this->rmdir($dirname);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->unlink($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否存在后,删除
|
||||
* @access private
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
private function unlink(string $path): bool
|
||||
{
|
||||
try {
|
||||
return is_file($path) && unlink($path);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹
|
||||
* @param $dirname
|
||||
* @return bool
|
||||
*/
|
||||
private function rmdir($dirname)
|
||||
{
|
||||
if (!is_dir($dirname)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$items = new FilesystemIterator($dirname);
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDir() && !$item->isLink()) {
|
||||
$this->rmdir($item->getPathname());
|
||||
} else {
|
||||
$this->unlink($item->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
@rmdir($dirname);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loaded 3 of 1279 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user