《PHP實(shí)例:YII Framework框架教程之緩存用法詳解》要點(diǎn):
本文介紹了PHP實(shí)例:YII Framework框架教程之緩存用法詳解,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:YII框架
本文實(shí)例講述了YII Framework框架緩存用法.分享給大家供大家參考,具體如下:PHP實(shí)例
緩存的產(chǎn)生原因眾所周知.于是YII作為一個(gè)高效,好用的框架,不能不支持緩存.所以YII對(duì)各種流行的緩存都提供了接口,你可以根據(jù)你的需要使用不同的緩存.PHP實(shí)例
1.YII中的緩存介紹PHP實(shí)例
YII中的緩存是通過(guò)組件方式定義的,具體在如下目錄PHP實(shí)例
/yii_dev/yii/framework/caching# tree
.
├── CApcCache.php
├── CCache.php
├── CDbCache.php
├── CDummyCache.php
├── CEAcceleratorCache.php
├── CFileCache.php
├── CMemCache.php
├── CWinCache.php
├── CXCache.php
├── CZendDataCache.php
└── dependencies
??? ├── CCacheDependency.php
??? ├── CChainedCacheDependency.php
??? ├── CDbCacheDependency.php
??? ├── CDirectoryCacheDependency.php
??? ├── CExpressionDependency.php
??? ├── CFileCacheDependency.php
??? └── CGlobalStateCacheDependency.phpPHP實(shí)例
1 directory, 17 filesPHP實(shí)例
官方原文解釋如下:PHP實(shí)例
Yii 提供了不同的緩存組件,可以將緩存數(shù)據(jù)存儲(chǔ)到不同的媒介中.例如, CMemCache 組件封裝了 PHP 的 memcache 擴(kuò)展并使用內(nèi)存作為緩存存儲(chǔ)媒介. CApcCache 組件封裝了 PHP APC 擴(kuò)展; 而 CDbCache 組件會(huì)將緩存的數(shù)據(jù)存入數(shù)據(jù)庫(kù).下面是一個(gè)可用緩存組件的列表:PHP實(shí)例
CMemCache: 使用 PHP memcache 擴(kuò)展.PHP實(shí)例
CApcCache: 使用 PHP APC 擴(kuò)展.PHP實(shí)例
CXCache: 使用 PHP XCache 擴(kuò)展.注意,這個(gè)是從 1.0.1 版本開(kāi)始支持的.PHP實(shí)例
CEAcceleratorCache: 使用 PHP EAccelerator 擴(kuò)展.PHP實(shí)例
CDbCache: 使用一個(gè)數(shù)據(jù)表存儲(chǔ)緩存數(shù)據(jù).默認(rèn)情況下,它將創(chuàng)建并使用在 runtime 目錄下的一個(gè) SQLite3 數(shù)據(jù)庫(kù). 你也可以通過(guò)設(shè)置其 connectionID 屬性指定一個(gè)給它使用的數(shù)據(jù)庫(kù).PHP實(shí)例
CZendDataCache: 使用 Zend Data Cache 作為后臺(tái)緩存媒介.注意,這個(gè)是從 1.0.4 版本開(kāi)始支持的.PHP實(shí)例
CFileCache: 使用文件存儲(chǔ)緩存數(shù)據(jù).這個(gè)特別適合用于存儲(chǔ)大塊數(shù)據(jù)(例如頁(yè)面).注意,這個(gè)是從 1.0.6 版本開(kāi)始支持的.PHP實(shí)例
CDummyCache: 目前 dummy 緩存并不實(shí)現(xiàn)緩存功能.此組件的目的是用于簡(jiǎn)化那些需要檢查緩存可用性的代碼. 例如,在開(kāi)發(fā)階段或者服務(wù)器尚未支持實(shí)際的緩存功能,我們可以使用此緩存組件.當(dāng)啟用了實(shí)際的緩存支持后,我們可以切換到使用相應(yīng)的緩存組件. 在這兩種情況中,我們可以使用同樣的代碼Yii::app()->cache->get($key) 獲取數(shù)據(jù)片段而不需要擔(dān)心 Yii::app()->cache 可能會(huì)是 null.此組件從 1.0.5 版開(kāi)始支持.PHP實(shí)例
提示: 由于所有的這些緩存組件均繼承自同樣的基類 CCache,因此無(wú)需改變使用緩存的那些代碼就可以切換到使用另一種緩存方式.PHP實(shí)例
緩存可以用于不同的級(jí)別.最低級(jí)別中,我們使用緩存存儲(chǔ)單個(gè)數(shù)據(jù)片段,例如變量,我們將此稱為 數(shù)據(jù)緩存(data caching).下一個(gè)級(jí)別中,我們?cè)诰彺嬷写鎯?chǔ)一個(gè)由視圖腳本的一部分生成的頁(yè)面片段. 而在最高級(jí)別中,我們將整個(gè)頁(yè)面存儲(chǔ)在緩存中并在需要時(shí)取回.PHP實(shí)例
在接下來(lái)的幾個(gè)小節(jié)中,我們會(huì)詳細(xì)講解如安在這些級(jí)別中使用緩存.PHP實(shí)例
注意: 依照定義,緩存是一個(gè)不穩(wěn)定的存儲(chǔ)媒介.即使沒(méi)有超時(shí),它也并不確保緩存數(shù)據(jù)一定存在. 因此,不要將緩存作為持久存儲(chǔ)器使用.(例如,不要使用緩存存儲(chǔ) Session 數(shù)據(jù)).PHP實(shí)例
2.緩存的配置和調(diào)用方式PHP實(shí)例
yii中的緩存主要是通過(guò)組件的方式實(shí)現(xiàn)的,具體需要配置方式可以通過(guò)緩存的類說(shuō)明進(jìn)行配置.PHP實(shí)例
通常是指定緩存組件的類PHP實(shí)例
例如apcPHP實(shí)例
'cache'=>array( 'class'=>'system.caching.CApcCache' ),
memcache的配置方式可能是PHP實(shí)例
* array( * 'components'=>array( * 'cache'=>array( * 'class'=>'CMemCache', * 'servers'=>array( * array( * 'host'=>'server1', * 'port'=>11211, * 'weight'=>60, * ), * array( * 'host'=>'server2', * 'port'=>11211, * 'weight'=>40, * ), * ), * ), * ), * )
使用方式:PHP實(shí)例
yii封裝了對(duì)不同緩存操作的辦法,主要集中在CCache.CCache是所有Cache類的基類.所以配置好緩存后可以調(diào)用方式很簡(jiǎn)單:PHP實(shí)例
<?php /** * CCache is the base class for cache classes with different cache storage implementation. * * A data item can be stored in cache by calling {@link set} and be retrieved back * later by {@link get}. In both operations, a key identifying the data item is required. * An expiration time and/or a dependency can also be specified when calling {@link set}. * If the data item expires or the dependency changes, calling {@link get} will not * return back the data item. * * Note, by definition, cache does not ensure the existence of a value * even if it does not expire. Cache is not meant to be a persistent storage. * * CCache implements the interface {@link ICache} with the following methods: * <ul> * <li>{@link get} : retrieve the value with a key (if any) from cache</li> * <li>{@link set} : store the value with a key into cache</li> * <li>{@link add} : store the value only if cache does not have this key</li> * <li>{@link delete} : delete the value with the specified key from cache</li> * <li>{@link flush} : delete all values from cache</li> * </ul> * * Child classes must implement the following methods: * <ul> * <li>{@link getValue}</li> * <li>{@link setValue}</li> * <li>{@link addValue}</li> * <li>{@link deleteValue}</li> * <li>{@link flush} (optional)</li> * </ul> * * CCache also implements ArrayAccess so that it can be used like an array. * * @author Qiang Xue <qiang.xue@gmail.com> * @version $Id: CCache.php 3001 2011-02-24 16:42:44Z alexander.makarow $ * @package system.caching * @since 1.0 */ abstract class CCache extends CApplicationComponent implements ICache, ArrayAccess {
根據(jù)CCache類說(shuō)明可以看出,常見(jiàn)的緩存操作辦法get,set,add,delete,flushPHP實(shí)例
/** * Retrieves a value from cache with a specified key. * @param string $id a key identifying the cached value * @return mixed the value stored in cache, false if the value is not in the cache, expired or the dependency has changed. */ public function get($id) { if(($value=$this->getValue($this->generateUniqueKey($id)))!==false) { $data=unserialize($value); if(!is_array($data)) return false; if(!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged()) { Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this)); return $data[0]; } } return false; } /** * Retrieves multiple values from cache with the specified keys. * Some caches (such as memcache, apc) allow retrieving multiple cached values at one time, * which may improve the performance since it reduces the communication cost. * In case a cache doesn't support this feature natively, it will be simulated by this method. * @param array $ids list of keys identifying the cached values * @return array list of cached values corresponding to the specified keys. The array * is returned in terms of (key,value) pairs. * If a value is not cached or expired, the corresponding array value will be false. * @since 1.0.8 */ public function mget($ids) { $uniqueIDs=array(); $results=array(); foreach($ids as $id) { $uniqueIDs[$id]=$this->generateUniqueKey($id); $results[$id]=false; } $values=$this->getValues($uniqueIDs); foreach($uniqueIDs as $id=>$uniqueID) { if(!isset($values[$uniqueID])) continue; $data=unserialize($values[$uniqueID]); if(is_array($data) && (!($data[1] instanceof ICacheDependency) || !$data[1]->getHasChanged())) { Yii::trace('Serving "'.$id.'" from cache','system.caching.'.get_class($this)); $results[$id]=$data[0]; } } return $results; } /** * Stores a value identified by a key into cache. * If the cache already contains such a key, the existing value and * expiration time will be replaced with the new ones. * * @param string $id the key identifying the value to be cached * @param mixed $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid. * @return boolean true if the value is successfully stored into cache, false otherwise */ public function set($id,$value,$expire=0,$dependency=null) { Yii::trace('Saving "'.$id.'" to cache','system.caching.'.get_class($this)); if($dependency!==null) $dependency->evaluateDependency(); $data=array($value,$dependency); return $this->setValue($this->generateUniqueKey($id),serialize($data),$expire); } /** * Stores a value identified by a key into cache if the cache does not contain this key. * Nothing will be done if the cache already contains the key. * @param string $id the key identifying the value to be cached * @param mixed $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid. * @return boolean true if the value is successfully stored into cache, false otherwise */ public function add($id,$value,$expire=0,$dependency=null) { Yii::trace('Adding "'.$id.'" to cache','system.caching.'.get_class($this)); if($dependency!==null) $dependency->evaluateDependency(); $data=array($value,$dependency); return $this->addValue($this->generateUniqueKey($id),serialize($data),$expire); } /** * Deletes a value with the specified key from cache * @param string $id the key of the value to be deleted * @return boolean if no error happens during deletion */ public function delete($id) { Yii::trace('Deleting "'.$id.'" from cache','system.caching.'.get_class($this)); return $this->deleteValue($this->generateUniqueKey($id)); } /** * Deletes all values from cache. * Be careful of performing this operation if the cache is shared by multiple applications. * @return boolean whether the flush operation was successful. */ public function flush() { Yii::trace('Flushing cache','system.caching.'.get_class($this)); return $this->flushValues(); }
即:PHP實(shí)例
Yii::app()->cache->xxx
xxx對(duì)應(yīng)具體的辦法.PHP實(shí)例
例如:PHP實(shí)例
$id = 'key1'; $value = 'cache value'; Yii::app()->cache->add($id, $value); var_dump(Yii::app()->cache->get($id));
下面是yii官方給出的幾種緩存方式的使用說(shuō)明,這里就麻木不仁,照搬了PHP實(shí)例
3.緩存的使用:數(shù)據(jù)緩存PHP實(shí)例
數(shù)據(jù)緩存PHP實(shí)例
數(shù)據(jù)緩存即存儲(chǔ)一些 PHP 變量到緩存中,以后再?gòu)木彺嬷腥〕鰜?lái).出于此目的,緩存組件的基類 CCache 提供了兩個(gè)最常用的辦法: set() 和 get().PHP實(shí)例
要在緩存中存儲(chǔ)一個(gè)變量 $value ,我們選擇一個(gè)唯一 ID 并調(diào)用 set() 存儲(chǔ)它:PHP實(shí)例
Yii::app()->cache->set($id, $value);
緩存的數(shù)據(jù)將一直留在緩存中,除非它由于某些緩存策略(例如緩存空間已滿,舊的數(shù)據(jù)被刪除)而被清除. 要改變這種行為,我們可以在調(diào)用 set() 的同時(shí)提供一個(gè)過(guò)期參數(shù),這樣在設(shè)定的時(shí)間段之后,緩存數(shù)據(jù)將被清除:PHP實(shí)例
// 值$value 在緩存中最多保留30秒 Yii::app()->cache->set($id, $value, 30);
稍后當(dāng)我們需要訪問(wèn)此變量時(shí)(在同一個(gè)或不同的 Web 哀求中),就可以通過(guò) ID 調(diào)用 get() 從緩存中將其取回. 如果返回的是 false,表示此值在緩存中不可用,我們應(yīng)該重新生成它.PHP實(shí)例
$value=Yii::app()->cache->get($id); if($value===false) { // 因?yàn)樵诰彺嬷袥](méi)找到 $value ,重新生成它 , // 并將它存入緩存以備以后使用: // Yii::app()->cache->set($id,$value); }
為要存入緩存的變量選擇 ID 時(shí),要確保此 ID 對(duì)應(yīng)用中所有其他存入緩存的變量是唯一的. 而在不同的應(yīng)用之間,這個(gè) ID 不需要是唯一的.緩存組件具有足夠的智慧區(qū)分不同應(yīng)用中的 ID.PHP實(shí)例
一些緩存存儲(chǔ)器,例如 MemCache, APC, 支持以批量模式獲取多個(gè)緩存值.這可以減少獲取緩存數(shù)據(jù)時(shí)帶來(lái)的開(kāi)銷. 從版本 1.0.8 起,Yii 提供了一個(gè)新的名為 mget() 的辦法.它可以利用此功能.如果底層緩存存儲(chǔ)器不支持此功能,mget() 依然可以模擬實(shí)現(xiàn)它.PHP實(shí)例
要從緩存中清除一個(gè)緩存值,調(diào)用 delete(); 要清楚緩存中的所有數(shù)據(jù),調(diào)用 flush(). 當(dāng)調(diào)用 flush() 時(shí)一定要小心,因?yàn)樗鼤?huì)同時(shí)清除其他應(yīng)用中的緩存.PHP實(shí)例
提示: 由于 CCache 實(shí)現(xiàn)了 ArrayAccess,緩存組件也可以像一個(gè)數(shù)組一樣使用.下面是幾個(gè)例子:PHP實(shí)例
$cache=Yii::app()->cache; $cache['var1']=$value1; // 相當(dāng)于: $cache->set('var1',$value1); $value2=$cache['var2']; // 相當(dāng)于: $value2=$cache->get('var2');
1. 緩存依賴PHP實(shí)例
除了過(guò)期設(shè)置,緩存數(shù)據(jù)也可能會(huì)因?yàn)橐蕾嚄l件發(fā)生變化而失效.例如,如果我們緩存了某些文件的內(nèi)容,而這些文件發(fā)生了改變,我們就應(yīng)該讓緩存的數(shù)據(jù)失效, 并從文件中讀取最新內(nèi)容而不是從緩存中讀取.PHP實(shí)例
我們將一個(gè)依賴關(guān)系表現(xiàn)為一個(gè) CCacheDependency 或其子類的實(shí)例. 當(dāng)調(diào)用 set() 時(shí),我們連同要緩存的數(shù)據(jù)將其一同傳入.PHP實(shí)例
// 此值將在30秒后失效 // 也可能因依賴的文件發(fā)生了變化而更快失效 Yii::app()->cache->set($id, $value, 30, new CFileCacheDependency('FileName'));
現(xiàn)在如果我們通過(guò)調(diào)用get() 從緩存中獲取 $value ,依賴關(guān)系將被檢查,如果發(fā)生改變,我們將會(huì)得到一個(gè) false 值,表示數(shù)據(jù)需要被重新生成.PHP實(shí)例
如下是可用的緩存依賴的簡(jiǎn)要說(shuō)明:PHP實(shí)例
CFileCacheDependency: 如果文件的最后修改時(shí)間發(fā)生改變,則依賴改變.
CDirectoryCacheDependency: 如果目錄和其子目錄中的文件發(fā)生改變,則依賴改變.
CDbCacheDependency: 如果指定 SQL 語(yǔ)句的查詢結(jié)果發(fā)生改變,則依賴改變.
CGlobalStateCacheDependency: 如果指定的全局狀態(tài)發(fā)生改變,則依賴改變.全局狀態(tài)是應(yīng)用中的一個(gè)跨哀求,跨會(huì)話的變量.它是通過(guò) CApplication::setGlobalState() 定義的.
CChainedCacheDependency: 如果鏈中的任何依賴發(fā)生改變,則此依賴改變.
CExpressionDependency: 如果指定的 PHP 表達(dá)式的結(jié)果發(fā)生改變,則依賴改變.此類從版本 1.0.4 起可用.PHP實(shí)例
4.緩存的使用:片段緩存PHP實(shí)例
片段緩存(Fragment Caching)PHP實(shí)例
片段緩存指緩存網(wǎng)頁(yè)某片段.例如,如果一個(gè)頁(yè)面在表中顯示每年的銷售摘要,我們可以存儲(chǔ)此表在緩存中,減少每次哀求需要重新產(chǎn)生的時(shí)間.PHP實(shí)例
要使用片段緩存,在控制器視圖腳本中調(diào)用CController::beginCache() 和CController::endCache() .這兩種辦法開(kāi)始和結(jié)束包括的頁(yè)面內(nèi)容將被緩存.類似data caching ,我們需要一個(gè)編號(hào),識(shí)別被緩存的片段.PHP實(shí)例
...別的HTML內(nèi)容... <?php if($this->beginCache($id)) { ?> ...被緩存的內(nèi)容... <?php $this->endCache(); } ?> ...別的HTML內(nèi)容...
在上面的,如果beginCache() 返回false,緩存的內(nèi)容將此地方自動(dòng)插入; 否則,在if語(yǔ)句內(nèi)的內(nèi)容將被執(zhí)行并在endCache()觸發(fā)時(shí)緩存.PHP實(shí)例
1. 緩存選項(xiàng)(Caching Options)PHP實(shí)例
當(dāng)調(diào)用beginCache(),可以提供一個(gè)數(shù)組由緩存選項(xiàng)組成的作為第二個(gè)參數(shù),以自定義片段緩存.事實(shí)上為了方便,beginCache() 和endCache()辦法是[ COutputCache ]widget的包裝.因此COutputCache的所有屬性都可以在緩存選項(xiàng)中初始化.PHP實(shí)例
有效期(Duration)PHP實(shí)例
也許是最常見(jiàn)的選項(xiàng)是duration,指定了內(nèi)容在緩存中多久有效.和CCache::set()過(guò)期參數(shù)有點(diǎn)類似.下面的代碼緩存內(nèi)容片段最多一小時(shí):PHP實(shí)例
...其他HTML內(nèi)容... <?php if($this->beginCache($id, array('duration'=>3600))) { ?> ...被緩存的內(nèi)容... <?php $this->endCache(); } ?> ...其他HTML內(nèi)容...
如果我們不設(shè)定期限,它將默認(rèn)為60 ,這意味著60秒后緩存內(nèi)容將無(wú)效.PHP實(shí)例
依賴(Dependency)PHP實(shí)例
像data caching ,內(nèi)容片段被緩存也可以有依賴.例如,文章的內(nèi)容被顯示取決于文章是否被修改.PHP實(shí)例
要指定一個(gè)依賴,我們建立了dependency選項(xiàng),可以是一個(gè)實(shí)現(xiàn)[ICacheDependency]的對(duì)象或可用于生成依賴對(duì)象的配置數(shù)組.下面的代碼指定片段內(nèi)容取決于lastModified 列的值是否變化:PHP實(shí)例
...其他HTML內(nèi)容... <?php if($this->beginCache($id, array('dependency'=>array( 'class'=>'system.caching.dependencies.CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post')))) { ?> ...被緩存的內(nèi)容... <?php $this->endCache(); } ?> ...其他HTML內(nèi)容...
變化(Variation)PHP實(shí)例
緩存的內(nèi)容可根據(jù)一些參數(shù)變化.例如,每個(gè)人的檔案都不一樣.緩存的檔案內(nèi)容將根據(jù)每個(gè)人ID變化.這意味著,當(dāng)調(diào)用beginCache()時(shí)將用不同的ID.PHP實(shí)例
COutputCache內(nèi)置了這一特征,程序員不需要編寫(xiě)根據(jù)ID變動(dòng)內(nèi)容的模式.以下是摘要.PHP實(shí)例
varyByRoute: 設(shè)置此選項(xiàng)為true ,緩存的內(nèi)容將根據(jù)route變化.因此,每個(gè)控制器和行動(dòng)的組合將有一個(gè)單獨(dú)的緩存內(nèi)容.
varyBySession: 設(shè)置此選項(xiàng)為true ,緩存的內(nèi)容將根據(jù)session ID變化.因此,每個(gè)用戶會(huì)話可能會(huì)看到由緩存提供的不同內(nèi)容.
varyByParam: 設(shè)置此選項(xiàng)的數(shù)組里的名字,緩存的內(nèi)容將根據(jù)GET參數(shù)的值變動(dòng).例如,如果一個(gè)頁(yè)面顯示文章的內(nèi)容根據(jù)id的GET參數(shù),我們可以指定varyByParam為array('id'),以使我們能夠緩存每篇文章內(nèi)容.如果沒(méi)有這樣的變化,我們只能能夠緩存某一文章.PHP實(shí)例
varyByExpression: by setting this option to a PHP expression, we can make the cached content to be variated according to the result of this PHP expression. This option has been available since version 1.0.4.
Request TypesPHP實(shí)例
有時(shí)候,我們希望片段緩存只對(duì)某些類型的哀求啟用.例如,對(duì)于某張網(wǎng)頁(yè)上顯示表單,我們只想要緩存initially requested表單(通過(guò)GET哀求).任何隨后顯示(通過(guò)POST哀求)的表單將不被緩存,因?yàn)楸韱慰赡馨脩糨斎?要做到這一點(diǎn),我們可以指定requestTypes 選項(xiàng):PHP實(shí)例
...其他HTML內(nèi)容... <?php if($this->beginCache($id, array('requestTypes'=>array('GET')))) { ?> ...被緩存的內(nèi)容... <?php $this->endCache(); } ?> ...其他HTML內(nèi)容...
2. 嵌套緩存(Nested Caching)PHP實(shí)例
片段緩存可以嵌套.就是說(shuō)一個(gè)緩存片段附在一個(gè)更大的片段緩存里.例如,意見(jiàn)緩存在內(nèi)部片段緩存,而且它們一起在外部緩存中在文章內(nèi)容里緩存.PHP實(shí)例
...其他HTML內(nèi)容... <?php if($this->beginCache($id1)) { ?> ...外部被緩存內(nèi)容... <?php if($this->beginCache($id2)) { ?> ...內(nèi)部被緩存內(nèi)容... <?php $this->endCache(); } ?> ...外部被緩存內(nèi)容... <?php $this->endCache(); } ?> ...其他HTML內(nèi)容...
嵌套緩存可以設(shè)定不同的緩存選項(xiàng).例如, 在上面的例子中內(nèi)部緩存和外部緩存可以設(shè)置時(shí)間長(zhǎng)短不同的持續(xù)值.當(dāng)數(shù)據(jù)存儲(chǔ)在外部緩存無(wú)效,內(nèi)部緩存仍然可以提供有效的內(nèi)部片段. 然而,反之就不行了.如果外部緩存包含有效的數(shù)據(jù), 它會(huì)永遠(yuǎn)保持緩存副本,即使內(nèi)容中的內(nèi)部緩存已經(jīng)過(guò)期.PHP實(shí)例
5.緩存的使用:頁(yè)面緩存PHP實(shí)例
頁(yè)面緩存PHP實(shí)例
頁(yè)面緩存指的是緩存整個(gè)頁(yè)面的內(nèi)容.頁(yè)面緩存可以發(fā)生在不同的地方. 例如,通過(guò)選擇適當(dāng)?shù)捻?yè)面頭,客戶端的瀏覽器可能會(huì)緩存網(wǎng)頁(yè)瀏覽有限時(shí)間. Web應(yīng)用程序本身也可以在緩存中存儲(chǔ)網(wǎng)頁(yè)內(nèi)容. 在本節(jié)中,我們側(cè)重于后一種方法.PHP實(shí)例
頁(yè)面緩存可以被看作是 片段緩存一個(gè)特殊情況 . 由于網(wǎng)頁(yè)內(nèi)容是往往通過(guò)應(yīng)用布局來(lái)生成,如果我們只是簡(jiǎn)單的在布局中調(diào)用beginCache() 和endCache(),將無(wú)法正常工作. 這是因?yàn)椴季衷贑Controller::render()辦法里的加載是在頁(yè)面內(nèi)容產(chǎn)生之后.PHP實(shí)例
如果想要緩存整個(gè)頁(yè)面,我們應(yīng)該跳過(guò)產(chǎn)生網(wǎng)頁(yè)內(nèi)容的動(dòng)作執(zhí)行.我們可以使用COutputCache作為動(dòng)作 過(guò)濾器來(lái)完成這一任務(wù).下面的代碼演示如何配置緩存過(guò)濾器:PHP實(shí)例
public function filters() { return array( array( 'COutputCache', 'duration'=>100, 'varyByParam'=>array('id'), ), ); }
上述過(guò)濾器配置會(huì)使過(guò)濾器適用于控制器中的所有行動(dòng). 我們可能會(huì)限制它在一個(gè)或幾個(gè)行動(dòng)通過(guò)使用插件操作器. 更多的細(xì)節(jié)中可以看過(guò)濾器.PHP實(shí)例
Tip: 我們可以使用COutputCache作為一個(gè)過(guò)濾器,因?yàn)樗鼜腃FilterWidget繼承過(guò)來(lái), 這意味著它是一個(gè)工具(widget)和一個(gè)過(guò)濾器.事實(shí)上,widget的工作方式和過(guò)濾器非常相似: 工具widget (過(guò)濾器filter)是在action動(dòng)作里的內(nèi)容執(zhí)行前執(zhí)行,在執(zhí)行后結(jié)束.PHP實(shí)例
6.緩存的使用:動(dòng)態(tài)內(nèi)容PHP實(shí)例
動(dòng)態(tài)內(nèi)容(Dynamic Content)PHP實(shí)例
當(dāng)使用fragment caching或page caching,我們常常遇到的這樣的情況 整個(gè)部分的輸出除了個(gè)別地方都是靜態(tài)的.例如,贊助頁(yè)可能會(huì)顯示靜態(tài)的贊助 信息,而用戶名稱顯示的是當(dāng)前用戶的.PHP實(shí)例
解決這個(gè)問(wèn)題,我們可以根據(jù)用戶名匹配緩存內(nèi)容,但是這將是我們寶貴空間一個(gè)巨大的浪費(fèi),因?yàn)榫彺娉擞脩裘渌蟛糠謨?nèi)容是相同的.我們還可以把網(wǎng)頁(yè)切成幾個(gè)片段并分別緩存,但這種情況會(huì)使頁(yè)面和代碼變得非常復(fù)雜.更好的辦法是使用由[ CController ]提供的動(dòng)態(tài)內(nèi)容dynamic content功能 .PHP實(shí)例
動(dòng)態(tài)內(nèi)容是指片段輸出即使是在片段緩存包括的內(nèi)容中也不會(huì)被緩存.即使是包括的內(nèi)容是從緩存中取出,為了使動(dòng)態(tài)內(nèi)容在所有時(shí)間是動(dòng)態(tài)的,每次都得重新生成.出于這個(gè)原因,我們要求 動(dòng)態(tài)內(nèi)容通過(guò)一些辦法或函數(shù)生成.PHP實(shí)例
調(diào)用CController::renderDynamic()在你想的地方插入動(dòng)態(tài)內(nèi)容.PHP實(shí)例
...別的HTML內(nèi)容... <?php if($this->beginCache($id)) { ?> ...被緩存的片段內(nèi)容... <?php $this->renderDynamic($callback); ?> ...被緩存的片段內(nèi)容... <?php $this->endCache(); } ?> ...別的HTML內(nèi)容...
在上面的, $callback指的是有效的PHP回調(diào).它可以是指向當(dāng)前控制器類的辦法或者全局函數(shù)的字符串名.它也可以是一個(gè)數(shù)組名指向一個(gè)類的辦法.其他任何的參數(shù),將傳遞到renderDynamic()辦法中.回調(diào)將返回動(dòng)態(tài)內(nèi)容而不是僅僅顯示它.PHP實(shí)例
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》PHP實(shí)例
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所贊助.PHP實(shí)例
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實(shí)例:YII Framework框架教程之緩存用法詳解》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/7370.html