ThinkPHP3.1.3核心類 Cache.class.php
/ThinkPHP/lib/core/Cache.class.php
一、示例代碼
需要注意的就三個(gè)函數(shù)
1、連接緩存
?public?function?connect($type='',$options=array())?{
????????if(empty($type))??$type?=?C('DATA_CACHE_TYPE');
????????$type??=?strtolower(trim($type));
????????$class?=?'Cache'.ucwords($type);?/*根據(jù)不同的類型?調(diào)用不同的緩存引擎??默認(rèn)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、取得緩存類實(shí)例
static?function?getInstance()?{
???????$param?=?func_get_args();
????????return?get_instance_of(__CLASS__,'connect',$param);
????}這里調(diào)用了 function.php 中的 get_instance_of 函數(shù),實(shí)際上 是吧 單例模式獨(dú)立出去了
3、隊(duì)列緩存
作用就是 設(shè)置緩存的長(zhǎng)度?
如果不啟用緩存隊(duì)列,在超時(shí)時(shí)間內(nèi),只要set緩存 就是不斷添加緩存
額設(shè)置的緩存長(zhǎng)度 如10 ,那么根據(jù)隊(duì)列“先進(jìn)先出”的原則,當(dāng)緩存數(shù)量大于10的時(shí)候,會(huì)刪除隊(duì)首的緩存。
二、值得說的編程小細(xì)節(jié):
1、
public?function?__get($name)?{
????????return?$this->get($name);
????}
????public?function?__set($name,$value)?{
????????return?$this->set($name,$value);
????}這里體現(xiàn)了面向?qū)ο?多態(tài)思想?
子類繼承Cache類, Cache類中的$this->set() 是調(diào)用的子類中的方法
2、function.php 中的 get_instance_of 函數(shù)
把單例模式獨(dú)立出來 是比較不錯(cuò)的方式





