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 |
濡傛灉闇瑕佹樉绀虹殑杈撳嚭鍦ㄧ紦瀛樹腑鍙敤锛岀紦瀛樹腑鐨勭増鏈皢 浠f浛鐪熷疄鐨勭増鏈鏄剧ず锛岄氳繃杩欑鏂瑰紡鍙互鑺傜渷鐢熸垚 鍘熷杈撳嚭鐨勬椂闂淬
鍥犱负COutputCache鏄粠CFilterWidget鎵╁睍鑰屾潵锛屾墍浠ュ畠鍙互 鐢ㄤ綔杩囨护鍣紙涓哄姩浣滅紦瀛橈級鎴栧皬鐗╀欢锛堜负鐗囨缂撳瓨锛夈 鍦ㄧ浜岀鐢ㄦ硶涓紝閫氬父浣跨敤蹇嵎鏂瑰紡CBaseController::beginCache()鍜 CBaseController::endCache()鏉ヤ唬鏇匡紝渚嬪涓嬮潰鐨勮鍥炬枃浠讹細
if($this->beginCache('cacheName',array('property1'=>'value1',...)) { // ... 鍦ㄦ澶勬樉绀虹紦瀛樼殑鍐呭 $this->endCache(); }
COutputCache蹇呴』涓庝竴涓氳繃cacheID鎸囧畾鐨勭紦瀛樺簲鐢ㄧ粍浠跺叡鍚屽伐浣溿 濡傛灉璇ョ紦瀛樺簲鐢ㄧ粍浠朵笉鍙敤锛孋OutputCache涔熷皢琚鐢ㄣ
鏈変袱涓洜绱犲喅瀹氫竴涓紦瀛樼殑鍐呭鏄惁鏈夋晥锛 duration 浠ュ強缂撳瓨鐨 dependency銆 鍓嶈呮寚瀹氫簡鏁版嵁鍙互鍦ㄧ紦瀛樹腑缁存寔鏈夋晥鐨勭鏁帮紙榛樿鍊兼槸 60绉掞級锛岃屽悗鑰呮寚瀹氫簡缂撳瓨鏁版嵁鐨勪緷璧栭」銆傚鏋滀竴涓 渚濊禆椤瑰彂鐢熸敼鍙橈紝锛堜緥濡傦紝鏁版嵁搴撲腑鐨勭浉鍏虫暟鎹鏇存柊锛夛紝 缂撳瓨涓殑鏁版嵁灏嗕笉鍐嶆湁鏁堛 鍙傝CCacheDependency浠ヤ簡瑙f洿澶氬叧浜庣紦瀛樹緷璧栫殑缁嗚妭銆
鏈夋椂鍊欙紝涓轰簡鎵ц涓涓‘瀹氱殑璇锋眰绫诲瀷锛屾湁蹇呰鍏抽棴杈撳嚭缂撳瓨銆 渚嬪锛屾垜浠粎甯屾湜涓涓〃鍗曞湪鍒濇琚姹傜殑鏃跺欒缂撳瓨锛岄殢鍚庡 姝よ〃鍗曠殑鏄剧ず灏嗕笉甯屾湜浣跨敤缂撳瓨锛屽洜涓哄畠鍖呭惈浜嗙敤鎴风殑杈撳叆銆 鎴戜滑鍙互璁剧疆requestTypes涓
array('GET')
浠ヨ揪鍒拌繖涓鐩殑銆
閫氳繃涓浜涘弬鏁扮殑璁剧疆锛屽彲浠ヤ粠缂撳瓨涓緱鍒颁笉鍚岀殑鍐呭銆 COutputCache鏀寔鍥涚鍙樺寲锛
- varyByRoute: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭鏄惁 闅忕潃璇锋眰鐨勮矾鐢憋紙鎺у埗鍣ㄤ笌鍔ㄤ綔锛夎屽彉鍖栥
- varyByParam: 杩欎釜鍙傛暟鎸囧畾浜嗕竴涓狦ET鍙傛暟 鐨勫悕瀛楀垪琛ㄥ苟浣跨敤鐩稿簲鐨勫兼潵鍐冲畾缂撳瓨鏁版嵁鐨勭増鏈
- varyBySession: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭鏄 鍚﹂殢鐫鐢ㄦ埛浼氳瘽鑰屽彉鍖栥
- varyByExpression: 杩欎釜鍙傛暟鎸囧畾缂撳瓨鍐呭 鏄惁闅忕潃鎸囧畾鐨凱HP琛ㄨ揪寮忕殑缁撴灉鑰屽彉鍖栥
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
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 |
灞炴ц缁
璁$畻鍩烘湰缂撳瓨閿笺 璁$畻鍑虹殑閿煎湪getCacheKey鍙互杩涗竴姝ュ彉鍔ㄣ 娲剧敓绫诲鏋滈渶瑕佹洿澶氱殑鍙樺寲锛屽彲浠ヨ鐩栨鏂规硶銆
鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒
缂撳瓨搴旂敤缁勪欢鐨処D銆傞粯璁ゅ兼槸鈥渃ache鈥濓紙涓昏鐨勭紦瀛樺簲鐢ㄧ粍浠讹級銆
璁$畻鏈虹紦瀛橀敭鍊笺 閿煎湪getBaseCacheKey鍏朵粬瑕佺礌鐨勫熀纭涓婅繘琛岃绠楋紝鍖呮嫭 varyByRoute, varyByParam 鍜 varyBySession銆
缂撳瓨鍐呭渚濊禆鐨勪緷璧栭」銆 杩欎釜鍊煎彲浠ユ槸涓涓疄鐜颁簡ICacheDependency鎺ュ彛鐨勫璞★紝 鎴栦竴涓寚瀹氫簡渚濊禆椤瑰璞$殑閰嶇疆鏁扮粍銆備緥濡傦細
array( 'class'=>'CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post', )灏嗕娇缂撳瓨杈撳嚭渚濊禆浜庢墍鏈夊笘瀛愮殑鏈鍚庝慨鏀规椂闂淬 濡傛灉浠讳綍涓涓笘瀛愮殑淇敼鏃堕棿鍙戠敓浜嗗彉鍖栵紝缂撳瓨鍐呭灏嗚鎾ら攢銆
鏁版嵁鍦ㄧ紦瀛樹腑淇濆瓨鐨勭鏁般傞粯璁ゅ兼槸60绉掋
濡傛灉璁剧疆涓0锛屽凡瀛樺湪鐨勭紦瀛樺唴瀹瑰皢琚粠缂撳瓨涓Щ闄ゃ
濡傛灉璁剧疆涓鸿礋鏁帮紝缂撳瓨灏嗚绂佺敤锛堜换浣曞凡淇濆瓨鍦ㄧ紦瀛樹腑鐨勫唴瀹
灏嗚淇濈暀锛夈
娉ㄦ剰锛氬鏋滅紦瀛樹緷璧栧彂鐢熶簡鏀瑰彉鎴栫紦瀛樼┖闂村埌杈句笂闄愶紝
缂撳瓨涓緝鏃╃殑鏁版嵁灏嗚绉婚櫎銆
鍐呭鏄惁宸插湪缂撳瓨涓瓨鍦
璇锋眰绫诲瀷鐨勫垪琛紙渚嬪锛孏ET锛孭OST锛夋寚鏄庝粎浣曠绫诲瀷鐨勭紦瀛樺彲鐢ㄣ 榛樿鍊兼槸null锛岃〃绀烘墍鏈夎姹傜被鍨嬨
涓涓狿HP琛ㄨ揪寮忥紝瀹冪殑缁撴灉鐢ㄤ簬缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨勮〃杈惧紡缁撴灉鍙互浣跨敤涓嶅悓鐨勭紦瀛樻暟鎹 鏉ヤ骇鐢熻緭鍑恒 杩欎釜琛ㄨ揪寮忎篃鍙互鏄竴涓湁鏁堢殑PHP鍥炶皟鍑芥暟锛 鍖呮嫭绫诲悕鍜屾柟娉曞悕(array(ClassName/Object, MethodName))锛 鎴栬呭尶鍚嶅嚱鏁(PHP 5.3.0+)銆傝繖涓嚱鏁/鏂规硶绛惧悕濡備笅锛
function foo($cache) { ... }杩欏効$cache鎸囧悜缂撳瓨杈撳嚭缁勪欢銆
GET鍙傛暟鐨勫垪琛紝瀹冨弬涓庡埌缂撳瓨閿肩殑璁$畻涓 閫氳繃璁剧疆姝ゅ睘鎬э紝姣忎釜涓嶅悓鐨凣ET鍙傛暟鍊肩殑闆嗗悎鍙互浣跨敤涓嶅悓 鐨勭紦瀛樻暟鎹潵浜х敓杈撳嚭銆
缂撳瓨鍐呭鏄惁鏍规嵁璺敱杩涜鍖哄垎銆 涓涓矾鐢辨槸鐢辫姹傜殑鎺у埗鍣↖D鍜屽姩浣淚D缁勬垚鐨勩 榛樿鍊兼槸true銆
缂撳瓨鍐呭鏄惁鏍规嵁鐢ㄦ埛浼氳瘽杩涜鍖哄垎銆傞粯璁ゅ兼槸false銆
鏂规硶璇︾粏
protected boolean checkContentCache()
| ||
{return} | boolean | 鍐呭鏄惁鍦ㄧ紦瀛樹腑鎵惧埌銆 |
protected function checkContentCache()
{
if((empty($this->requestTypes) || in_array(Yii::app()->getRequest()->getRequestType(),$this->requestTypes))
&& ($this->_cache=$this->getCache())!==null)
{
if($this->duration>0 && ($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;
}
鍦ㄧ紦瀛樹腑鏌ユ壘鍐呭銆
public boolean filter(CFilterChain $filterChain)
| ||
$filterChain | CFilterChain | 灏嗚搴旂敤鍒板姩浣滅殑杩囨护鍣ㄧ殑鍒楄〃 |
{return} | boolean | 鍦ㄨ繖涓繃婊ゅ櫒鎵ц涔嬪悗鏄惁鍋滄杩囨护澶勭悊銆傞粯璁ゅ兼槸false銆 |
public function filter($filterChain)
{
if(!$this->getIsContentCached())
$filterChain->run();
$this->run();
}
鍦ㄥ姩浣滆杩愯鍓嶆墽琛岃繃婊ゃ 濡傛灉闇瑕佸墠缃繃婊わ紝閭d箞娲剧敓绫诲簲褰撹鐩栨鏂规硶銆
protected string getBaseCacheKey()
| ||
{return} | string | 娌℃湁鍙樺寲鐨勫熀鏈紦瀛橀敭鍊笺 |
protected function getBaseCacheKey()
{
return self::CACHE_KEY_PREFIX.$this->getId().'.';
}
璁$畻鍩烘湰缂撳瓨閿笺 璁$畻鍑虹殑閿煎湪getCacheKey鍙互杩涗竴姝ュ彉鍔ㄣ 娲剧敓绫诲鏋滈渶瑕佹洿澶氱殑鍙樺寲锛屽彲浠ヨ鐩栨鏂规硶銆
protected ICache getCache()
| ||
{return} | ICache | 鐢ㄤ簬缂撳瓨鍐呭鐨勭紦瀛樺櫒 |
protected function getCache()
{
return Yii::app()->getComponent($this->cacheID);
}
protected string getCacheKey()
| ||
{return} | string | 缂撳瓨閿 |
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, varyByParam 鍜 varyBySession銆
public boolean getIsContentCached()
| ||
{return} | boolean | 鍐呭鏄惁宸插湪缂撳瓨涓瓨鍦 |
public function getIsContentCached()
{
if($this->_contentCached!==null)
return $this->_contentCached;
else
return $this->_contentCached=$this->checkContentCache();
}
public void init()
|
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()鏂规硶涔嬪墠鐨勫唴瀹 灏嗚鑾峰彇骞朵繚瀛樺湪缂撳瓨涓 濡傛灉鍙敤鐨勫唴瀹瑰湪缂撳瓨涓凡瀛樺湪鍒欐鏂规硶浠涔堜篃涓嶄細鍋氥
public void recordAction(string $context, string $method, array $params)
| ||
$context | string | 鎺у埗鍣ㄧ殑涓涓睘鎬у悕瀛椼傛灞炴ф寚鍚戜竴涓璞★紝 瀹冪殑鏂规硶鍙互琚褰曘傚鏋滀负绌鸿〃绀烘鎺у埗鍣ㄦ湰韬 |
$method | string | 鏂规硶鐨勫悕瀛 |
$params | array | 浼犻掔粰鏂规硶鐨勫弬鏁 |
public function recordAction($context,$method,$params)
{
$this->_actions[]=array($context,$method,$params);
}
褰撹緭鍑虹紦瀛樻湁鏁堟椂璁板綍涓涓柟娉曡皟鐢ㄣ 褰撳唴瀹规槸鐢辫緭鍑虹紦瀛樻彁渚涙椂锛岃褰曟柟娉曞皢琚 閲嶈皟鐢ㄣ
protected void replayActions()
|
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])));
}
}
閲嶅仛琚褰曠殑鏂规硶璋冪敤鐨勯泦鍚堛
public void run()
|
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()鏂规硶涔嬪悗鐨勫唴瀹 灏嗚鑾峰彇骞朵繚瀛樺湪缂撳瓨涓 濡傛灉鍙敤鐨勫唴瀹瑰湪缂撳瓨涓凡瀛樺湪鍒欐鏂规硶浠涔堜篃涓嶄細鍋氥