鍍金池/ 教程/ PHP/ Smarty Caching緩存
Smarty安裝
Smarty insert
Smarty 建立緩存
Smarty調(diào)試控制臺
Smarty if,elseif,else
Smarty include_php
Smarty多個緩存
Smarty方法
Smarty section,sectionelse
Smarty注釋代碼
Smarty屬性
Smarty緩沖處理函數(shù)
Smarty變量調(diào)節(jié)器
Smarty函數(shù)
Smarty組合修改器
Smarty雙引號里值的嵌入
Smarty預(yù)過濾器
Smarty foreach,foreachelse
Smarty include
Smarty Caching緩存
Smarty變量
Smarty assign用法
Smarty控制插件輸出緩沖
Smarty從配置文件讀取的變量
Smarty對象
Smarty literal
Smarty緩存集合
Smarty教程
Smarty display方法
Smarty自定義函數(shù)
Smarty配置文件
Smarty擴展設(shè)置
Smarty數(shù)學(xué)運算
Smarty輸出濾鏡
Smarty fetch方法

Smarty Caching緩存

Caching [緩存]

Table of Contents [內(nèi)容列表]

緩存被用來保存一個文檔的輸出從而加速display()或fetch()函數(shù)的執(zhí)行。如果一個函數(shù)被加進緩存,那么實際輸出的內(nèi)容將用緩存來代替。緩存可讓事物非??焖俚膱?zhí)行,特別是帶有長計算時間的模板。一旦display()或fetch()用緩存輸出,那么一個緩存文檔將非常容易用幾個模板文檔或是配置文檔等來組成〔功力不小〕。


一旦模板是動態(tài)〔應(yīng)該不難理解〕的,哪些文檔你加了緩存,緩存時間多長都是很重要的。舉個例子,比如你站點的首頁內(nèi)容不是經(jīng)常更改,那么對首頁緩存一個小時或是更長都可得到很好效果。相反,幾分鐘就要更新一下信息的天氣地圖頁面,用緩存就不好了。

Setting Up Caching [建立緩存]

The first thing to do is enable caching. This is done by setting $caching = true (or 1.)
首先要做的就是讓緩存可用。這就要設(shè)置$caching = true(或 1.)


Example 14-1. enabling caching
例14-1.使緩存可用

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;

$smarty->display('index.tpl');


建立緩存后,display('index.tpl')函數(shù)會把模板返回原來狀態(tài)〔沒緩存〕,也會把輸出保存copy〖n.名詞〗到$cache_dir.下次調(diào)用display('index.tpl'),保存的緩存copy〖n.〗會被再用來代替原來的模板。

技術(shù)提示:在$chche_dir目錄里的文檔命名跟模板一致。盡管是用.php作為擴展名,但并不會被當(dāng)作php代碼來解析。所以不要去修改它。

每個緩存頁都有一個用$cache_lifetime來控制的會話期。初始值是3600秒,就是一小時〔廢話嘛〕。會話期結(jié)束,緩存就會重建。你可以通過設(shè)置$caching=2來控制單個緩存文件各自的的過期時間。祥細內(nèi)容察看$cache_lifetime里面的列表。


Example 14-2. setting cache_lifetime per cache
例14-2 設(shè)置單個緩存會話期〔時間〕

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = 2; // lifetime is per cache

// set the cache_lifetime for index.tpl to 5 minutes
$smarty->cache_lifetime = 300;
$smarty->display('index.tpl');

// set the cache_lifetime for home.tpl to 1 hour
$smarty->cache_lifetime = 3600;
$smarty->display('home.tpl');


// NOTE: the following $cache_lifetime setting will not work when $caching = 2.
//提示:在$chching=2后面的$chche_lifetime不會起作用。
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// home.tpl的緩存會話期設(shè)為1小時后,不會再按$cache_lifetime的值改變。
// The home.tpl cache will still expire after 1 hour.
// home.tpl的緩存會在1小時后結(jié)束。$smarty->cache_lifetime = 30; // 30 seconds
$smarty->display('home.tpl');

 
如果$compile_check可用,每個跟緩存文檔相關(guān)的模板文檔和配置文檔都會被檢查來確定是否需要修改。在緩存產(chǎn)生后,改動任何文檔,緩存也跟著更新改動。設(shè)置$compile_check為false,這是實現(xiàn)最佳性能的最小改動〔應(yīng)該是這樣:D〕。


Example 14-3. enabling $compile_check
例14-3.可用$compile_check

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;
$smarty->compile_check = true;

$smarty->display('index.tpl');


一旦$force_compile可用,緩存文檔會一直重建。這有效地關(guān)閉緩存。$force_compile只是用來調(diào)試,更有效關(guān)閉緩存的方法是讓$caching = false(或0.)

is_cached()函數(shù)可用來測試一個模板是否有有效的緩存。如果一個緩存模板需要從數(shù)據(jù)庫中獲取數(shù)據(jù),可以用這個函數(shù)來跳過這個過程。


Example 14-4. using is_cached()
例14-4.使用is_cached()

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;
// www.yiibai.com/smarty
if(!$smarty->is_cached('index.tpl')) {
	// No cache available, do variable assignments here.
	$contents = get_database_contents();
	$smarty->assign($contents);
}

$smarty->display('index.tpl');


你可以插入模板函數(shù)insert來使部分頁面動態(tài)化。除了在右下方顯示的標(biāo)語外整個頁面都可以緩存。在緩存內(nèi)容里面可以插入函數(shù)來使標(biāo)語也動態(tài)化。查看相關(guān)文檔關(guān)于insert的細節(jié)和例子。


你可以用clear_all_cache()來清除所有緩存,或用clear_cache()來清除單個緩存文檔。


Example 14-5. clearing the cache
例14-5.清除緩存

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;

// clear out all cache files
$smarty->clear_all_cache();

// clear only cache for index.tpl
$smarty->clear_cache('index.tpl');

$smarty->display('index.tpl');