Yii Framework v1.1.10 绫诲弬鑰

COutputCache

system.web.widgets
缁ф壙 class COutputCache » CFilterWidget » CWidget » CBaseController » CComponent
瀹炵幇 IFilter
婧愯嚜 1.0
鐗堟湰 $Id: COutputCache.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/web/widgets/COutputCache.php
COutputCache 浣夸竴涓姩浣滄垨瑙嗗浘鐗囨鐢熸垚鐨勮緭鍑虹紦鍐插彲鐢ㄣ

濡傛灉闇瑕佹樉绀虹殑杈撳嚭鍦ㄧ紦瀛樹腑鍙敤锛岀紦瀛樹腑鐨勭増鏈皢 浠f浛鐪熷疄鐨勭増鏈鏄剧ず锛岄氳繃杩欑鏂瑰紡鍙互鑺傜渷鐢熸垚 鍘熷杈撳嚭鐨勬椂闂淬

鍥犱负COutputCache鏄粠CFilterWidget鎵╁睍鑰屾潵锛屾墍浠ュ畠鍙互 鐢ㄤ綔杩囨护鍣紙涓哄姩浣滅紦瀛橈級鎴栧皬鐗╀欢锛堜负鐗囨缂撳瓨锛夈 鍦ㄧ浜岀鐢ㄦ硶涓紝閫氬父浣跨敤蹇嵎鏂瑰紡CBaseController::beginCache()CBaseController::endCache()鏉ヤ唬鏇匡紝渚嬪涓嬮潰鐨勮鍥炬枃浠讹細
if($this->beginCache('cacheName',array('property1'=>'value1',...))
{
    // ... 鍦ㄦ澶勬樉绀虹紦瀛樼殑鍐呭
   $this->endCache();
}


COutputCache蹇呴』涓庝竴涓氳繃cacheID鎸囧畾鐨勭紦瀛樺簲鐢ㄧ粍浠跺叡鍚屽伐浣溿 濡傛灉璇ョ紦瀛樺簲鐢ㄧ粍浠朵笉鍙敤锛孋OutputCache涔熷皢琚鐢ㄣ

鏈変袱涓洜绱犲喅瀹氫竴涓紦瀛樼殑鍐呭鏄惁鏈夋晥锛 duration 浠ュ強缂撳瓨鐨 dependency銆 鍓嶈呮寚瀹氫簡鏁版嵁鍙互鍦ㄧ紦瀛樹腑缁存寔鏈夋晥鐨勭鏁帮紙榛樿鍊兼槸 60绉掞級锛岃屽悗鑰呮寚瀹氫簡缂撳瓨鏁版嵁鐨勪緷璧栭」銆傚鏋滀竴涓 渚濊禆椤瑰彂鐢熸敼鍙橈紝锛堜緥濡傦紝鏁版嵁搴撲腑鐨勭浉鍏虫暟鎹鏇存柊锛夛紝 缂撳瓨涓殑鏁版嵁灏嗕笉鍐嶆湁鏁堛 鍙傝CCacheDependency浠ヤ簡瑙f洿澶氬叧浜庣紦瀛樹緷璧栫殑缁嗚妭銆

鏈夋椂鍊欙紝涓轰簡鎵ц涓涓‘瀹氱殑璇锋眰绫诲瀷锛屾湁蹇呰鍏抽棴杈撳嚭缂撳瓨銆 渚嬪锛屾垜浠粎甯屾湜涓涓〃鍗曞湪鍒濇琚姹傜殑鏃跺欒缂撳瓨锛岄殢鍚庡 姝よ〃鍗曠殑鏄剧ず灏嗕笉甯屾湜浣跨敤缂撳瓨锛屽洜涓哄畠鍖呭惈浜嗙敤鎴风殑杈撳叆銆 鎴戜滑鍙互璁剧疆requestTypesarray('GET')浠ヨ揪鍒拌繖涓鐩殑銆

閫氳繃涓浜涘弬鏁扮殑璁剧疆锛屽彲浠ヤ粠缂撳瓨涓緱鍒颁笉鍚岀殑鍐呭銆 COutputCache鏀寔鍥涚鍙樺寲锛
  • varyByRoute: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭鏄惁 闅忕潃璇锋眰鐨勮矾鐢憋紙鎺у埗鍣ㄤ笌鍔ㄤ綔锛夎屽彉鍖栥
  • varyByParam: 杩欎釜鍙傛暟鎸囧畾浜嗕竴涓狦ET鍙傛暟 鐨勫悕瀛楀垪琛ㄥ苟浣跨敤鐩稿簲鐨勫兼潵鍐冲畾缂撳瓨鏁版嵁鐨勭増鏈
  • varyBySession: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭鏄 鍚﹂殢鐫鐢ㄦ埛浼氳瘽鑰屽彉鍖栥
  • varyByExpression: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭 鏄惁闅忕潃鎸囧畾鐨凱HP琛ㄨ揪寮忕殑缁撴灉鑰屽彉鍖栥
瑕佸疄鐜版洿楂樼骇鐨勫彉鍖栵紝鍙互瑕嗙洊getBaseCacheKey()鏂规硶銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
actionPrefix string actions鐨処D鐨勫墠缂銆 褰撳井浠跺湪CController::actions涓0鏄庝簡 鍔ㄤ綔鎻愪緵鑰咃紝鍙互涓哄叾鍔ㄤ綔鐨処D鎸囧畾鍓嶇紑浠ュ尯鍒 浜庡埆鐨勫井浠舵垨鎺у埗鍣ㄣ傚綋寰欢鐢ㄤ簬鎺у埗鍣 鐨勮鍥句腑鏃讹紝蹇呴』閰嶇疆鍚屾牱鐨勫墠缂銆 CWidget
cacheID string 缂撳瓨搴旂敤缁勪欢鐨処D銆傞粯璁ゅ兼槸鈥渃ache鈥濓紙涓昏鐨勭紦瀛樺簲鐢ㄧ粍浠讹級銆 COutputCache
controller CController 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
dependency mixed 缂撳瓨鍐呭渚濊禆鐨勪緷璧栭」銆 杩欎釜鍊煎彲浠ユ槸涓涓疄鐜颁簡ICacheDependency鎺ュ彛鐨勫璞★紝 鎴栦竴涓寚瀹氫簡渚濊禆椤瑰璞$殑閰嶇疆鏁扮粍銆備緥濡傦細
array(
    'class'=>'CDbCacheDependency',
    'sql'=>'SELECT MAX(lastModified) FROM Post',
)
灏嗕娇缂撳瓨杈撳嚭渚濊禆浜庢墍鏈夊笘瀛愮殑鏈鍚庝慨鏀规椂闂淬 濡傛灉浠讳綍涓涓笘瀛愮殑淇敼鏃堕棿鍙戠敓浜嗗彉鍖栵紝缂撳瓨鍐呭灏嗚鎾ら攢銆
COutputCache
duration integer 鏁版嵁鍦ㄧ紦瀛樹腑淇濆瓨鐨勭鏁般傞粯璁ゅ兼槸60绉掋 濡傛灉璁剧疆涓0锛屽凡瀛樺湪鐨勭紦瀛樺唴瀹瑰皢琚粠缂撳瓨涓Щ闄ゃ 濡傛灉璁剧疆涓鸿礋鏁帮紝缂撳瓨灏嗚绂佺敤锛堜换浣曞凡淇濆瓨鍦ㄧ紦瀛樹腑鐨勫唴瀹 灏嗚淇濈暀锛夈

娉ㄦ剰锛氬鏋滅紦瀛樹緷璧栧彂鐢熶簡鏀瑰彉鎴栫紦瀛樼┖闂村埌杈句笂闄愶紝 缂撳瓨涓緝鏃╃殑鏁版嵁灏嗚绉婚櫎銆
COutputCache
id string 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
isContentCached boolean 鍐呭鏄惁宸插湪缂撳瓨涓瓨鍦 COutputCache
isFilter boolean 杩欎釜widget鏄惁浣滀负涓涓繃婊ゅ櫒浣跨敤銆 CFilterWidget
owner CBaseController 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
requestTypes array 璇锋眰绫诲瀷鐨勫垪琛紙渚嬪锛孏ET锛孭OST锛夋寚鏄庝粎浣曠绫诲瀷鐨勭紦瀛樺彲鐢ㄣ 榛樿鍊兼槸null锛岃〃绀烘墍鏈夎姹傜被鍨嬨 COutputCache
skin mixed 寰欢浣跨敤鐨勭毊鑲ょ殑鍚嶇О銆傞粯璁や负鈥渄efault鈥濄 濡傛灉姝ゅ睘鎬ц缃负false锛屽井浠跺皢涓嶄細鏈夌毊鑲よ浣跨敤銆 CWidget
stopAction boolean 褰撹繖涓獁idget鐢ㄤ綔涓涓繃婊ゅ櫒鏃舵槸鍚﹀仠姝㈠姩浣滅殑鎵ц銆 姝ゅ睘鎬у簲褰撲粎鍦CWidget::init鏂规硶涓繘琛屾敼鍙樸 榛樿鍊兼槸false锛岃〃绀哄姩浣滃皢浼氳鎵ц銆 CFilterWidget
varyByExpression string 涓涓狿HP琛ㄨ揪寮忥紝瀹冪殑缁撴灉鐢ㄤ簬缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨勮〃杈惧紡缁撴灉鍙互浣跨敤涓嶅悓鐨勭紦瀛樻暟鎹 鏉ヤ骇鐢熻緭鍑恒 杩欎釜琛ㄨ揪寮忎篃鍙互鏄竴涓湁鏁堢殑PHP鍥炶皟鍑芥暟锛 鍖呮嫭绫诲悕鍜屾柟娉曞悕(array(ClassName/Object, MethodName))锛 鎴栬呭尶鍚嶅嚱鏁(PHP 5. COutputCache
varyByParam array GET鍙傛暟鐨勫垪琛紝瀹冨弬涓庡埌缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨凣ET鍙傛暟鍊肩殑闆嗗悎鍙互浣跨敤涓嶅悓 鐨勭紦瀛樻暟鎹潵浜х敓杈撳嚭銆 COutputCache
varyByRoute boolean 缂撳瓨鍐呭鏄惁鏍规嵁璺敱杩涜鍖哄垎銆 涓涓矾鐢辨槸鐢辫姹傜殑鎺у埗鍣↖D鍜屽姩浣淚D缁勬垚鐨勩 榛樿鍊兼槸true銆 COutputCache
varyBySession boolean 缂撳瓨鍐呭鏄惁鏍规嵁鐢ㄦ埛浼氳瘽杩涜鍖哄垎銆傞粯璁ゅ兼槸false銆 COutputCache
viewPath string 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
baseCacheKey string 璁$畻鍩烘湰缂撳瓨閿笺 COutputCache
cache ICache 鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒 COutputCache
cacheKey string 璁$畻鏈虹紦瀛橀敭鍊笺 COutputCache

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犲櫒銆 CFilterWidget
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
actions() 杩斿洖姝idget浣跨敤鐨勫姩浣滅殑鍒楄〃銆 CWidget
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
filter() 鍦ㄥ姩浣滆杩愯鍓嶆墽琛岃繃婊ゃ COutputCache
getController() 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getId() 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
getIsContentCached() 妫鏌ュ唴瀹规槸鍚﹀凡鍦ㄧ紦瀛樹腑瀛樺湪 COutputCache
getIsFilter() 妫鏌ヨ繖涓獁idget鏄惁浣滀负涓涓繃婊ゅ櫒浣跨敤銆 CFilterWidget
getOwner() 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
getViewFile() 鏍规嵁瑙嗗浘鍚嶆煡鎵捐鍥炬枃浠躲 CWidget
getViewPath() 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鏍囧織鍐呭缂撳瓨鐨勫紑濮嬨 COutputCache
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
recordAction() 褰撹緭鍑虹紦瀛樻湁鏁堟椂璁板綍涓涓柟娉曡皟鐢ㄣ COutputCache
render() 娓叉煋涓涓鍥俱 CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() 鏍囧織鍐呭缂撳瓨鐨勭粨鏉熴 COutputCache
setId() 璁剧疆姝ゅ井浠剁殑ID銆 CWidget
widget() Creates a widget and executes it. CBaseController

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
checkContentCache() 鍦ㄧ紦瀛樹腑鏌ユ壘鍐呭銆 COutputCache
getBaseCacheKey() 璁$畻鍩烘湰缂撳瓨閿笺 COutputCache
getCache() 杩斿洖鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒 COutputCache
getCacheKey() 璁$畻鏈虹紦瀛橀敭鍊笺 COutputCache
replayActions() 閲嶅仛琚褰曠殑鏂规硶璋冪敤鐨勯泦鍚堛 COutputCache

灞炴ц缁

baseCacheKey 灞炴 鍙
protected string getBaseCacheKey()

璁$畻鍩烘湰缂撳瓨閿笺 璁$畻鍑虹殑閿煎湪getCacheKey鍙互杩涗竴姝ュ彉鍔ㄣ 娲剧敓绫诲鏋滈渶瑕佹洿澶氱殑鍙樺寲锛屽彲浠ヨ鐩栨鏂规硶銆

cache 灞炴 鍙
protected ICache getCache()

鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒

cacheID 灞炴
public string $cacheID;

缂撳瓨搴旂敤缁勪欢鐨処D銆傞粯璁ゅ兼槸鈥渃ache鈥濓紙涓昏鐨勭紦瀛樺簲鐢ㄧ粍浠讹級銆

cacheKey 灞炴 鍙
protected string getCacheKey()

璁$畻鏈虹紦瀛橀敭鍊笺 閿煎湪getBaseCacheKey鍏朵粬瑕佺礌鐨勫熀纭涓婅繘琛岃绠楋紝鍖呮嫭 varyByRoute, varyByParamvaryBySession

dependency 灞炴
public mixed $dependency;

缂撳瓨鍐呭渚濊禆鐨勪緷璧栭」銆 杩欎釜鍊煎彲浠ユ槸涓涓疄鐜颁簡ICacheDependency鎺ュ彛鐨勫璞★紝 鎴栦竴涓寚瀹氫簡渚濊禆椤瑰璞$殑閰嶇疆鏁扮粍銆備緥濡傦細

array(
    'class'=>'CDbCacheDependency',
    'sql'=>'SELECT MAX(lastModified) FROM Post',
)
灏嗕娇缂撳瓨杈撳嚭渚濊禆浜庢墍鏈夊笘瀛愮殑鏈鍚庝慨鏀规椂闂淬 濡傛灉浠讳綍涓涓笘瀛愮殑淇敼鏃堕棿鍙戠敓浜嗗彉鍖栵紝缂撳瓨鍐呭灏嗚鎾ら攢銆

duration 灞炴
public integer $duration;

鏁版嵁鍦ㄧ紦瀛樹腑淇濆瓨鐨勭鏁般傞粯璁ゅ兼槸60绉掋 濡傛灉璁剧疆涓0锛屽凡瀛樺湪鐨勭紦瀛樺唴瀹瑰皢琚粠缂撳瓨涓Щ闄ゃ 濡傛灉璁剧疆涓鸿礋鏁帮紝缂撳瓨灏嗚绂佺敤锛堜换浣曞凡淇濆瓨鍦ㄧ紦瀛樹腑鐨勫唴瀹 灏嗚淇濈暀锛夈

娉ㄦ剰锛氬鏋滅紦瀛樹緷璧栧彂鐢熶簡鏀瑰彉鎴栫紦瀛樼┖闂村埌杈句笂闄愶紝 缂撳瓨涓緝鏃╃殑鏁版嵁灏嗚绉婚櫎銆

isContentCached 灞炴 鍙
public boolean getIsContentCached()

鍐呭鏄惁宸插湪缂撳瓨涓瓨鍦

requestTypes 灞炴
public array $requestTypes;

璇锋眰绫诲瀷鐨勫垪琛紙渚嬪锛孏ET锛孭OST锛夋寚鏄庝粎浣曠绫诲瀷鐨勭紦瀛樺彲鐢ㄣ 榛樿鍊兼槸null锛岃〃绀烘墍鏈夎姹傜被鍨嬨

varyByExpression 灞炴
public string $varyByExpression;

涓涓狿HP琛ㄨ揪寮忥紝瀹冪殑缁撴灉鐢ㄤ簬缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨勮〃杈惧紡缁撴灉鍙互浣跨敤涓嶅悓鐨勭紦瀛樻暟鎹 鏉ヤ骇鐢熻緭鍑恒 杩欎釜琛ㄨ揪寮忎篃鍙互鏄竴涓湁鏁堢殑PHP鍥炶皟鍑芥暟锛 鍖呮嫭绫诲悕鍜屾柟娉曞悕(array(ClassName/Object, MethodName))锛 鎴栬呭尶鍚嶅嚱鏁(PHP 5.3.0+)銆傝繖涓嚱鏁/鏂规硶绛惧悕濡備笅锛

function foo($cache) { ... }
杩欏効$cache鎸囧悜缂撳瓨杈撳嚭缁勪欢銆

varyByParam 灞炴
public array $varyByParam;

GET鍙傛暟鐨勫垪琛紝瀹冨弬涓庡埌缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨凣ET鍙傛暟鍊肩殑闆嗗悎鍙互浣跨敤涓嶅悓 鐨勭紦瀛樻暟鎹潵浜х敓杈撳嚭銆

varyByRoute 灞炴
public boolean $varyByRoute;

缂撳瓨鍐呭鏄惁鏍规嵁璺敱杩涜鍖哄垎銆 涓涓矾鐢辨槸鐢辫姹傜殑鎺у埗鍣↖D鍜屽姩浣淚D缁勬垚鐨勩 榛樿鍊兼槸true銆

varyBySession 灞炴
public boolean $varyBySession;

缂撳瓨鍐呭鏄惁鏍规嵁鐢ㄦ埛浼氳瘽杩涜鍖哄垎銆傞粯璁ゅ兼槸false銆

鏂规硶璇︾粏

checkContentCache() 鏂规硶
protected boolean checkContentCache()
{return} boolean 鍐呭鏄惁鍦ㄧ紦瀛樹腑鎵惧埌銆
婧愮爜锛 framework/web/widgets/COutputCache.php#220 (鏄剧ず)
protected function checkContentCache()
{
    if((empty(
$this->requestTypes) || in_array(Yii::app()->getRequest()->getRequestType(),$this->requestTypes))
        && (
$this->_cache=$this->getCache())!==null)
    {
        if(
$this->duration>&& ($data=$this->_cache->get($this->getCacheKey()))!==false)
        {
            
$this->_content=$data[0];
            
$this->_actions=$data[1];
            return 
true;
        }
        if(
$this->duration==0)
            
$this->_cache->delete($this->getCacheKey());
        if(
$this->duration<=0)
            
$this->_cache=null;
    }
    return 
false;
}

鍦ㄧ紦瀛樹腑鏌ユ壘鍐呭銆

filter() 鏂规硶
public boolean filter(CFilterChain $filterChain)
$filterChain CFilterChain 灏嗚搴旂敤鍒板姩浣滅殑杩囨护鍣ㄧ殑鍒楄〃
{return} boolean 鍦ㄨ繖涓繃婊ゅ櫒鎵ц涔嬪悗鏄惁鍋滄杩囨护澶勭悊銆傞粯璁ゅ兼槸false銆
婧愮爜锛 framework/web/widgets/COutputCache.php#149 (鏄剧ず)
public function filter($filterChain)
{
    if(!
$this->getIsContentCached())
        
$filterChain->run();
    
$this->run();
}

鍦ㄥ姩浣滆杩愯鍓嶆墽琛岃繃婊ゃ 濡傛灉闇瑕佸墠缃繃婊わ紝閭d箞娲剧敓绫诲簲褰撹鐩栨鏂规硶銆

getBaseCacheKey() 鏂规硶
protected string getBaseCacheKey()
{return} string 娌℃湁鍙樺寲鐨勫熀鏈紦瀛橀敭鍊笺
婧愮爜锛 framework/web/widgets/COutputCache.php#253 (鏄剧ず)
protected function getBaseCacheKey()
{
    return 
self::CACHE_KEY_PREFIX.$this->getId().'.';
}

璁$畻鍩烘湰缂撳瓨閿笺 璁$畻鍑虹殑閿煎湪getCacheKey鍙互杩涗竴姝ュ彉鍔ㄣ 娲剧敓绫诲鏋滈渶瑕佹洿澶氱殑鍙樺寲锛屽彲浠ヨ鐩栨鏂规硶銆

getCache() 鏂规硶
protected ICache getCache()
{return} ICache 鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒
婧愮爜锛 framework/web/widgets/COutputCache.php#242 (鏄剧ず)
protected function getCache()
{
    return 
Yii::app()->getComponent($this->cacheID);
}

getCacheKey() 鏂规硶
protected string getCacheKey()
{return} string 缂撳瓨閿
婧愮爜锛 framework/web/widgets/COutputCache.php#264 (鏄剧ず)
protected function getCacheKey()
{
    if(
$this->_key!==null)
        return 
$this->_key;
    else
    {
        
$key=$this->getBaseCacheKey().'.';
        if(
$this->varyByRoute)
        {
            
$controller=$this->getController();
            
$key.=$controller->getUniqueId().'/';
            if((
$action=$controller->getAction())!==null)
                
$key.=$action->getId();
        }
        
$key.='.';

        if(
$this->varyBySession)
            
$key.=Yii::app()->getSession()->getSessionID();
        
$key.='.';

        if(
is_array($this->varyByParam) && isset($this->varyByParam[0]))
        {
            
$params=array();
            foreach(
$this->varyByParam as $name)
            {
                if(isset(
$_GET[$name]))
                    
$params[$name]=$_GET[$name];
                else
                    
$params[$name]='';
            }
            
$key.=serialize($params);
        }
        
$key.='.';

        if(
$this->varyByExpression!==null)
            
$key.=$this->evaluateExpression($this->varyByExpression);
        
$key.='.';

        return 
$this->_key=$key;
    }
}

璁$畻鏈虹紦瀛橀敭鍊笺 閿煎湪getBaseCacheKey鍏朵粬瑕佺礌鐨勫熀纭涓婅繘琛岃绠楋紝鍖呮嫭 varyByRoute, varyByParamvaryBySession

getIsContentCached() 鏂规硶
public boolean getIsContentCached()
{return} boolean 鍐呭鏄惁宸插湪缂撳瓨涓瓨鍦
婧愮爜锛 framework/web/widgets/COutputCache.php#208 (鏄剧ず)
public function getIsContentCached()
{
    if(
$this->_contentCached!==null)
        return 
$this->_contentCached;
    else
        return 
$this->_contentCached=$this->checkContentCache();
}

init() 鏂规硶
public void init()
婧愮爜锛 framework/web/widgets/COutputCache.php#162 (鏄剧ず)
public function init()
{
    if(
$this->getIsContentCached())
        
$this->replayActions();
    else if(
$this->_cache!==null)
    {
        
$this->getController()->getCachingStack()->push($this);
        
ob_start();
        
ob_implicit_flush(false);
    }
}

鏍囧織鍐呭缂撳瓨鐨勫紑濮嬨 鏄剧ず鍦ㄦ鏂规硶涔嬪悗锛endCache()鏂规硶涔嬪墠鐨勫唴瀹 灏嗚鑾峰彇骞朵繚瀛樺湪缂撳瓨涓 濡傛灉鍙敤鐨勫唴瀹瑰湪缂撳瓨涓凡瀛樺湪鍒欐鏂规硶浠涔堜篃涓嶄細鍋氥

recordAction() 鏂规硶
public void recordAction(string $context, string $method, array $params)
$context string 鎺у埗鍣ㄧ殑涓涓睘鎬у悕瀛椼傛灞炴ф寚鍚戜竴涓璞★紝 瀹冪殑鏂规硶鍙互琚褰曘傚鏋滀负绌鸿〃绀烘鎺у埗鍣ㄦ湰韬
$method string 鏂规硶鐨勫悕瀛
$params array 浼犻掔粰鏂规硶鐨勫弬鏁
婧愮爜锛 framework/web/widgets/COutputCache.php#315 (鏄剧ず)
public function recordAction($context,$method,$params)
{
    
$this->_actions[]=array($context,$method,$params);
}

褰撹緭鍑虹紦瀛樻湁鏁堟椂璁板綍涓涓柟娉曡皟鐢ㄣ 褰撳唴瀹规槸鐢辫緭鍑虹紦瀛樻彁渚涙椂锛岃褰曟柟娉曞皢琚 閲嶈皟鐢ㄣ

replayActions() 鏂规硶
protected void replayActions()
婧愮爜锛 framework/web/widgets/COutputCache.php#323 (鏄剧ず)
protected function replayActions()
{
    if(empty(
$this->_actions))
        return;
    
$controller=$this->getController();
    
$cs=Yii::app()->getClientScript();
    foreach(
$this->_actions as $action)
    {
        if(
$action[0]==='clientScript')
            
$object=$cs;
        else if(
$action[0]==='')
            
$object=$controller;
        else
            
$object=$controller->{$action[0]};
        if(
method_exists($object,$action[1]))
            
call_user_func_array(array($object,$action[1]),$action[2]);
        else if(
$action[0]==='' && function_exists($action[1]))
            
call_user_func_array($action[1],$action[2]);
        else
            throw new 
CException(Yii::t('yii','Unable to replay the action "{object}.{method}". The method does not exist.',
                array(
'object'=>$action[0],
                    
'method'=>$action[1])));
    }
}

閲嶅仛琚褰曠殑鏂规硶璋冪敤鐨勯泦鍚堛

run() 鏂规硶
public void run()
婧愮爜锛 framework/web/widgets/COutputCache.php#180 (鏄剧ず)
public function run()
{
    if(
$this->getIsContentCached())
    {
        if(
$this->getController()->isCachingStackEmpty())
            echo 
$this->getController()->processDynamicOutput($this->_content);
        else
            echo 
$this->_content;
    }
    else if(
$this->_cache!==null)
    {
        
$this->_content=ob_get_clean();
        
$this->getController()->getCachingStack()->pop();
        
$data=array($this->_content,$this->_actions);
        if(
is_array($this->dependency))
            
$this->dependency=Yii::createComponent($this->dependency);
        
$this->_cache->set($this->getCacheKey(),$data,$this->duration,$this->dependency);

        if(
$this->getController()->isCachingStackEmpty())
            echo 
$this->getController()->processDynamicOutput($this->_content);
        else
            echo 
$this->_content;
    }
}

鏍囧織鍐呭缂撳瓨鐨勭粨鏉熴 鏄剧ず鍦ㄦ鏂规硶涔嬪墠锛init()鏂规硶涔嬪悗鐨勫唴瀹 灏嗚鑾峰彇骞朵繚瀛樺湪缂撳瓨涓 濡傛灉鍙敤鐨勫唴瀹瑰湪缂撳瓨涓凡瀛樺湪鍒欐鏂规硶浠涔堜篃涓嶄細鍋氥

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