改进Cache类init方法

This commit is contained in:
thinkphp
2017-12-06 16:52:49 +08:00
parent ecd243bb41
commit cc81f12735

View File

@@ -23,12 +23,12 @@ class Cache
/** /**
* @var int 缓存读取次数 * @var int 缓存读取次数
*/ */
public static $readTimes = 0; public static $readTimes = 0;
/** /**
* @var int 缓存写入次数 * @var int 缓存写入次数
*/ */
public static $writeTimes = 0; public static $writeTimes = 0;
/** /**
* @var object 操作句柄 * @var object 操作句柄
@@ -46,17 +46,21 @@ class Cache
{ {
$type = !empty($options['type']) ? $options['type'] : 'File'; $type = !empty($options['type']) ? $options['type'] : 'File';
if (false === $name) $name = md5(serialize($options)); if (false === $name) {
$name = md5(serialize($options));
}
if (true === $name || !isset(self::$instance[$name])) { if (true === $name || !isset(self::$instance[$name])) {
$class = false === strpos($type, '\\') ? $class = false === strpos($type, '\\') ?
'\\think\\cache\\driver\\' . ucwords($type) : '\\think\\cache\\driver\\' . ucwords($type) :
$type; $type;
// 记录初始化信息 // 记录初始化信息
App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info'); App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');
if (true === $name) return new $class($options); if (true === $name) {
return new $class($options);
}
self::$instance[$name] = new $class($options); self::$instance[$name] = new $class($options);
} }
@@ -73,15 +77,15 @@ class Cache
public static function init(array $options = []) public static function init(array $options = [])
{ {
if (is_null(self::$handler)) { if (is_null(self::$handler)) {
if (!empty($options)) { if (empty($options) && 'complex' == Config::get('cache.type')) {
$connect = self::connect($options); $default = Config::get('cache.default');
} elseif ('complex' == Config::get('cache.type')) { // 获取默认缓存配置,并连接
$connect = self::connect(Config::get('cache.default')); $options = Config::get('cache.' . $default['type']) ?: Config::get('cache.default');
} else { } else {
$connect = self::connect(Config::get('cache')); $options = !empty($options) ? $options : Config::get('cache');
} }
self::$handler = $connect; self::$handler = self::connect($options);
} }
return self::$handler; return self::$handler;