[李景山php]每天TP5-20161222|thinkphp5-Cache.php

來源:互聯網
上載者:User

標籤:thinkphp

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;use think\App;// 快取檔案 處理,此處是 thinkphp 庫檔案,怎麼能用thinkphp檔案呢class Cache{    protected static $instance = [];// 受保護 靜態 執行個體化控制代碼 數組    public static $readTimes   = 0;//讀時間    public static $writeTimes  = 0;// 寫入時間    /**     * 操作控制代碼     * @var object     * @access protected     */    protected static $handler;// 當前操作 控制代碼 受保護的 靜態    /**     * 串連緩衝     * @access public     * @param array         $options  配置數組     * @param bool|string   $name 緩衝串連標識 true 強制重新串連     * @return \think\cache\Driver     */    public static 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, ‘\\‘) ? $type : ‘\\think\\cache\\driver\\‘ . ucwords($type);// 產生對應的類名            // 記錄初始化資訊            App::$debug && Log::record(‘[ CACHE ] INIT ‘ . $type, ‘info‘);// 如果開啟調試資訊,就記錄開始資訊            if (true === $name) {// 如果 強制重新初始化                return new $class($options);// 返回新的 執行個體化數組 全部重新來過 執行個體化 之後不儲存而已            } else {                self::$instance[$name] = new $class($options);// 否則 部分 初始化            }        }        self::$handler = self::$instance[$name];//把建立的執行個體化 存起來        return self::$handler;// 返回控制代碼    }// connect 實際的目的就是 return new $class($option) 如果是true 不存, flase 就存到 $instance[$name]裡面    /**     * 自動初始化緩衝     * @access public     * @param array         $options  配置數組     * @return void     */    public static function init(array $options = [])    {        if (is_null(self::$handler)) {// 如果當前沒有唄使用的 對象 ,就處理,否則 什麼事情都不做            // 自動初始化緩衝            if (!empty($options)) {// 選項不為空白                self::connect($options);            } elseif (‘complex‘ == Config::get(‘cache.type‘)) {// 特殊類型                self::connect(Config::get(‘cache.default‘));// 選項 擷取            } else {// 擷取預設選項                self::connect(Config::get(‘cache‘));            }// 總之,就是跟句不同的選項進行 執行個體化, 此處為公用的 靜態 函數,也就意味這外部是可以調用的        }    }// 自動初始化,    /**     * 切換緩衝類型 需要配置 cache.type 為 complex     * @access public     * @param string $name 緩衝標識     * @return \think\cache\Driver     */    public static function store($name)    {        if (‘complex‘ == Config::get(‘cache.type‘)) {// 如果 類型 特殊,重新設定            self::connect(Config::get(‘cache.‘ . $name), strtolower($name));        }// 只有這樣的情況下,才能完成切換,否則是不行的        return self::$handler;    }// 切換緩衝 控制代碼    /**     * 判斷緩衝是否存在     * @access public     * @param string $name 緩衝變數名     * @return bool     */    public static function has($name)    {        self::init();// 初始化        self::$readTimes++;//讀取次數加一        return self::$handler->has($name);// 通過注入後的class 尋找是否包含這個資料    }// 判讀當前緩衝是否存在    /**     * 讀取緩衝     * @access public     * @param string $name 緩衝標識     * @param mixed  $default 預設值     * @return mixed     */    public static function get($name, $default = false)    {        self::init();        self::$readTimes++;        return self::$handler->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::init();        self::$writeTimes++;        return self::$handler->set($name, $value, $expire);    }//外包寫入,靜態方法    /**     * 自增緩衝(針對數值緩衝)     * @access public     * @param string    $name 緩衝變數名     * @param int       $step 步長     * @return false|int     */    public static function inc($name, $step = 1)    {        self::init();        self::$writeTimes++;        return self::$handler->inc($name, $step);    }// 自增    /**     * 自減緩衝(針對數值緩衝)     * @access public     * @param string    $name 緩衝變數名     * @param int       $step 步長     * @return false|int     */    public static function dec($name, $step = 1)    {        self::init();        self::$writeTimes++;        return self::$handler->dec($name, $step);    }    /**     * 刪除緩衝     * @access public     * @param string    $name 緩衝標識     * @return boolean     */    public static function rm($name)    {        self::init();        self::$writeTimes++;        return self::$handler->rm($name);    }    /**     * 清除緩衝     * @access public     * @param string $tag 標籤名     * @return boolean     */    public static function clear($tag = null)    {        self::init();        self::$writeTimes++;        return self::$handler->clear($tag);    }    /**     * 緩衝標籤     * @access public     * @param string        $name 標籤名     * @param string|array  $keys 緩衝標識     * @param bool          $overlay 是否覆蓋     * @return \think\cache\Driver     */    public static function tag($name, $keys = null, $overlay = false)    {        self::init();        return self::$handler->tag($name, $keys, $overlay);    }// 標籤擷取}


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1872495

[李景山php]每天TP5-20161222|thinkphp5-Cache.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.