《PHP教程:Smarty模板引擎緩存機(jī)制詳解》要點(diǎn):
本文介紹了PHP教程:Smarty模板引擎緩存機(jī)制詳解,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
本文實(shí)例講述了Smarty模板引擎緩存機(jī)制.分享給大家供大家參考,具體如下:PHP學(xué)習(xí)
首先說(shuō)下smarty緩存和編譯,這是兩個(gè)不同的概念,編譯默認(rèn)情況下是啟動(dòng)的,而緩存機(jī)制需要人為開(kāi)啟,smarty編譯過(guò)的文件還是php文件,所以執(zhí)行的時(shí)候還是編譯的,如果涉及到數(shù)據(jù)庫(kù),還是要拜訪數(shù)據(jù)庫(kù)的所以開(kāi)銷(xiāo)也不小啦,所以需要smarty緩存來(lái)解決!PHP學(xué)習(xí)
1.開(kāi)啟全局緩存PHP學(xué)習(xí)
$smarty->cache_dir = "/caches/"; //緩存目錄 $smarty->caching = true; //開(kāi)啟緩存,為flase的時(shí)侯緩存無(wú)效 $smarty->cache_lifetime = 3600; //緩存時(shí)間
2.一個(gè)頁(yè)面使用多個(gè)緩存PHP學(xué)習(xí)
如:一個(gè)文章模板頁(yè)面會(huì)生成多個(gè)文章頁(yè)面,當(dāng)然是緩存成很多頁(yè)面,實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,只要在display()辦法設(shè)置第二個(gè)參數(shù),指定唯一標(biāo)識(shí)符即可.如下php代碼:PHP學(xué)習(xí)
$smarty->display('index.tpl',$_GET["article_id"]);
如上,通過(guò)第二個(gè)參數(shù)文章的id緩存一個(gè)文章頁(yè)面.PHP學(xué)習(xí)
3.為緩存減小開(kāi)銷(xiāo)PHP學(xué)習(xí)
也就是說(shuō),已經(jīng)緩存的頁(yè)面無(wú)需進(jìn)行數(shù)據(jù)庫(kù)的操作處理了,可通過(guò)is_cached()辦法判斷!PHP學(xué)習(xí)
if(!$smarty->is_cached('index.tpl')){ //調(diào)用數(shù)據(jù)庫(kù) } $smarty->display('index.tpl');
4.清除緩存PHP學(xué)習(xí)
一般在開(kāi)發(fā)過(guò)程中是不開(kāi)啟緩存的,因?yàn)樵诰彺鏁r(shí)間內(nèi)輸出結(jié)果不變,但是在應(yīng)用過(guò)程中開(kāi)啟緩存能大大提高web性能,清除緩存辦法如下:PHP學(xué)習(xí)
clear_all_cache();//清除所有緩存 clear_cache('index.tpl');//清除index.tpl的緩存 clear_cache('index.tpl',cache_id);//清除指定id的緩存
5.關(guān)閉局部緩存PHP學(xué)習(xí)
如果一個(gè)頁(yè)面中一部分緩存,而另一部分不需要緩存,就可以這樣做,比如說(shuō)顯示用戶(hù)登錄的名稱(chēng)就需要關(guān)閉緩存,smarty提供了如下三種解決辦法:PHP學(xué)習(xí)
(1)使用insert模板的一部分不被緩存PHP學(xué)習(xí)
定義一個(gè)inser標(biāo)簽要使用的處理函數(shù),函數(shù)名格式為:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是說(shuō),如果你定義的函數(shù)為insert_abc,則模板中使用辦法為{insert name=abc}PHP學(xué)習(xí)
參數(shù)通過(guò)$params傳入PHP學(xué)習(xí)
也可以做成insert插件,文件名命名為:insert.xx.php,函數(shù)命名為:smarty_insert_aa($params,&$smarty),xx定義同上PHP學(xué)習(xí)
(2)$smarty->register_block($params, &$smarty)使整篇頁(yè)面中的某一塊不被緩存PHP學(xué)習(xí)
定義一個(gè)block:PHP學(xué)習(xí)
smarty_block_name($params,$content, &$smarty){return $content;} //name表示區(qū)域名
注冊(cè)block:PHP學(xué)習(xí)
$smarty->register_block(name, smarty_block_name, false); //第三參數(shù)false表示該區(qū)域不被緩存
模板寫(xiě)法:PHP學(xué)習(xí)
{name}內(nèi)容 {/name}
寫(xiě)成block插件:PHP學(xué)習(xí)
第一步:定義一件插件函數(shù):block.cacheless.php,放在smarty的 plugins目錄PHP學(xué)習(xí)
block.cacheless.php的內(nèi)容如下:PHP學(xué)習(xí)
<?php function smarty_block_cacheless($param, $content, &$smarty) { return $content; } ?>
第二步:編寫(xiě)程序及模板PHP學(xué)習(xí)
示例程序:testCacheLess.phpPHP學(xué)習(xí)
<?php include(Smarty.class.php); $smarty = new Smarty; $smarty->caching=true; $smarty->cache_lifetime = 6; $smarty->display(cache.tpl); ?>
所用的模板:cache.tplPHP學(xué)習(xí)
已經(jīng)緩存的:{$smarty.now}<br> {cacheless} 沒(méi)有緩存的:{$smarty.now} {/cacheless}
現(xiàn)在運(yùn)行一下,發(fā)現(xiàn)是不起作用的,兩行內(nèi)容都被緩存了PHP學(xué)習(xí)
第三步:改寫(xiě)Smarty_Compiler.class.php(注:該文件很重要,請(qǐng)先備份,以在必要時(shí)恢復(fù))PHP學(xué)習(xí)
查找:
PHP學(xué)習(xí)
修改成:PHP學(xué)習(xí)
if($tag_command == cacheless) $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, false); else $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);
你也可以直接將原句的最后一個(gè)參數(shù)改成false,即關(guān)閉默認(rèn)緩存.PHP學(xué)習(xí)
(3)使用register_function阻止插件從緩存中輸出PHP學(xué)習(xí)
index.tpl:PHP學(xué)習(xí)
<div>{current_time}{/div} index.php: function smarty_function_current_time($params, &$smarty){ return date("Y-m-d H:m:s"); } $smarty=new smarty(); $smarty->caching = true; $smarty->register_function('current_time','smarty_function_current_time',false); if(!$smarty->is_cached()){ ....... } $smarty->display('index.tpl');
注解:PHP學(xué)習(xí)
定義一個(gè)函數(shù),函數(shù)名格式為:smarty_type_name($params, &$smarty)
type為functionPHP學(xué)習(xí)
name為用戶(hù)自定義標(biāo)簽名稱(chēng),在這里是{current_time}PHP學(xué)習(xí)
兩個(gè)參數(shù)是必須的,即使在函數(shù)中沒(méi)有使用也要寫(xiě)上.兩個(gè)參數(shù)的功能同上.PHP學(xué)習(xí)
更多關(guān)于Smarty相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《smarty模板入門(mén)基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》PHP學(xué)習(xí)
希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所贊助.PHP學(xué)習(xí)
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/6528.html