?
このドキュメントでは、 php中國(guó)語(yǔ)ネットマニュアル リリース
Caching is used to speed up a call to display() or fetch() by saving its output to a file. If a cached version of the call is available,
that is displayed instead of regenerating the output. Caching can speed things
up tremendously, especially templates with longer computation times. Since
the output of display() or fetch() is cached, one cache file could conceivably
be made up of several template files, config files, etc.
緩存被用來(lái)保存一個(gè)文檔的輸出從而加速display()或fetch()函數(shù)的執(zhí)行。如果一個(gè)函數(shù)被加進(jìn)緩存,那么實(shí)際輸出的內(nèi)容將用緩存來(lái)代替。緩存可讓事物非常快速的執(zhí)行,特別是帶有長(zhǎng)計(jì)算時(shí)間的模板。一旦display()或fetch()用緩存輸出,那么一個(gè)緩存文檔將非常容易用幾個(gè)模板文檔或是配置文檔等來(lái)組成〔功力不小〕。
Since templates are dynamic, it is important to be careful what you are caching
and for how long. For instance, if you are displaying the front page of your
website that does not change its content very often, it might work well to
cache this page for an hour or more. On the other hand, if you are displaying
a page with a weather map containing new information by the minute, it would
not make sense to cache this page.
一旦模板是動(dòng)態(tài)〔應(yīng)該不難理解〕的,哪些文檔你加了緩存,緩存時(shí)間多長(zhǎng)都是很重要的。舉個(gè)例子,比如你站點(diǎn)的首頁(yè)內(nèi)容不是經(jīng)常更改,那么對(duì)首頁(yè)緩存一個(gè)小時(shí)或是更長(zhǎng)都可得到很好效果。相反,幾分鐘就要更新一下信息的天氣地圖頁(yè)面,用緩存就不好了。
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
|
With caching enabled, the function call to display('index.tpl') will render
the template as usual, but also saves a copy of its output to a file (a
cached copy) in the $cache_dir. Upon the next call to display('index.tpl'), the cached copy will be used instead
of rendering the template again.
建立緩存后,display('index.tpl')函數(shù)會(huì)把模板返回原來(lái)狀態(tài)〔沒(méi)緩存〕,也會(huì)把輸出保存copy〖n.名詞〗到$cache_dir.下次調(diào)用display('index.tpl'),保存的緩存copy〖n.〗會(huì)被再用來(lái)代替原來(lái)的模板。
Technical Note: The files in the $cache_dir are named similar to the template name. Although they end in the ".php" extention, they are not really executable php scripts. Do not edit these files!
技術(shù)提示:在$chche_dir目錄里的文檔命名跟模板一致。盡管是用.php作為擴(kuò)展名,但并不會(huì)被當(dāng)作php代碼來(lái)解析。所以不要去修改它。
Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds, or 1 hour. After that time expires, the
cache is regenerated. It is possible to give individual caches their own
expiration time by setting $caching = 2. See the documentation on $cache_lifetime for details.
每個(gè)緩存頁(yè)都有一個(gè)用$cache_lifetime來(lái)控制的會(huì)話期。初始值是3600秒,就是一小時(shí)〔廢話嘛〕。會(huì)話期結(jié)束,緩存就會(huì)重建。你可以通過(guò)設(shè)置$caching=2來(lái)控制單個(gè)緩存文件各自的的過(guò)期時(shí)間。祥細(xì)內(nèi)容察看$cache_lifetime里面的列表。
Example 14-2. setting cache_lifetime per cache
|
If $compile_check is enabled, every template file and config file that is involved with the cache
file is checked for modification. If any of the files have been modified
since the cache was generated, the cache is immediately regenerated. This
is a slight overhead so for optimum performance, leave $compile_check set
to false.
如果$compile_check可用,每個(gè)跟緩存文檔相關(guān)的模板文檔和配置文檔都會(huì)被檢查來(lái)確定是否需要修改。在緩存產(chǎn)生后,改動(dòng)任何文檔,緩存也跟著更新改動(dòng)。設(shè)置$compile_check為false,這是實(shí)現(xiàn)最佳性能的最小改動(dòng)〔應(yīng)該是這樣:D〕。
Example 14-3. enabling $compile_check
|
If $force_compile is enabled, the cache files will always be regenerated. This effectively turns
off caching. $force_compile is usually for debugging purposes only, a more
efficient way of disabling caching is to set $caching = false (or 0.)
一旦$force_compile可用,緩存文檔會(huì)一直重建。這有效地關(guān)閉緩存。$force_compile只是用來(lái)調(diào)試,更有效關(guān)閉緩存的方法是讓$caching = false(或0.)
The is_cached() function can be used to test if a template has a valid cache or not. If you
have a cached template that requires something like a database fetch, you
can use this to skip that process.
is_cached()函數(shù)可用來(lái)測(cè)試一個(gè)模板是否有有效的緩存。如果一個(gè)緩存模板需要從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù),可以用這個(gè)函數(shù)來(lái)跳過(guò)這個(gè)過(guò)程。
Example 14-4. using is_cached()
|
You can keep parts of a page dynamic with the insert template function. Let's say the whole page can be cached except for a banner
that is displayed down the right side of the page. By using an insert function
for the banner, you can keep this element dynamic within the cached content.
See the documentation on insert for details and examples.
你可以插入模板函數(shù)insert來(lái)使部分頁(yè)面動(dòng)態(tài)化。除了在右下方顯示的標(biāo)語(yǔ)外整個(gè)頁(yè)面都可以緩存。在緩存內(nèi)容里面可以插入函數(shù)來(lái)使標(biāo)語(yǔ)也動(dòng)態(tài)化。查看相關(guān)文檔關(guān)于insert的細(xì)節(jié)和例子。
You can clear all the cache files with the clear_all_cache() function, or individual cache files (or groups) with the clear_cache() function.
你可以用clear_all_cache()來(lái)清除所有緩存,或用clear_cache()來(lái)清除單個(gè)緩存文檔。
Example 14-5. clearing the cache
|