From ac0547e36b84831cd64d0c82355ad2697d04211d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 23 Jan 2016 13:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=A8=A1=E5=BC=8F=20?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E5=AD=97=E6=AE=B5=E7=BC=93=E5=AD=98=20?= =?UTF-8?q?=E5=8F=96=E6=B6=88app=E7=B1=BB=E7=9A=84=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C=E7=BC=93=E5=AD=98=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E8=87=AA=E5=8A=A8=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 5 ----- library/think/Cache.php | 21 +++++++++++++++------ library/think/Log.php | 10 ++++++++-- library/think/Model.php | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 0dba6fe8..fac13095 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -51,11 +51,6 @@ class App } } - // 日志初始化 - Log::init($config['log']); - // 缓存初始化 - Cache::connect($config['cache']); - // 设置系统时区 date_default_timezone_set($config['default_timezone']); diff --git a/library/think/Cache.php b/library/think/Cache.php index 37cb2a09..66046bd0 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -13,8 +13,9 @@ namespace think; class Cache { - public static $readTimes = 0; - public static $writeTimes = 0; + protected static $instance = []; + public static $readTimes = 0; + public static $writeTimes = 0; /** * 操作句柄 @@ -31,15 +32,23 @@ class Cache */ public static function connect(array $options = []) { - $type = !empty($options['type']) ? $options['type'] : 'File'; - $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type); - unset($options['type']); - self::$handler = new $class($options); + $md5 = md5(serialize($options)); + if (!isset(self::$instance[$md5])) { + $type = !empty($options['type']) ? $options['type'] : 'File'; + $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type); + unset($options['type']); + self::$instance[$md5] = new $class($options); + } + self::$handler = self::$instance[$md5]; return self::$handler; } public static function __callStatic($method, $params) { + if (is_null(self::$handler)) { + // 自动初始化缓存 + self::connect(Config::get('cache')); + } return call_user_func_array([self::$handler, $method], $params); } } diff --git a/library/think/Log.php b/library/think/Log.php index cb941c31..16853dab 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -76,7 +76,10 @@ class Log */ public static function save() { - self::$driver && self::$driver->save(self::$log); + if (is_null(self::$driver)) { + self::init(Config::get('log')); + } + self::$driver->save(self::$log); } /** @@ -95,8 +98,11 @@ class Log // 监听log_write APP_HOOK && Hook::listen('log_write', $log); + if (is_null(self::$driver)) { + self::init(Config::get('log')); + } // 写入日志 - self::$driver && self::$driver->save($log); + self::$driver->save($log); } /** diff --git a/library/think/Model.php b/library/think/Model.php index e52f7758..e9ff5205 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1142,7 +1142,7 @@ class Model } // 记录字段类型信息 $this->fields['_type'] = $type; - Cache::set($guid, $this->fields); + APP_DEBUG && Cache::set($guid, $this->fields); $fields = $this->fields; } else { $this->fields = $fields;