Smarty模板引擎緩存機制詳解
來源:易賢網(wǎng) 閱讀:827 次 日期:2016-08-19 15:00:25
溫馨提示:易賢網(wǎng)小編為您整理了“Smarty模板引擎緩存機制詳解”,方便廣大網(wǎng)友查閱!

本文實例講述了Smarty模板引擎緩存機制。分享給大家供大家參考,具體如下:

首先說下smarty緩存和編譯,這是兩個不同的概念,編譯默認情況下是啟動的,而緩存機制需要人為開啟,smarty編譯過的文件還是php文件,所以執(zhí)行的時候還是編譯的,如果涉及到數(shù)據(jù)庫,還是要訪問數(shù)據(jù)庫的所以開銷也不小啦,所以需要smarty緩存來解決!

1.開啟全局緩存

$smarty->cache_dir = "/caches/"; //緩存目錄

$smarty->caching = true; //開啟緩存,為flase的時侯緩存無效

$smarty->cache_lifetime = 3600; //緩存時間

2.一個頁面使用多個緩存

如:一個文章模板頁面會生成多個文章頁面,當然是緩存成很多頁面,實現(xiàn)起來很簡單,只要在display()方法設置第二個參數(shù),指定唯一標識符即可。如下php代碼:

$smarty->display('index.tpl',$_GET["article_id"]);

如上,通過第二個參數(shù)文章的id緩存一個文章頁面。

3.為緩存減小開銷

也就是說,已經(jīng)緩存的頁面無需進行數(shù)據(jù)庫的操作處理了,可通過is_cached()方法判斷!

if(!$smarty->is_cached('index.tpl')){

 //調(diào)用數(shù)據(jù)庫

}

$smarty->display('index.tpl');

4.清除緩存

一般在開發(fā)過程中是不開啟緩存的,因為在緩存時間內(nèi)輸出結(jié)果不變,但是在應用過程中開啟緩存能大大提高web性能,清除緩存方法如下:

clear_all_cache();//清除所有緩存

clear_cache('index.tpl');//清除index.tpl的緩存

clear_cache('index.tpl',cache_id);//清除指定id的緩存

5.關(guān)閉局部緩存

如果一個頁面中一部分緩存,而另一部分不需要緩存,就可以這樣做,比如說顯示用戶登錄的名稱就需要關(guān)閉緩存,smarty提供了如下三種解決方法:

(1)使用insert模板的一部分不被緩存

定義一個inser標簽要使用的處理函數(shù),函數(shù)名格式為:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是說,如果你定義的函數(shù)為insert_abc,則模板中使用方法為{insert name=abc}

參數(shù)通過$params傳入

也可以做成insert插件,文件名命名為:insert.xx.php,函數(shù)命名為:smarty_insert_aa($params,&$smarty),xx定義同上

(2)$smarty->register_block($params, &$smarty)使整篇頁面中的某一塊不被緩存

定義一個block:

smarty_block_name($params,$content, &$smarty){return $content;} 

//name表示區(qū)域名

注冊block:

$smarty->register_block(name, smarty_block_name, false);

//第三參數(shù)false表示該區(qū)域不被緩存

模板寫法:

{name}內(nèi)容 {/name}

寫成block插件:

第一步:定義一件插件函數(shù):block.cacheless.php,放在smarty的 plugins目錄

block.cacheless.php的內(nèi)容如下:

<?php

function smarty_block_cacheless($param, $content, &$smarty) {

return $content;

}

?>

第二步:編寫程序及模板

示例程序:testCacheLess.php

<?php

include(Smarty.class.php);

$smarty = new Smarty;

$smarty->caching=true;

$smarty->cache_lifetime = 6;

$smarty->display(cache.tpl);

?>

所用的模板:cache.tpl

已經(jīng)緩存的:{$smarty.now}<br>

{cacheless}

沒有緩存的:{$smarty.now}

{/cacheless}

現(xiàn)在運行一下,發(fā)現(xiàn)是不起作用的,兩行內(nèi)容都被緩存了

第三步:改寫Smarty_Compiler.class.php(注:該文件很重要,請先備份,以在必要時恢復)

查找:

復制代碼 代碼如下:

$this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);

修改成:

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)閉默認緩存。

(3)使用register_function阻止插件從緩存中輸出

index.tpl:

<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');

注解:

定義一個函數(shù),函數(shù)名格式為:smarty_type_name($params, &$smarty)

type為function

name為用戶自定義標簽名稱,在這里是{current_time}

兩個參數(shù)是必須的,即使在函數(shù)中沒有使用也要寫上。兩個參數(shù)的功能同上。

希望本文所述對大家基于smarty模板的PHP程序設計有所幫助。

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:Smarty模板引擎緩存機制詳解
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)