1,Smarty快取的設定:
複製程式碼 程式碼如下:
$smarty->cache-dir="目錄名"; //建立快取目錄名稱
$smarty->caching=true; //開啟緩存,為false的時候快取無效
$smarty->cache_lifetime=60; //快取時間,單位是秒
2,Smarty快取的使用與清除
複製代碼 代碼如下: ->display("cache.tpl",cache_id); //建立帶有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_函數預設是不緩存,這個屬性是不能修改
使用方法:例子
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表示區域名稱
註冊block:$smarty->register_block('name', 'smarty_block_name', false); / /第三參數false表示該區域不被快取
模板寫法:{name}內容{/name}
寫成block外掛程式:
1)定義一件外掛程式:block.cacheless.php,放在smarty的plugins目錄
block.cacheless.php的內容如下:
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 編寫程式及模板
範例程式:testCacheLess.php
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display('cache.tpl');
?>
所使用的模板:cache.tpl
已經快取的:{$smarty.now}
{cacheless}
沒有快取的:{$smarty.now}
{/cacheless}
4自訂快取
設定cache_handler_func使用自訂的函式處理快取
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smarty_obj, &$smart cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
函數的一般是根椐$action來判斷快取目前運算:
switch($action){
case "read"://讀取快取內容
case "write"://寫入快取
case "clear"://清空
}
一般使用md5($tpl_file .$cache_id.$compile_id)作為唯一的cache_id
如果需要,可使用gzcompress和gzuncompress來壓縮和解壓
以上就介紹了eset smart security php smarty模版引擎中的快取應用,包括了eset smart security方面的內容,希望對PHP教程有興趣的朋友有所幫助。