<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] * * * * * ## 1 數據緩存文件源代碼(/thinkphp/library/think/Cache.php) ~~~ protected static $instance = []; public static $readTimes = 0; public static $writeTimes = 0; protected static $handler = null; ~~~ ~~~ public static function connect(array $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); APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info'); } 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); } ~~~ ## 2 文件分析 > $instance:緩存對象實例數組 > $readTimes,$writeTimes:??? > $heandler:當前緩存處理句柄 1 `public static function connect(array $options=[])` > $options 緩存配置參數 這里的參數與緩存驅動目錄的緩存驅動文件參數一致, 以/thinkphp/library/think/cache/File.php為例 ~~~ protected $options = [ 'expire' => 0, 'cache_subdir' => false, 'path_level' => 1, 'prefix' => '', 'length' => 0, 'path' => LOG_PATH, 'data_compress' => false, ]; ~~~ `$md5 = md5(serialize($options));` 將配置參數組織為作為緩存對象索引 `if (!isset(self::$instance[$md5])) {}` 檢查$instance中是否存在對應索引的緩存對象 `$type = !empty($options['type']) ? $options['type'] : 'File';` 緩存類型,默認為File `$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type);` 根據緩存類型,獲取緩存驅動目錄下的文件默認為File.php `unset($options['type']);` 刪除緩存驅動類型配置,防止創建其他類型出錯 `self::$instance[$md5] = new $class($options);` 創建對應緩存驅動對象 默認為File() `APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info');` 日志記錄 ~~~ self::$handler = self::$instance[$md5]; return self::$handler; ~~~ 保存創建的緩存對象到處理句柄,并返回 * * * * * 2 `public static function __callStatic($method, $params){}` > $method:調用的緩存句柄的方法 > $params:調用方法傳遞的參數 使用魔術方法調用緩存驅動方法 關于魔術方式的原理見php的魔術方法 ~~~ if (is_null(self::$handler)) { self::connect(Config::get('cache')); } ~~~ 如果沒有self::$handler,則調用self::connect()創建緩存處理句柄 `return call_user_func_array([self::$handler, $method], $params);` 使用call_user_func_array 以$params調用$handler的$method, 關于call_user_func_array的原理見php的反射 ## 3 使用方法 1 緩存配置 在全局默認配置文析convention.php件的分析中,有關緩存的配置參數如下 ~~~ 'cache' => [ 'type' => 'File', 'path' => CACHE_PATH, 'prefix' => '', 'expire' => 0, ], ~~~ > type 緩存類型默認為File > path 緩存目錄默認為CACHE_PATH,在base.php中定義, > prefix 緩存前綴 > expire 緩存時間 * * * * * 2 框架使用范例分析 打開文件thinkphp/library/think/Template.php,查找 ` Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);` Cache::set()調用$handler的set方法, 也就是thinkphp/library/think/cache/File.php文件中的set()方法。 其分析見驅動擴展的D:(\cache)緩存驅動 * * * * * 3 緩存方法 在輔助函數helper.php中的S()方法可以用來快速緩存, 使用方法與tp3.2類似,參考[官方手冊](http://www.hmoore.net/manual/thinkphp/1835) ## 4 總結 tp5支持豐富多樣的緩存實現,具體的緩存實現見 驅動擴展的[D:(\cache)緩存驅動](http://www.hmoore.net/zmwtp/tp5/120856) tp5在輔助helper.php文件中使用S()封裝了緩存的快捷接口,分析見 [另:(helper.php)輔助函數](http://www.hmoore.net/zmwtp/tp5/120824) 緩存大量使用在框架的模板解析(Template.php)中。使用方法見[模板解析](http://www.hmoore.net/zmwtp/tp5/120830)的分析
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看