feat: 本次提交更新内容如下
php有问题,覆盖下
This commit is contained in:
@@ -13,82 +13,101 @@ namespace think;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Class Cache
|
||||
*
|
||||
* @package think
|
||||
*
|
||||
* @mixin Driver
|
||||
* @mixin \think\cache\driver\File
|
||||
*/
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* @var array 缓存的实例
|
||||
* 缓存实例
|
||||
* @var array
|
||||
*/
|
||||
public static $instance = [];
|
||||
protected $instance = [];
|
||||
|
||||
/**
|
||||
* @var int 缓存读取次数
|
||||
* 缓存配置
|
||||
* @var array
|
||||
*/
|
||||
public static $readTimes = 0;
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* @var int 缓存写入次数
|
||||
* 操作句柄
|
||||
* @var object
|
||||
*/
|
||||
public static $writeTimes = 0;
|
||||
protected $handler;
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->init($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var object 操作句柄
|
||||
*/
|
||||
public static $handler;
|
||||
|
||||
/**
|
||||
* 连接缓存驱动
|
||||
* 连接缓存
|
||||
* @access public
|
||||
* @param array $options 配置数组
|
||||
* @param bool|string $name 缓存连接标识 true 强制重新连接
|
||||
* @param array $options 配置数组
|
||||
* @param bool|string $name 缓存连接标识 true 强制重新连接
|
||||
* @return Driver
|
||||
*/
|
||||
public static function connect(array $options = [], $name = false)
|
||||
public function connect(array $options = [], $name = false)
|
||||
{
|
||||
$type = !empty($options['type']) ? $options['type'] : 'File';
|
||||
|
||||
if (false === $name) {
|
||||
$name = md5(serialize($options));
|
||||
}
|
||||
|
||||
if (true === $name || !isset(self::$instance[$name])) {
|
||||
$class = false === strpos($type, '\\') ?
|
||||
'\\think\\cache\\driver\\' . ucwords($type) :
|
||||
$type;
|
||||
|
||||
// 记录初始化信息
|
||||
App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');
|
||||
if (true === $name || !isset($this->instance[$name])) {
|
||||
$type = !empty($options['type']) ? $options['type'] : 'File';
|
||||
|
||||
if (true === $name) {
|
||||
return new $class($options);
|
||||
$name = md5(serialize($options));
|
||||
}
|
||||
|
||||
self::$instance[$name] = new $class($options);
|
||||
$this->instance[$name] = Loader::factory($type, '\\think\\cache\\driver\\', $options);
|
||||
}
|
||||
|
||||
return self::$instance[$name];
|
||||
return $this->instance[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动初始化缓存
|
||||
* @access public
|
||||
* @param array $options 配置数组
|
||||
* @param array $options 配置数组
|
||||
* @param bool $force 强制更新
|
||||
* @return Driver
|
||||
*/
|
||||
public static function init(array $options = [])
|
||||
public function init(array $options = [], $force = false)
|
||||
{
|
||||
if (is_null(self::$handler)) {
|
||||
if (empty($options) && 'complex' == Config::get('cache.type')) {
|
||||
$default = Config::get('cache.default');
|
||||
// 获取默认缓存配置,并连接
|
||||
$options = Config::get('cache.' . $default['type']) ?: $default;
|
||||
} elseif (empty($options)) {
|
||||
$options = Config::get('cache');
|
||||
if (is_null($this->handler) || $force) {
|
||||
|
||||
if ('complex' == $options['type']) {
|
||||
$default = $options['default'];
|
||||
$options = isset($options[$default['type']]) ? $options[$default['type']] : $default;
|
||||
}
|
||||
|
||||
self::$handler = self::connect($options);
|
||||
$this->handler = $this->connect($options);
|
||||
}
|
||||
|
||||
return self::$handler;
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
public static function __make(Config $config)
|
||||
{
|
||||
return new static($config->pull('cache'));
|
||||
}
|
||||
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
public function setConfig(array $config)
|
||||
{
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,151 +116,18 @@ class Cache
|
||||
* @param string $name 缓存标识
|
||||
* @return Driver
|
||||
*/
|
||||
public static function store($name = '')
|
||||
public function store($name = '')
|
||||
{
|
||||
if ('' !== $name && 'complex' == Config::get('cache.type')) {
|
||||
return self::connect(Config::get('cache.' . $name), strtolower($name));
|
||||
if ('' !== $name && 'complex' == $this->config['type']) {
|
||||
return $this->connect($this->config[$name], strtolower($name));
|
||||
}
|
||||
|
||||
return self::init();
|
||||
return $this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($name)
|
||||
public function __call($method, $args)
|
||||
{
|
||||
self::$readTimes++;
|
||||
|
||||
return self::init()->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($name, $default = false)
|
||||
{
|
||||
self::$readTimes++;
|
||||
|
||||
return self::init()->get($name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @param mixed $value 存储数据
|
||||
* @param int|null $expire 有效时间 0为永久
|
||||
* @return boolean
|
||||
*/
|
||||
public static function set($name, $value, $expire = null)
|
||||
{
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->set($name, $value, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public static function inc($name, $step = 1)
|
||||
{
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->inc($name, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public static function dec($name, $step = 1)
|
||||
{
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->dec($name, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @return boolean
|
||||
*/
|
||||
public static function rm($name)
|
||||
{
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->rm($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public static function clear($tag = null)
|
||||
{
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->clear($tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存并删除
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return mixed
|
||||
*/
|
||||
public static function pull($name)
|
||||
{
|
||||
self::$readTimes++;
|
||||
self::$writeTimes++;
|
||||
|
||||
return self::init()->pull($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不存在则写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return mixed
|
||||
*/
|
||||
public static function remember($name, $value, $expire = null)
|
||||
{
|
||||
self::$readTimes++;
|
||||
|
||||
return self::init()->remember($name, $value, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string $name 标签名
|
||||
* @param string|array $keys 缓存标识
|
||||
* @param bool $overlay 是否覆盖
|
||||
* @return Driver
|
||||
*/
|
||||
public static function tag($name, $keys = null, $overlay = false)
|
||||
{
|
||||
return self::init()->tag($name, $keys, $overlay);
|
||||
return call_user_func_array([$this->init(), $method], $args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user