From cc81f127350aa666c1ce3f74bf6ef280840d01c0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 6 Dec 2017 16:52:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BCache=E7=B1=BBinit=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Cache.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/library/think/Cache.php b/library/think/Cache.php index 9f19e34b..00be217f 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -23,12 +23,12 @@ class Cache /** * @var int 缓存读取次数 */ - public static $readTimes = 0; + public static $readTimes = 0; /** * @var int 缓存写入次数 */ - public static $writeTimes = 0; + public static $writeTimes = 0; /** * @var object 操作句柄 @@ -46,17 +46,21 @@ class Cache { $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])) { $class = false === strpos($type, '\\') ? - '\\think\\cache\\driver\\' . ucwords($type) : - $type; + '\\think\\cache\\driver\\' . ucwords($type) : + $type; // 记录初始化信息 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); } @@ -73,15 +77,15 @@ class Cache public static function init(array $options = []) { if (is_null(self::$handler)) { - if (!empty($options)) { - $connect = self::connect($options); - } elseif ('complex' == Config::get('cache.type')) { - $connect = self::connect(Config::get('cache.default')); + if (empty($options) && 'complex' == Config::get('cache.type')) { + $default = Config::get('cache.default'); + // 获取默认缓存配置,并连接 + $options = Config::get('cache.' . $default['type']) ?: Config::get('cache.default'); } 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;