cache是smarty区别于其他模板系统的一个很重要的地方:
1. 设置当前这个页面的缓存时间。
2. 设置当前这个页面的CacheID,模板编号可以使用“|”分组。
3. 判断当前页面有没有被缓存过,如果缓存过,则直接读取缓存文件,没有缓存或者缓存过期,则执行正常的逻辑查找数据,显示页面,同时生成缓存文件。
1.配置cache
include("Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="./templates";
$smarty->compile_dir="./templates_c";
$smarty->config_dir ="./config";
$smarty->caching=true;
2.使用cache
如果模板为index.html;则
if($smarty->is_cached("index.html"))
{
$smarty->display("index.html");
exit;
}
也可以用
$smarty->force_compile = false; (配置cache时)
来实现。
3.使用cache_id
$a=$_GET["a"];
if ($a=="about") {
$cache_id="about";$title="公司介绍";
}
else {
$cache_id="contact";$title="联系我们";
}
$smarty->assign('article',$article);
$smarty->caching = true; //缓存页面
$smarty->display('basic.tpl',$cache_id);
如果我这个模板basic.tpl的缓存ID有很多个呢?比如news.php?newsid=xxx, 缓存ID为newsid,如何进行全部清除呢?
$smarty->clear_cache("basic.tpl") //试过是不行的
$smarty->clear_all_cache(); //显然会把不想清的别的模板的缓存也给清空了。
现在我在后台对这个带参数的页面basic.php?a=about 或=contact
里所涉及到的东东进行修改更新数据库操作。当然这时候也要清空一下这个basic.tpl模板的所有缓存即缓存号为about和contact的两个缓存。
// 清除某一模板的多个缓存中指定缓存号的一个
$smarty->clear_cache("basic.tpl","about");
$smarty->clear_cache("basic.tpl","contact");
http://www.6070e.com/