色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php smarty模版引擎中的緩存應用

1,Smarty緩存的配置:
$smarty->cache-dir="目錄名"; //創建緩存目錄名
$smarty->caching=true; //開啟緩存,為false的時候緩存無效
$smarty->cache_lifetime=60; //緩存時間,單位是秒
2,Smarty緩存的使用與清除
$marty->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的內容如下:
<?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
已經緩存的:{$smarty.now}<br>
{cacheless}
沒有緩存的:{$smarty.now}
{/cacheless}
4自定義緩存
設置cache_handler_func使用自定義的函數處理緩存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$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來壓縮和解壓

php技術php smarty模版引擎中的緩存應用,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 葫芦岛市| 星子县| 和龙市| 宁晋县| 康定县| 玉环县| 舒兰市| 噶尔县| 基隆市| 志丹县| 武乡县| 南木林县| 河间市| 阳城县| 伊宁县| 本溪| 扶风县| 洛宁县| 白朗县| 铁力市| 禄劝| 六盘水市| 泗洪县| 水富县| 五河县| 东兴市| 淮滨县| 合水县| 高密市| 江城| 沁源县| 灵宝市| 香格里拉县| 无为县| 瓦房店市| 体育| 常熟市| 墨脱县| 谢通门县| 夏河县| 灵璧县|