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() 的呼叫。如果呼叫的檔案已有快取的話,則會展示已儲存的快取,而不會重新產生輸出。快取可 以產生極大的影響,特別是在當樣版需要極大的運算時間的時候。當 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.
因為樣版檔案是會改變的,所以必須注意到你快取到的內容與時間。舉例來說,如果 你正在展現你的網站的首頁的話,一般來說首頁的內容通常不會改變,所以將首頁快取住一個小 時或更久將是一個不錯的做法。另一方面,如果你在展示一個天氣圖的網頁,內容包括每分鐘更 新的資訊,將此頁快取的話則不會有多大的意義。
The first thing to do is enable caching. This is done by setting $caching = true (or 1.)
第一件要作的事情是開啟快取,方法是將 $caching = true (or 1.)
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') 時,輸出樣版的方式將與正常的 情況一樣,此外系統將會把輸出的檔案複製一份存到 $cache_dir。 當下次呼叫 display('index.tpl') 時則會使用存到快取目錄下的複製檔案,而不會重新產 生輸出。
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!
技術提醒: 在 $cache_dir 目錄中檔案的名稱與樣版檔案名稱類似,此時雖然檔案的結尾是 ".php" 附檔名,但是這些檔案並非為真正可執行的 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秒或1小時。超過此時間後會重新產生快取。我們可以給某些特定的快取 他們自己的存活時間,設定方式為 $caching = 2。請參閱文件 $cache_lifetime 以得到更多的內容。
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 是開啟的話,則系統會檢查包含在快取檔內的樣版檔案與組態檔是否有所改變。如果在快取產 生完畢之後有任何的檔案有變更的話,則會重新產生快取,不過如此一來將會無法達到最佳效率 ,所以還是建議將 $compile_check 設為 false。
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 是開啟的話,則被快取住的檔案都會被重新產生,此舉將導致關閉快取功能。$force_compile 通常只為了除錯的原因才使用,一個有效率關掉快取的方式為設定 $caching = false (or 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() 函式可以用來測試是否某個樣板有正確的快取,如果你有一個快取樣板需要像是抓取資料庫資 類的東西的話,你就可以使用此函式跳過此程序。
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.
你可以使用 insert 樣板函式,以保持網頁中某部分是可變更的。讓我們來假設一個情況,假設某個網頁除了一個 右下方橫幅廣告外,其他都是被快取住的話,那我們就可以使用 insert 函式插入此橫幅廣告, 如此以來我們就可以變更快取內容中的廣告內容。請參閱文件 insert 以取得更多內容與範例。
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() 函式以清除單一或單一群組的快取檔案。