php-smarty模版引擎中的缓存应用
同学们在学习的时候一定要保持足够耐心,才能把知识学好。以下百分网小编整理的php smarty模版引擎中的缓存应用实例代码的介绍,希望对大家有所帮助,更多信息请关注应届毕业生网!
1,Smarty缓存的配置:
$smarty->cache-dir="目录名"; /pic/p>
$smarty->caching=true; /pic/p>
$smarty->cache_lifetime=60; /pic/p>
2,Smarty缓存的使用与清除
$marty->display("cache.tpl",cache_id); /pic/p>
$marty->clear_all_cache(); /pic/p>
$marty->clear_cache("index.php"); /pic/p>
$marty->clear_cache("index.php',cache_id); /pic/p>
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;} /pic/p>
注册block:$smarty->register_block('name', 'smarty_block_name', false); /pic/p>
模板写法:{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, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
该函数的一般是根椐$action来判断缓存当前操作:
switch($action){
case "read":/pic/p>
case "write":/pic/p>
case "clear":/pic/p>
}
一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和gzuncompress来压缩和解压
【php-smarty模版引擎中的缓存应用】相关文章:
PHP 中九大缓存技术总结11-01
PHP中9大缓存技术总结02-11
CPU缓存的作用11-24
PHP缓存技术11-17
java的缓存机制03-06
cpu缓存是什么09-11
php缓存技术经典总结09-12
java的缓存机制简介12-17
- 相关推荐