Yii Framework v1.1.10 绫诲弬鑰

CFileCache

system.caching
缁ф壙 class CFileCache » CCache » CApplicationComponent » CComponent
瀹炵幇 ArrayAccess, ICache, IApplicationComponent
鐗堟湰 $Id: CFileCache.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/caching/CFileCache.php
CFileCache鎻愪緵浜嗕竴涓熀浜庢枃浠剁殑缂撳瓨鏈哄埗銆

CFileCache灏嗘瘡涓紦瀛樻暟鎹瓨鍌ㄥ湪鐙珛鐨勬枃浠堕噷锛 鏂囦欢浣嶄簬cachePath鐩綍锛岄粯璁ゆ槸protected/runtime/cache CFileCache浼氳嚜鍔ㄦ墽琛屽瀮鍦炬敹闆嗕互鍒犻櫎杩囨湡缂撳瓨鏂囦欢銆

鏌ョ湅CCache鎿嶄綔鎵嬪唽浠ヤ簡瑙FileCache鏀寔鐨勫父鐢ㄧ紦瀛樻搷浣溿

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
behaviors array 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 CApplicationComponent
cacheFileSuffix string 缂撳瓨鏂囦欢鍚庣紑锛岄粯璁ゆ槸. CFileCache
cachePath string 瀛樺偍缂撳瓨鏂囦欢鐨勭洰褰曘傞粯璁ゆ槸null锛屽嵆浣跨敤 protected/runtime/cache鐩綍銆 CFileCache
directoryLevel integer 瀛樺偍缂撳瓨鏂囦欢鐨勫瓙鐩綍绾у埆銆傞粯璁ゆ槸0锛 鍗充笉浣跨敤瀛愮洰褰曘傝嫢绯荤粺鐨勭紦瀛樻枃浠舵暟鐩法澶э紙1涓囦互涓婏級锛 浣犲彲鑳介渶瑕佸皢璇ュ艰缃负1鎴栬2浠ラ伩鍏嶆枃浠剁郴缁熻秴杩囪礋鎷呫 杩欎釜灞炴х殑鍊煎簲涓嶈秴杩16锛堝缓璁皬浜3锛夈 CFileCache
gCProbability integer 缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0. CFileCache
isInitialized boolean 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
keyPrefix string 鍔犲湪姣忎釜缂撳瓨閿悕鍓嶇殑瀛楃涓蹭互淇濊瘉閿悕鏄敮涓鐨勩傞粯璁ゅ兼槸application ID CCache

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
add() 浠呬粎鍦ㄧ攧鍒紦瀛樺肩殑閿悕涓嶅瓨鍦ㄧ殑鎯呭喌涓嬶紝寰缂撳瓨涓瓨鍌ㄥ笺 CCache
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
delete() 浠庣紦瀛樹腑鍒犻櫎鎸囧畾閿悕瀵瑰簲鐨勫 CCache
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
flush() 鍒犻櫎鎵鏈夌紦瀛樺笺 CCache
gc() 鍒犻櫎杩囨湡缂撳瓨鏂囦欢銆 CFileCache
get() 浠庣紦瀛樹腑妫绱竴涓尮閰嶆寚瀹氶敭鍚嶇殑鍊笺 CCache
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getGCProbability() 杩斿洖缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0.01%鐨勬鐜囥 CFileCache
getIsInitialized() 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鍒濆鍖栬繖涓簲鐢ㄧ▼搴忕粍浠躲 CFileCache
mget() 浠庣紦瀛樹腑妫绱㈠嚭澶氫釜鍖归厤鎸囧畾閿悕鐨勫笺 CCache
offsetExists() 杩斿洖鎸囧畾閿悕鐨勭紦瀛樻潯鐩槸鍚﹀瓨鍦ㄣ CCache
offsetGet() 浠庣紦瀛樹腑妫绱竴涓尮閰嶆寚瀹氶敭鍚嶇殑鍊笺 CCache
offsetSet() 寰缂撳瓨涓瓨鍌ㄤ竴涓敤閿悕鍖哄垎鐨勫笺 CCache
offsetUnset() 浠庣紦瀛樹腑鍒犻櫎鎸囧畾閿悕瀵瑰簲鐨勫 CCache
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
set() 鏍规嵁涓涓敤浠ョ攧鍒殑閿悕寰缂撳瓨涓瓨鍌ㄤ竴涓笺 CCache
setGCProbability() 璁剧疆缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0.01%鐨勬鐜囥 鏁板煎繀椤诲湪0鍒1000000涔嬮棿銆0鎰忓懗鐫涓嶄細鎵ц鍨冨溇鏀堕泦銆 CFileCache

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
addValue() 浠呬粎鍦ㄧ攧鍒紦瀛樺肩殑閿悕涓嶅瓨鍦ㄧ殑鎯呭喌涓嬶紝寰缂撳瓨涓瓨鍌ㄥ笺 CFileCache
deleteValue() 浠庣紦瀛樹腑鍒犻櫎鎸囧畾閿悕瀵瑰簲鐨勫 CFileCache
flushValues() 鍒犻櫎鎵鏈夌紦瀛樺笺 CFileCache
generateUniqueKey() CCache
getCacheFile() 鏍规嵁閿悕杩斿洖缂撳瓨鏂囦欢璺緞 CFileCache
getValue() 浠庣紦瀛樹腑妫绱竴涓尮閰嶆寚瀹氶敭鍚嶇殑鍊笺 CFileCache
getValues() 浠庣紦瀛樹腑妫绱㈠嚭澶氫釜鍖归厤鎸囧畾閿悕鐨勫笺 CCache
setValue() 寰缂撳瓨涓瓨鍌ㄤ竴涓敤閿悕鍖哄垎鐨勫笺 CFileCache

灞炴ц缁

cacheFileSuffix 灞炴
public string $cacheFileSuffix;

缂撳瓨鏂囦欢鍚庣紑锛岄粯璁ゆ槸.bin銆

cachePath 灞炴
public string $cachePath;

瀛樺偍缂撳瓨鏂囦欢鐨勭洰褰曘傞粯璁ゆ槸null锛屽嵆浣跨敤 protected/runtime/cache鐩綍銆

directoryLevel 灞炴
public integer $directoryLevel;

瀛樺偍缂撳瓨鏂囦欢鐨勫瓙鐩綍绾у埆銆傞粯璁ゆ槸0锛 鍗充笉浣跨敤瀛愮洰褰曘傝嫢绯荤粺鐨勭紦瀛樻枃浠舵暟鐩法澶э紙1涓囦互涓婏級锛 浣犲彲鑳介渶瑕佸皢璇ュ艰缃负1鎴栬2浠ラ伩鍏嶆枃浠剁郴缁熻秴杩囪礋鎷呫 杩欎釜灞炴х殑鍊煎簲涓嶈秴杩16锛堝缓璁皬浜3锛夈

gCProbability 灞炴
public integer getGCProbability()
public void setGCProbability(integer $value)

缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0.01%鐨勬鐜囥

鏂规硶璇︾粏

addValue() 鏂规硶
protected boolean addValue(string $key, string $value, integer $expire)
$key string 鐢ㄤ互鐢勫埆缂撳瓨鍊肩殑閿悕
$value string 瑕佺紦瀛樼殑鍊
$expire integer 浠ョ涓哄崟浣嶇殑鏁板硷紝琛ㄧず缂撳瓨鐨勮繃鏈熸椂闂淬備负0鍒欐案涓嶈繃鏈熴
{return} boolean 鎴愬姛瀛樺偍鍒扮紦瀛樹腑鍒欒繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
婧愮爜锛 framework/caching/CFileCache.php#158 (鏄剧ず)
protected function addValue($key,$value,$expire)
{
    
$cacheFile=$this->getCacheFile($key);
    if(@
filemtime($cacheFile)>time())
        return 
false;
    return 
$this->setValue($key,$value,$expire);
}

浠呬粎鍦ㄧ攧鍒紦瀛樺肩殑閿悕涓嶅瓨鍦ㄧ殑鎯呭喌涓嬶紝寰缂撳瓨涓瓨鍌ㄥ笺 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆

deleteValue() 鏂规硶
protected boolean deleteValue(string $key)
$key string 瑕佸垹闄ゅ肩殑閿悕
{return} boolean 濡傛灉鍒犻櫎鏈熼棿娌℃湁鍙戠敓閿欒
婧愮爜锛 framework/caching/CFileCache.php#172 (鏄剧ず)
protected function deleteValue($key)
{
    
$cacheFile=$this->getCacheFile($key);
    return @
unlink($cacheFile);
}

浠庣紦瀛樹腑鍒犻櫎鎸囧畾閿悕瀵瑰簲鐨勫 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆

flushValues() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.5锛
protected boolean flushValues()
{return} boolean 濡傛灉娓呯┖鎿嶄綔鎴愬姛鎵ц銆
婧愮爜锛 framework/caching/CFileCache.php#94 (鏄剧ず)
protected function flushValues()
{
    
$this->gc(false);
    return 
true;
}

鍒犻櫎鎵鏈夌紦瀛樺笺 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆

gc() 鏂规硶
public void gc(boolean $expiredOnly=true, string $path=NULL)
$expiredOnly boolean 鏄惁鍙垹闄よ繃鏈熺紦瀛樻枃浠躲傝嫢涓篺alse锛屽垯鎵鏈夊湪鐩綍cachePath涓嬬殑鏂囦欢浼氳鍒犻櫎銆
$path string 瑕佸垹闄ょ殑璺緞銆傝嫢涓簄ull锛屽畠浼氳璁炬垚cachePath
婧愮爜锛 framework/caching/CFileCache.php#204 (鏄剧ず)
public function gc($expiredOnly=true,$path=null)
{
    if(
$path===null)
        
$path=$this->cachePath;
    if((
$handle=opendir($path))===false)
        return;
    while((
$file=readdir($handle))!==false)
    {
        if(
$file[0]==='.')
            continue;
        
$fullPath=$path.DIRECTORY_SEPARATOR.$file;
        if(
is_dir($fullPath))
            
$this->gc($expiredOnly,$fullPath);
        else if(
$expiredOnly && @filemtime($fullPath)<time() || !$expiredOnly)
            @
unlink($fullPath);
    }
    
closedir($handle);
}

鍒犻櫎杩囨湡缂撳瓨鏂囦欢銆

getCacheFile() 鏂规硶
protected string getCacheFile(string $key)
$key string 缂撳瓨閿
{return} string 缂撳瓨鏂囦欢璺緞
婧愮爜锛 framework/caching/CFileCache.php#183 (鏄剧ず)
protected function getCacheFile($key)
{
    if(
$this->directoryLevel>0)
    {
        
$base=$this->cachePath;
        for(
$i=0;$i<$this->directoryLevel;++$i)
        {
            if((
$prefix=substr($key,$i+$i,2))!==false)
                
$base.=DIRECTORY_SEPARATOR.$prefix;
        }
        return 
$base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
    }
    else
        return 
$this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
}

鏍规嵁閿悕杩斿洖缂撳瓨鏂囦欢璺緞

getGCProbability() 鏂规硶
public integer getGCProbability()
{return} integer 缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0.01%鐨勬鐜囥
婧愮爜锛 framework/caching/CFileCache.php#68 (鏄剧ず)
public function getGCProbability()
{
    return 
$this->_gcProbability;
}

getValue() 鏂规硶
protected string getValue(string $key)
$key string 鐢ㄤ互鐢勫埆缂撳瓨鍊肩殑鍞竴閿悕
{return} string 缂撳瓨涓瓨鍌ㄧ殑鍊硷紝濡傛灉璇ュ间笉瀛樺湪鎴栬呭凡杩囨湡鍒欒繑鍥瀎alse銆
婧愮爜锛 framework/caching/CFileCache.php#106 (鏄剧ず)
protected function getValue($key)
{
    
$cacheFile=$this->getCacheFile($key);
    if((
$time=@filemtime($cacheFile))>time())
        return @
file_get_contents($cacheFile);
    else if(
$time>0)
        @
unlink($cacheFile);
    return 
false;
}

浠庣紦瀛樹腑妫绱竴涓尮閰嶆寚瀹氶敭鍚嶇殑鍊笺 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆

init() 鏂规硶
public void init()
婧愮爜锛 framework/caching/CFileCache.php#55 (鏄剧ず)
public function init()
{
    
parent::init();
    if(
$this->cachePath===null)
        
$this->cachePath=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache';
    if(!
is_dir($this->cachePath))
        
mkdir($this->cachePath,0777,true);
}

鍒濆鍖栬繖涓簲鐢ㄧ▼搴忕粍浠躲 璇ユ柟娉曞湪鎺ュ彛IApplicationComponent涓湁瑕佹眰銆 瀹冧細妫鏌emcache鏄惁鍙敤銆

setGCProbability() 鏂规硶
public void setGCProbability(integer $value)
$value integer 缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负100锛屾剰鍛崇潃0.01%鐨勬鐜囥 鏁板煎繀椤诲湪0鍒1000000涔嬮棿銆0鎰忓懗鐫涓嶄細鎵ц鍨冨溇鏀堕泦銆
婧愮爜锛 framework/caching/CFileCache.php#78 (鏄剧ず)
public function setGCProbability($value)
{
    
$value=(int)$value;
    if(
$value<0)
        
$value=0;
    if(
$value>1000000)
        
$value=1000000;
    
$this->_gcProbability=$value;
}

setValue() 鏂规硶
protected boolean setValue(string $key, string $value, integer $expire)
$key string 鐢ㄤ互鐢勫埆缂撳瓨鍊肩殑閿悕
$value string 瑕佺紦瀛樼殑鍊
$expire integer 浠ョ涓哄崟浣嶇殑鏁板硷紝琛ㄧず缂撳瓨鐨勮繃鏈熸椂闂淬備负0鍒欐案涓嶈繃鏈熴
{return} boolean 鎴愬姛瀛樺偍鍒扮紦瀛樹腑鍒欒繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
婧愮爜锛 framework/caching/CFileCache.php#125 (鏄剧ず)
protected function setValue($key,$value,$expire)
{
    if(!
$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
    {
        
$this->gc();
        
$this->_gced=true;
    }

    if(
$expire<=0)
        
$expire=31536000// 1 year
    
$expire+=time();

    
$cacheFile=$this->getCacheFile($key);
    if(
$this->directoryLevel>0)
        @
mkdir(dirname($cacheFile),0777,true);
    if(@
file_put_contents($cacheFile,$value,LOCK_EX)!==false)
    {
        @
chmod($cacheFile,0777);
        return @
touch($cacheFile,$expire);
    }
    else
        return 
false;
}

寰缂撳瓨涓瓨鍌ㄤ竴涓敤閿悕鍖哄垎鐨勫笺 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.