本文實例講述了YII Framework框架緩存用法。分享給大家供大家參考,具體如下:緩存的產生原因眾所周知。于是YII作為一個高效,好用的框架,不能不支持緩存。所以YII對各種流行的緩存都提供了接口,你可以根據你的需要使用不同的緩存。1.YII中的緩存介紹YII中的緩存是通過組件方式定義的,具體在如下目錄/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.php1 directory, 17 files官方原文解釋如下:Yii 提供了不同的緩存組件,可以將緩存數據存儲到不同的媒介中。例如, CMemCache 組件封裝了 PHP 的 memcache 擴展并使用內存作為緩存存儲媒介。 CApcCache 組件封裝了 PHP APC 擴展; 而 CDbCache 組件會將緩存的數據存入數據庫。下面是一個可用緩存組件的列表:CMemCache: 使用 PHP memcache 擴展.CApcCache: 使用 PHP APC 擴展.CXCache: 使用 PHP XCache 擴展。注意,這個是從 1.0.1 版本開始支持的。CEAcceleratorCache: 使用 PHP EAccelerator 擴展.CDbCache: 使用一個數據表存儲緩存數據。默認情況下,它將創建并使用在 runtime 目錄下的一個 SQLite3 數據庫。 你也可以通過設置其 connectionID 屬性指定一個給它使用的數據庫。CZendDataCache: 使用 Zend Data Cache 作為后臺緩存媒介。注意,這個是從 1.0.4 版本開始支持的。CFileCache: 使用文件存儲緩存數據。這個特別適合用于存儲大塊數據(例如頁面)。注意,這個是從 1.0.6 版本開始支持的。CDummyCache: 目前 dummy 緩存并不實現緩存功能。此組件的目的是用于簡化那些需要檢查緩存可用性的代碼。 例如,在開發階段或者服務器尚未支持實際的緩存功能,我們可以使用此緩存組件。當啟用了實際的緩存支持后,我們可以切換到使用相應的緩存組件。 在這兩種情況中,我們可以使用同樣的代碼Yii::app()- cache- get($key) 獲取數據片段而不需要擔心 Yii::app()- cache 可能會是 null。此組件從 1.0.5 版開始支持。提示: 由于所有的這些緩存組件均繼承自同樣的基類 CCache,因此無需改變使用緩存的那些代碼就可以切換到使用另一種緩存方式。緩存可以用于不同的級別。最低級別中,我們使用緩存存儲單個數據片段,例如變量,我們將此稱為 數據緩存(data caching)。下一個級別中,我們在緩存中存儲一個由視圖腳本的一部分生成的頁面片段。 而在最高級別中,我們將整個頁面存儲在緩存中并在需要時取回。在接下來的幾個小節中,我們會詳細講解如何在這些級別中使用緩存。注意: 按照定義,緩存是一個不穩定的存儲媒介。即使沒有超時,它也并不確保緩存數據一定存在。 因此,不要將緩存作為持久存儲器使用。(例如,不要使用緩存存儲 Session 數據)。2.緩存的配置和調用方式yii中的緩存主要是通過組件的方式實現的,具體需要配置方式可以通過緩存的類說明進行配置。通常是指定緩存組件的類例如apc'cache'= array( 'html' target='_blank'>class'= 'system.caching.CApcCache'memcache的配置方式可能是* array(* 'components'= array(* 'cache'= array(* 'class'= 'CMemCache',* 'servers'= array(* array(* 'host'= 'server1',* 'port'= 11211,* 'weight'= 60,* array(* 'host'= 'server2',* 'port'= 11211,* 'weight'= 40,使用方式:yii封裝了對不同緩存操作的方法,主要集中在CCache。CCache是所有Cache類的基類。所以配置好緩存后可以調用方式很簡單: * 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.0abstract class CCache extends CApplicationComponent implements ICache, ArrayAccess