单模板多缓存
根据id的不同生成不同的的缓存页面;
例如商品页,一个模板 goods_id不同,取出商品不同
但普通缓存手法,就商品不变了
解决方法:display和isCached里添加goods_id参数
<?php
/*单模板 多缓存
display和isCached里添加goods_id参数*/
require('../../smarty3/libs/Smarty.class.php');
require('./mysmarty.class.php');
$smarty=new MySmarty();
$smarty->caching=true;
$smarty->cache_lifetime=20;
$smarty->cache_dir='./cache';
// 根据goods_id保存不同的页面缓存
$goods_id=$_GET['goods_id']+0;
//取出一个商品,[商品页]
if(!$smarty->isCached('temp7.html',$goods_id)){
$conn=mysqli_connect('localhost','root','123456','boolshop');
$sql ='set names utf8';
mysqli_query($conn,$sql);
$sql ='select * from goods where goods_id='.$goods_id;
$rs=mysqli_query($conn,$sql);
// 取出单行
$row=mysqli_fetch_assoc($rs);
$smarty->assign('goods',$row);
echo '我不是缓存哟~ o(* ̄▽ ̄*)ブ ';
}
$smarty->display('temp7.html',$goods_id);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>temp7</title>
</head>
<body>
<table>
<tr><td>商品号</td><td>商品名</td></tr>
<tr><td>{$goods.goods_id}</td><td>{$goods.goods_name}</td></tr>
</table>
</body>
</html>
删缓存
//删缓存,2个参数,模板名,缓存id;如果不加id就把这个模板的缓存都删了
$smarty->clearCache('temp7.html',$goods_id);
不缓存
//加了这行强制不缓存开启
$smarty->force_cache=true;