?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
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.
緩存被用來保存一個文檔的輸出從而加速display()或fetch()函數(shù)的執(zhí)行。如果一個函數(shù)被加進(jìn)緩存,那么實際輸出的內(nèi)容將用緩存來代替。緩存可讓事物非??焖俚膱?zhí)行,特別是帶有長計算時間的模板。一旦display()或fetch()用緩存輸出,那么一個緩存文檔將非常容易用幾個模板文檔或是配置文檔等來組成〔功力不小〕。
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.
一旦模板是動態(tài)〔應(yīng)該不難理解〕的,哪些文檔你加了緩存,緩存時間多長都是很重要的。舉個例子,比如你站點(diǎn)的首頁內(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
|
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ù)會把模板返回原來狀態(tài)〔沒緩存〕,也會把輸出保存copy〖n.名詞〗到$cache_dir.下次調(diào)用display('index.tpl'),保存的緩存copy〖n.〗會被再用來代替原來的模板。
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ò)展名,但并不會被當(dāng)作php代碼來解析。所以不要去修改它。
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.
每個緩存頁都有一個用$cache_lifetime來控制的會話期。初始值是3600秒,就是一小時〔廢話嘛〕。會話期結(jié)束,緩存就會重建。你可以通過設(shè)置$caching=2來控制單個緩存文件各自的的過期時間。祥細(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可用,每個跟緩存文檔相關(guān)的模板文檔和配置文檔都會被檢查來確定是否需要修改。在緩存產(chǎn)生后,改動任何文檔,緩存也跟著更新改動。設(shè)置$compile_check為false,這是實現(xiàn)最佳性能的最小改動〔應(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可用,緩存文檔會一直重建。這有效地關(guān)閉緩存。$force_compile只是用來調(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ù)可用來測試一個模板是否有有效的緩存。如果一個緩存模板需要從數(shù)據(jù)庫中獲取數(shù)據(jù),可以用這個函數(shù)來跳過這個過程。
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來使部分頁面動態(tài)化。除了在右下方顯示的標(biāo)語外整個頁面都可以緩存。在緩存內(nèi)容里面可以插入函數(shù)來使標(biāo)語也動態(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()來清除所有緩存,或用clear_cache()來清除單個緩存文檔。
Example 14-5. clearing the cache
|