緩存適配器
注:在2.0.3以前有bug,請下載最新版本的CI。
CodeIgniter提供了多種目前業(yè)界流行的快速動(dòng)態(tài)緩存組件的封裝類。除了基于純文本的緩存(文件緩存)外,其他緩存組件均需對服務(wù)器環(huán)境進(jìn)行正確配置才能使用,否則程序會(huì)拋出致命異常(Fatal Exception)錯(cuò)誤。
目錄
- 用法舉例
- 函數(shù)速查
支持的緩存適配器
- Alternative PHP Cache (APC) 緩存
- 純文本緩存
- Memcached緩存
- 虛擬緩存
用法舉例
下面這個(gè)例子:首先加載緩存適配器,然后指定 APC 作為適配器優(yōu)先使用的緩存實(shí)現(xiàn),同時(shí),我們指定文本緩存作為替代方案。這樣,在一些服務(wù)器不支持APC的情況下(如國內(nèi)的虛擬主機(jī)),我們可以使用替代方案保證程序正常運(yùn)行。
$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
if ( ! $foo = $this->cache->get('foo'))
{
?????echo 'Saving to the cache!<br />';
?????$foo = 'foobarbaz!';
?????// Save into the cache for 5 minutes
?????$this->cache->save('foo', $foo, 300);
}
echo $foo;
譯者注:為了便于理解上面的代碼,我們不妨舉個(gè)例子。緩存適配器,我們可以理解為一個(gè)“通電的插座”;而APC緩存,相當(dāng)于我們希望插在插座上使用的空調(diào);文本緩存我們可以理解與空調(diào)功能相同但效率卻完全不同的電風(fēng)扇。
Function Reference
is_supported(driver['string'])
如果你通過 $this->cache->get() 來訪問緩存適配器,此函數(shù)將自動(dòng)觸發(fā)。但是,如果你希望有針對性的使用某個(gè)具體的緩存實(shí)現(xiàn)(如下例中的 APC),請確保調(diào)用此函數(shù),用來檢查服務(wù)器環(huán)境是否支持這種緩存類型。
if ($this->cache->apc->is_supported())
{
?????if ($data = $this->cache->apc->get('my_cache'))
?????{
??????????// do things.
?????}
}
get(id['string'])
此函數(shù)將嘗試從緩存系統(tǒng)中獲取指定的緩存項(xiàng)。如果緩存不存在,或者超過緩存期限,則返回 FALSE。
$foo = $this->cache->get('my_cached_item');
save(id['string'], data['mixed'], ttl['int'])
此函數(shù)嘗試將一個(gè)緩存項(xiàng)存儲(chǔ)到對應(yīng)的緩存系統(tǒng)中。如果存儲(chǔ)失敗,則返回FALSE。
第三個(gè)參數(shù)(可選項(xiàng))指定了緩存的存活時(shí)間,默認(rèn)為60秒。
$this->cache->save('cache_item_id', 'data_to_cache');
delete(id['string'])
此函數(shù)嘗試從緩存系統(tǒng)中刪除某個(gè)指定的緩存項(xiàng)。如果刪除失敗,則返回FALSE。
$this->cache->delete('cache_item_id');
clean()
此函數(shù)用來清空所有緩存。如果清空失敗,則返回 FALSE.
$this->cache->clean();
cache_info()
此函數(shù)將返回所有緩存信息.
var_dump($this->cache->cache_info());
get_metadata(id['string'])
此函數(shù)將返回緩存系統(tǒng)中指定緩存項(xiàng)的詳細(xì)信息。
var_dump($this->cache->get_metadata('my_cached_item'));
Drivers
Alternative PHP Cache (APC) Caching
All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
$this->load->driver('cache');
$this->cache->apc->save('foo', 'bar', 10);
For more information on APC, please see http://php.net/apc
基于文件的緩存
Unlike caching from the Output Class, 基于文件的緩存可以對view頁面分塊緩存. 不過使用時(shí)需要小心,應(yīng)該通過對程序進(jìn)行評(píng)測,以確保由于磁盤I/O增加所導(dǎo)致的性能下降相對于緩存所帶來的性能提升是值得的。
All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
$this->load->driver('cache');
$this->cache->file->save('foo', 'bar', 10);
Memcached 緩存
使用分布式 Memcached 服務(wù)器 可以通過配置文件: memcached.php 來配置,該文件在 application/config/ 目錄下.
All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
$this->load->driver('cache');
$this->cache->memcached->save('foo', 'bar', 10);
若想了解更多關(guān)于 Memcached的信息, 請參考 http://php.net/memcached
Dummy Cache
This is a caching backend that will always 'miss.' It stores no data, but lets you keep your caching code in place in environments that don't support your chosen cache.
?