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鏀寔鐨勫父鐢ㄧ紦瀛樻搷浣溿
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
灞炴
缂撳瓨涓鍧楁暟鎹椂锛屼細杩涜鍨冨溇 鏀堕泦鐨勬鐜囷紙鐧句竾鍒嗕箣涓璁★級銆傞粯璁や负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;
}
寰缂撳瓨涓瓨鍌ㄤ竴涓敤閿悕鍖哄垎鐨勫笺 杩欐槸鍦ㄧ埗绫讳腑瀹氫箟鐨勬柟娉曠殑鍏蜂綋瀹炵幇銆