官方文檔對緩衝技術的介紹:
緩衝被用來儲存一個文檔的輸出從而加速display()或fetch()函數的執行。如果一個函數被加進緩衝,那麼實際輸出的內容將用緩衝來代替。緩衝可讓事物非常快速的執行,特別是帶有長計算時間的模板。一旦display()或fetch()用緩衝輸出,那麼一個緩衝文檔將非常容易用幾個模板文檔或是配置文檔等來組成〔功力不小〕。
一旦模板是動態〔應該不難理解〕的,哪些文檔你加了緩衝,緩衝時間多長都是很重要的。舉個例子,比如你網站的首頁內容不是經常更改,那麼對首頁緩衝一個小時或是更長都可得到很好效果。相反,幾分鐘就要更新一下資訊的天氣地圖頁面,用緩衝就不好了。
設定緩衝目錄
在使用緩衝之前,首先要確定好快取檔案的儲存目錄,可通過下列兩種方法設定緩衝目錄:
1 <?php 2 3 // set directory where rendered template's output is stored 4 $smarty->setCacheDir('./cache'); 5 6 // chaining of method calls 7 $smarty->setTemplateDir('./templates') 8 ->setCompileDir('./templates_c') 9 ->setCacheDir('./cache');10 11 ?>
開啟緩衝功能
配置好快取檔案路徑後,就可通過$caching開啟Smarty緩衝功能,該值可為:
- Smarty::CACHING_LIFETIME_CURRENT:用$cache_lifetime來判斷快取檔案是否到期。
- Smarty::CACHING_LIFETIME_SAVED:根據快取檔案的產生時間與$cache_lifetime判斷快取檔案是否到期,這個設定可以為特定的快取檔案設定特定的緩衝時間。
- Smarty::CACHING_OFF:關閉Smarty緩衝功能(預設)
1 <?php 2 require('Smarty.class.php'); 3 $smarty = new Smarty; 4 5 // uses the value of $smarty->cacheLifetime() to determine 6 // the number of seconds a cache is good for 7 $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); 8 9 $smarty->display('index.tpl');10 ?>
註:如果設定$compile_check屬性為true時,當模版或設定檔變更後,快取檔案都將被重建。
設定緩衝時間
$cache_lifetime:定義快取檔案的有效時間,以秒為單位,一旦時間到期,快取檔案將會被重建(預設3600秒)。
如果將該值設定為-1,表示快取檔案永不到期,設定為0,快取檔案將總會被重建(僅用於開發階段)。
可以將$caching值設定為Smarty::CACHING_LIFETIME_SAVED,在設定$caching_lifetime,這樣,之後通過調用display()或fetch()來顯示的模版,將會產生自己模版的獨立的緩衝時間。
註:使用該設定之前,必須保證$caching處於開啟狀態。
1 <?php 2 require('Smarty.class.php'); 3 $smarty = new Smarty; 4 5 // retain current cache lifetime for each specific display call 6 $smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED); 7 8 // set the cache_lifetime for index.tpl to 5 minutes 9 $smarty->setCacheLifetime(300);10 $smarty->display('index.tpl');11 12 // set the cache_lifetime for home.tpl to 1 hour13 $smarty->setCacheLifetime(3600);14 $smarty->display('home.tpl');15 16 // NOTE: the following $cache_lifetime setting will not work when $caching17 // is set to Smarty::CACHING_LIFETIME_SAVED.18 // The cache lifetime for home.tpl has already been set19 // to 1 hour, and will no longer respect the value of $cache_lifetime.20 // The home.tpl cache will still expire after 1 hour.21 $smarty->setCacheLifetime(30); // 30 seconds22 $smarty->display('home.tpl');23 ?>
$cache_id
某些時候你可能需要對一個頁面產生多個快取檔案,如帶有分頁的頁面,使用的模版相同,但不同頁數的內容卻不一樣。對於這種情況,只需對display()或fetch()方法傳遞第二個參數cache_id既可。如當訪問頁面: http://127.0.0.1/test?page=1
1 <?php2 $page = $_GET['page'];3 $smarty->display('test.tpl', $page);4 ?>
註:$cache_id將被用來產生的快取檔案名中的一部分來使用,所以需要限制$cache_id的命名規則,如不能使用空格,及其它一些特殊字元等。
在一個項目中,也許需要緩衝成百上千個頁面,預設情況下,這些快取檔案將全部被緩衝到$cache_dir目錄下,對與一些系統而言,將這些快取檔案存放到不同的子目錄中去尋找某一特定的檔案,要比在一個包含了上千個檔案目錄中去尋找一個特定檔案,執行的速度要快得多。因此,我們有必要將這些快取檔案分別儲存在快取檔案目錄的子目錄中。通過設定$cache_id可以到達這個目的。
首先需要設定$use_sub_dirs的值為ture,它允許我們在產生快取檔案的時候建立子緩衝目錄。
將要組建目錄放到$cache_id中,之間用"|"分隔既可,如在我要開啟admin/login.tpl模版檔案時,我希望快取檔案同樣儲存在$cache_dir/admin/目錄下,可以這樣實現:
1 <?php2 $smarty->use_sub_dirs = true;3 $smarty->display('admin/login.tpl', 'admin|');4 ?>
上述語句將會在你的緩衝目錄下自動建立一個admin目錄,並將產生的login.tpl的快取檔案存放到該目錄下,當然,你可以指定多層目錄,如:'admin|user|login|',快取檔案將會儲存在$chace_dir/admin/user/login/目錄下。
註:上述涉及到的功能對與$compile_id同樣生效
精確控制緩衝
我們經常會遇到這種情況,一個頁麵包含多個部分,有些部分的內容變動幾率很小,而有些部分的內容時時都在變動,如股票網站,網頁中的股票走勢圖需要時時更新,而其它一些內容的更新頻率不會很高,這是我們就需要只是真對頁面中除走勢圖以外的所有內容進行緩衝。Smarty的緩衝機制非常靈活,可以為要緩衝模版的某段地區指定為禁止緩衝。如使用{nocache}{/nocache}標籤,以及其它一些支援nocache的標籤或方法。
1 {nocache}2 {*通過{nocache}標籤進行無緩衝設定 *}3 {$smarty.now|date_format}4 {/nocache}5 {*通過nocache屬性進行無緩衝設定*}6 {$smarty.now|date_format nocache}
1 <?php2 //通過向assign傳遞第三個參數true來設定無緩衝3 $smarty->assign('foo',time(),true);4 ?>
刪除快取檔案
void clearCache(string template, string cache_id, string compile_id, int expire_time);
用於清除指定模版的快取檔案。
選擇性參數expire_time:用來指定超過某一時間(以秒為單位)的緩衝才會被清除。
1 <?php2 // clear the cache for a template3 $smarty->clearCache('index.tpl');4 5 // clear the cache for a particular cache id in an multiple-cache template6 $smarty->clearCache('index.tpl', 'MY_CACHE_ID');7 ?>
你也可以通過cache_id刪除指定緩衝目錄下的子目錄中的快取檔案,如:
1 $smarty->clearCache(null, 'admin|user|');
表示刪除$cache_dir/admin/user/目錄下的所有快取檔案。
clearAllCache(int expire_time);
清除所有快取檔案。
其它緩衝常用變數與方法
$force_compile
設定該值為true時,則每次都會強制重建快取檔案,並會忽略$compile_check的設定,僅可在開發過程中開啟該選項。預設為false。
bool isCached(string template, string cache_id, string compile_id);
判斷指定模版是否存在有效快取檔案,僅當$caching處於開啟狀態,該方法才可用。
參數cache_id和compile_id為選擇性參數。
1 <?php2 $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);3 4 if(!$smarty->isCached('index.tpl')) {5 // do database calls, assign vars here6 }7 8 $smarty->display('index.tpl');9 ?>
getCacheDir();
該方法用來擷取快取檔案的路徑資訊。
1 <?php2 3 // get directory where compiled templates are stored4 $cacheDir = $smarty->getCacheDir();5 6 ?>
$caching_type
用於設定緩衝的資源類型,預設:file。關於資源類型,將在後面介紹。
$cache_modified_check
如果設定該變數為真,Smarty將分析用戶端發送來的If-Modified-Since頭資訊.如果快取檔案時間戳記自上次訪問以來沒有改變,則發送一個"304 Not Modified"頭,而不是快取檔案內容.這種方式僅工作於沒有 {insert} 標記的緩衝內容.