緩存被用來保存一個文檔的輸出從而加速display()或fetch()函數(shù)的執(zhí)行。如果一個函數(shù)被加進緩存,那么實際輸出的內(nèi)容將用緩存來代替。緩存可讓事物非??焖俚膱?zhí)行,特別是帶有長計算時間的模板。一旦display()或fetch()用緩存輸出,那么一個緩存文檔將非常容易用幾個模板文檔或是配置文檔等來組成〔功力不小〕。
一旦模板是動態(tài)〔應(yīng)該不難理解〕的,哪些文檔你加了緩存,緩存時間多長都是很重要的。舉個例子,比如你站點的首頁內(nèi)容不是經(jīng)常更改,那么對首頁緩存一個小時或是更長都可得到很好效果。相反,幾分鐘就要更新一下信息的天氣地圖頁面,用緩存就不好了。
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
|
建立緩存后,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
|
如果$compile_check可用,每個跟緩存文檔相關(guān)的模板文檔和配置文檔都會被檢查來確定是否需要修改。在緩存產(chǎn)生后,改動任何文檔,緩存也跟著更新改動。設(shè)置$compile_check為false,這是實現(xiàn)最佳性能的最小改動〔應(yīng)該是這樣:D〕。
Example 14-3. enabling $compile_check
|
一旦$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()
|
你可以插入模板函數(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
|