php smarty模版引擎中的緩存應(yīng)用
更新時間:2009年12月02日 00:25:41 作者:
php中smarty模版引擎中的緩存應(yīng)用實(shí)現(xiàn)代碼,需要的朋友可以參考下。
1,Smarty緩存的配置:
$smarty->cache-dir="目錄名"; //創(chuàng)建緩存目錄名
$smarty->caching=true; //開啟緩存,為false的時候緩存無效
$smarty->cache_lifetime=60; //緩存時間,單位是秒
2,Smarty緩存的使用與清除
$marty->display("cache.tpl",cache_id); //創(chuàng)建帶ID的緩存
$marty->clear_all_cache(); //清楚所有緩存
$marty->clear_cache("index.php"); //清楚index.php中的緩存
$marty->clear_cache("index.php',cache_id); //清楚index.php中指定ID的緩存
3,Smarty的局部緩存
第一個: insert_函數(shù)默認(rèn)是不緩存,這個屬性是不能修改
使用方法:例子
index.php中,
function insert_get_time(){
return date("Y-m-d H:m:s");
}
index.html中,
{insert name="get_time"}
第二個: smarty_block
定義一個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插件:
1)定義一件插件函數(shù):block.cacheless.php,放在smarty的plugins目錄
block.cacheless.php的內(nèi)容如下:
<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 編寫程序及模板
示例程序: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}
4自定義緩存
設(shè)置cache_handler_func使用自定義的函數(shù)處理緩存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
該函數(shù)的一般是根椐$action來判斷緩存當(dāng)前操作:
switch($action){
case "read"://讀取緩存內(nèi)容
case "write"://寫入緩存
case "clear"://清空
}
一般使用md5($tpl_file.$cache_id.$compile_id)作為唯一的cache_id
如果需要,可使用gzcompress和gzuncompress來壓縮和解壓
復(fù)制代碼 代碼如下:
$smarty->cache-dir="目錄名"; //創(chuàng)建緩存目錄名
$smarty->caching=true; //開啟緩存,為false的時候緩存無效
$smarty->cache_lifetime=60; //緩存時間,單位是秒
2,Smarty緩存的使用與清除
復(fù)制代碼 代碼如下:
$marty->display("cache.tpl",cache_id); //創(chuàng)建帶ID的緩存
$marty->clear_all_cache(); //清楚所有緩存
$marty->clear_cache("index.php"); //清楚index.php中的緩存
$marty->clear_cache("index.php',cache_id); //清楚index.php中指定ID的緩存
3,Smarty的局部緩存
第一個: insert_函數(shù)默認(rèn)是不緩存,這個屬性是不能修改
使用方法:例子
index.php中,
function insert_get_time(){
return date("Y-m-d H:m:s");
}
index.html中,
{insert name="get_time"}
第二個: smarty_block
定義一個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插件:
1)定義一件插件函數(shù):block.cacheless.php,放在smarty的plugins目錄
block.cacheless.php的內(nèi)容如下:
<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 編寫程序及模板
示例程序: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}
4自定義緩存
設(shè)置cache_handler_func使用自定義的函數(shù)處理緩存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
該函數(shù)的一般是根椐$action來判斷緩存當(dāng)前操作:
switch($action){
case "read"://讀取緩存內(nèi)容
case "write"://寫入緩存
case "clear"://清空
}
一般使用md5($tpl_file.$cache_id.$compile_id)作為唯一的cache_id
如果需要,可使用gzcompress和gzuncompress來壓縮和解壓
相關(guān)文章
php SQLite學(xué)習(xí)筆記與常見問題分析
php SQLite學(xué)習(xí)資料收集,與一些常見問題的解決方法2008-07-07
手把手教你打印出PDF(關(guān)于fpdf的簡單應(yīng)用)
本篇文章是對關(guān)于fpdf的簡單應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
關(guān)于URL最大長度限制的相關(guān)資料查證
這篇文章主要介紹了關(guān)于URL最大長度限制的相關(guān)資料查證,這里記錄一下,方便以后使用。2014-12-12
phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
這篇文章主要介紹了phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法,本文的原因是服務(wù)器的安全設(shè)置造成,服務(wù)器中屏蔽fsockopen函數(shù)的使用權(quán)限,所以導(dǎo)致發(fā)送失敗,需要的朋友可以參考下2014-07-07
利用phpExcel實(shí)現(xiàn)Excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(全步驟詳細(xì)解析)
本人的這段例程是使用在Thinkphp的開發(fā)框架上,要是使用在其他框架也是同樣的方法,很多人可能不能正確的實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出,問題基本上都是phpExcel的核心類引用路徑出錯,如果有問題大家務(wù)必要對路勁是否引用正確進(jìn)行測試2013-11-11
php中的標(biāo)量數(shù)據(jù)類型總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于php中的標(biāo)量數(shù)據(jù)類型總結(jié)內(nèi)容,對此有興趣的朋友們可以跟著學(xué)習(xí)下。2022-01-01
最新用php獲取谷歌PR值算法,附上php查詢PR值代碼示例
用php程序怎么獲取谷歌PR值,已經(jīng)有好幾個人問我php查詢PR值怎么實(shí)現(xiàn)的,于是現(xiàn)在就把php查詢PR值法算法附上,大家可以直接使用下面的代碼去測試php查詢PR值2011-12-12

