最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP文件緩存smarty模板應(yīng)用實例分析

 更新時間:2016年02月26日 11:38:46   作者:不吃磚頭  
這篇文章主要介紹了PHP文件緩存smarty模板應(yīng)用方法,結(jié)合實例形式較為詳細的分析了smarty模板緩存的相關(guān)使用技巧,需要的朋友可以參考下

本文實例分析了PHP文件緩存smarty模板應(yīng)用。分享給大家供大家參考,具體如下:

一、使用緩存

要開啟smarty的緩存,只需將caching設(shè)為true,并指定cache_dir即可.
使用cache_lefetime指定緩存生存時間,單位為秒
要對相同頁面生成多個不同的緩存,在display或fetch中加入第二參數(shù)cache_id,如:

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

此特性可用于對不同的$_GET進行不同的緩存
 
二、清除緩存

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

三、使用自定義緩存方式

設(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)閉緩存

要在某些區(qū)域使緩存失效(只對需要的緩存),有幾種方法:

insert:

定義一個insert標(biāo)簽要使用的處理函數(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定義同上

register_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}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

灌南县| 中宁县| 阳高县| 万全县| 太谷县| 永春县| 临沂市| 扎囊县| 桓仁| 许昌县| 梓潼县| 浪卡子县| 桐庐县| 临夏市| 延长县| 松潘县| 鹤庆县| 弥渡县| 临湘市| 宁都县| 北辰区| 突泉县| 兴义市| 招远市| 沧州市| 澄江县| 夏津县| 布尔津县| 黄大仙区| 开远市| 常德市| 辽阳市| 灵川县| 贡山| 孝感市| 图片| 从化市| 石林| 辽源市| 岳阳市| 志丹县|