php smarty模版引擎中的緩存應(yīng)用
更新時間:2009年12月02日 00:25:41 作者:
php中smarty模版引擎中的緩存應(yīng)用實現(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)文章
不用mod_rewrite直接用php實現(xiàn)偽靜態(tài)化頁面代碼
不用mod_rewrite直接用php代碼實現(xiàn)偽靜態(tài)效果,大家看后就會發(fā)現(xiàn)php真的很方便2008-10-10PHP面向?qū)ο蟪绦蛟O(shè)計之類與反射API詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計之類與反射API,結(jié)合實例形式較為詳細(xì)的分析了類的驗證、檢查、傳參及反射API等概念與操作技巧,需要的朋友可以參考下2016-12-12php+dojo 的數(shù)據(jù)庫保存拖動布局的一個方法dojo 這里下載
php+dojo 的數(shù)據(jù)庫保存拖動布局的一個方法dojo 這里下載...2007-03-03PHP實現(xiàn)redis限制單ip、單用戶的訪問次數(shù)功能示例
這篇文章主要介紹了PHP實現(xiàn)redis限制單ip、單用戶的訪問次數(shù)功能,結(jié)合實例形式分析了php連接redis及獲取、記錄客戶端信息,并限制客戶訪問次數(shù)等操作技巧,需要的朋友可以參考下2018-06-06php foreach 參數(shù)強制類型轉(zhuǎn)換的問題
大家都知道foreach的參數(shù)如果不是數(shù)組類型,在運行的時候 就會出現(xiàn)類似“Warning: Invalid argument supplied for foreach() in XXX”warning信息。2010-12-12php學(xué)習(xí)筆記之字符串常見操作總結(jié)
這篇文章主要介紹了php學(xué)習(xí)筆記之字符串常見操作,結(jié)合實例形式總結(jié)分析了php字符串的定義、單引號與雙引號的用法以及常見字符串操作函數(shù)使用技巧,需要的朋友可以參考下2019-07-07php 按指定元素值去除數(shù)組元素的實現(xiàn)方法
php 按指定元素值去除數(shù)組元素的實現(xiàn)方法,需要的朋友可以參考下。2011-11-11PHP實現(xiàn)的memcache環(huán)形隊列類實例
這篇文章主要介紹了PHP實現(xiàn)的memcache環(huán)形隊列類,實例分析了基于memcache實現(xiàn)環(huán)形隊列的方法,涉及memcache緩存及隊列的相關(guān)技巧,需要的朋友可以參考下2015-07-07