Smarty之单模板多缓存,删缓存,不缓存

本文介绍了如何在Smarty中实现单模板针对不同ID生成多个缓存页面,以商品页面为例,通过在`display`和`isCached`方法中加入goods_id参数来区分缓存。同时,讲解了删除缓存的方法,如使用`clearCache`函数配合goods_id清除特定商品的缓存,以及如何通过设置`force_cache`为true来强制不缓存当前请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单模板多缓存

根据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);

?>


html

<!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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值