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