W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
緩存可以通過設(shè)置 $caching
為:Smarty::CACHING_LIFETIME_CURRENT
或Smarty::CACHING_LIFETIME_SAVED
來開啟。
Example 15.1. 開啟緩存
<?php require('Smarty.class.php'); $smarty = new Smarty; // 使用$smarty->cacheLifetime()可以更精確定義緩存時(shí)間 $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); $smarty->display('index.tpl'); ?>
開啟了緩存,調(diào)用display('index.tpl')
會(huì)正常渲染模板,但也會(huì)保存一份輸出的內(nèi)容到$cache_dir
目錄下的文件中(緩存副本)。 在下一次調(diào)用display('index.tpl')
, 緩存文件會(huì)替代渲染模板的過程。
在$cache_dir
下的文件,文件名與模板名稱相似。 雖然這些文件也有.php
后綴,但它們不會(huì)被直接執(zhí)行。請(qǐng)不要編輯這些文件!
每個(gè)緩存頁面都有一個(gè)緩存過期時(shí)間$cache_lifetime
。 默認(rèn)是3600秒,也就是一小時(shí)。 當(dāng)超過了此時(shí)間,緩存將被重新生成。 當(dāng)設(shè)置$caching
為Smarty::CACHING_LIFETIME_SAVED
時(shí),可以給每個(gè)緩存設(shè)置其單獨(dú)的緩存時(shí)間。 參見$cache_lifetime
。
Example 15.2. 為每個(gè)緩存設(shè)置$cache_lifetime
<?php require('Smarty.class.php'); $smarty = new Smarty; // 讓每個(gè)緩存的過期時(shí)間都可以在display執(zhí)行前單獨(dú)設(shè)置。 $smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED); // 設(shè)置index.tpl的過期時(shí)間為5分鐘 $smarty->setCacheLifetime(300); $smarty->display('index.tpl'); // 設(shè)置home.tpl的過期時(shí)間為1小時(shí) $smarty->setCacheLifetime(3600); $smarty->display('home.tpl'); // 注意:當(dāng)$caching設(shè)置了Smarty::CACHING_LIFETIME_SAVED后, // 下面的$cache_lifetime將不會(huì)起效。 // home.tpl已經(jīng)設(shè)置了過期時(shí)間為1小時(shí), // 所以不會(huì)再遵循下面的$cache_lifetime值, // home.tpl的過期時(shí)間還是1小時(shí)。 $smarty->setCacheLifetime(30); // 30 秒 $smarty->display('home.tpl'); ?>
當(dāng) $compile_check
開啟的時(shí)候(默認(rèn)開啟), 每個(gè)模板文件和配置文件都會(huì)在緩存檢查的時(shí)候執(zhí)行編譯檢查。 如果這些文件在緩存生成后被修改,那么緩存會(huì)馬上重新生成。 這是一個(gè)覆蓋的選項(xiàng),所以更好的性能建議把 $compile_check
設(shè)置成false。
Example 15.3. 關(guān)閉 $compile_check
<?php require('Smarty.class.php'); $smarty = new Smarty; $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); $smarty->setCompileCheck(false); $smarty->display('index.tpl'); ?>
如果開啟了 $force_compile
, 緩存文件將總是會(huì)重新生成。效果和關(guān)閉緩存是一樣的, 而且還會(huì)降低性能。 $force_compile
一般用于 調(diào)試的目的。 更確當(dāng)?shù)姆绞绞前丫彺?em>$caching
設(shè)置成Smarty::CACHING_OFF。
isCached()
函數(shù)可以檢查模板的緩存是否存在。 如果你的模板是需要讀取某些數(shù)據(jù)(如數(shù)據(jù)庫), 那么你可以用它來跳過這個(gè)過程。
Example 15.4. 使用 isCached()
<?php require('Smarty.class.php'); $smarty = new Smarty; $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); if(!$smarty->isCached('index.tpl')) { // 找不到緩存,這里進(jìn)行一些賦值操作 $contents = get_database_contents(); $smarty->assign($contents); } $smarty->display('index.tpl'); ?>
你可以使用{nocache}{/nocache}
來設(shè)置頁面上部分區(qū)塊是動(dòng)態(tài)的(不緩存), 同樣你也可以使用 {insert}
函數(shù),或者nocache
參數(shù)來達(dá)到同樣目的。
比如說我們希望把整個(gè)頁面緩存,除了頁面兩邊顯示的banner廣告。 那么我們可以用{insert}
來顯示banner廣告, 這樣就可以在緩存的內(nèi)容里面,保存廣告部分的動(dòng)態(tài)效果。 參見 {insert}
的詳細(xì)文檔。
你可以通過clearAllCache()
來刪除全部緩存,或者 用clearCache()
來刪除特定的緩存組的緩存內(nèi)容。
Example 15.5. 刪除緩存
<?php require('Smarty.class.php'); $smarty = new Smarty; $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); // 僅刪除index.tpl的緩存 $smarty->clearCache('index.tpl'); // 刪除全部緩存 $smarty->clearAllCache(); $smarty->display('index.tpl'); ?>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: