我一直在想一个问题,现在的网站大都实现静态化。Smarty3缓存还有用吗?有什么用。而且我觉得smarty3只是将PHP代码组合了一下。使的同样的功能在PHP跟smart3使用方法还不一样。这又是何必呢。先不说这些了。还是进入正题讲一下Smarty3缓存吧。
1、Smarty3缓存的配置
$smarty->cache_dir = “/cache/”; //设置缓存目录
$smarty->caching = 1; //开启smarty缓存
$smarty->cache_lifetime =3600; //缓存时间 默认是3600单位是秒
2、Smarty3缓存的使用和清除
$smarty->display(‘cache.tpl’, cache_id); //创建带ID的缓存
$smarty->clear_all_cache(); //清除所有缓存
$smarty->clear_cache(‘index.htm’); //清除index.tpl的缓存
$smarty->clear_cache(‘index.htm’,cache_id); //清除指定id的缓存
3、Smarty局部缓存使用方法
insert 函数默认是不缓存的。并且这个属性不能修改
index.htm
<div>{insert name=”get_time”}</div>
index.php
function insert_get_time(){
return date(“Y-m-d H:m:s”);
}
smarty_block 函数功能更加强大,使用方法同上
{blockname}
没有缓存的:{$smarty.now}
{/blockname}
$smarty->clear_all_cache();
实例 index.php
<?php
include_once ‘global.php’;
$id = $_GET['id'];
$name = array(‘a’=>”php”,’b'=>”jsp”,’c'=>”aspaaaa”);
function insert_shijian(){
//return date(“Y-m-d H:m:s”);
$a=’this change’;
return($a);
}
$smarty->cache_lifetime = 600; //缓存时间 单位是秒
$smarty->assign(‘name’,$name);
$smarty->assign(‘title’,'这是我的留言板首页’);$smarty->assign(‘id’,$id);
$smarty->display(‘index.html’,$id);
//$smarty->clear_all_cache();
//$smarty->clear_cache(‘index.html’,$id);
?>