首頁 > php教程 > php手册 > 主體

ThinkPHP3.1.3核心类 Cache.class.php

WBOY
發布: 2016-06-06 20:02:10
原創
1535 人瀏覽過

/ThinkPHP/lib/core/Cache.class.php 一、示例代码 需要注意的就三个函数 1、连接缓存 public function connect($type=,$options=array()) { if(empty($type)) $type = C(DATA_CACHE_TYPE); $type = strtolower(trim($type)); $class = Cache.ucwords($type)

/ThinkPHP/lib/core/Cache.class.php

一、示例代码

需要注意的就三个函数

1、连接缓存

 public function connect($type='',$options=array()) {
        if(empty($type))  $type = C('DATA_CACHE_TYPE');
        $type  = strtolower(trim($type));
        $class = 'Cache'.ucwords($type); /*根据不同的类型 调用不同的缓存引擎  默认TP只提供了 文件缓存方式 在 Lib/Driver/Cache/CacheFile.class.php*/
        if(class_exists($class))
            $cache = new $class($options);
        else
            throw_exception(L('_CACHE_TYPE_INVALID_').':'.$type);
        return $cache;
    }
登入後複製

2、取得缓存类实例

static function getInstance() {
       $param = func_get_args();
        return get_instance_of(__CLASS__,'connect',$param);
    }
登入後複製
这里调用了 function.php 中的 get_instance_of 函数,实际上 是吧 单例模式独立出去了


3、队列缓存

作用就是 设置缓存的长度 

如果不启用缓存队列,在超时时间内,只要set缓存 就是不断添加缓存

额设置的缓存长度 如10 ,那么根据队列“先进先出”的原则,当缓存数量大于10的时候,会删除队首的缓存。


二、值得说的编程小细节:

1、

public function __get($name) {
        return $this->get($name);
    }

    public function __set($name,$value) {
        return $this->set($name,$value);
    }
登入後複製
这里体现了面向对象 多态思想 

子类继承Cache类, Cache类中的$this->set() 是调用的子类中的方法

2、function.php 中的 get_instance_of 函数

把单例模式独立出来 是比较不错的方式



相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!