单页面、多缓存

本文介绍了一个使用Smarty模板引擎实现动态内容静态化的案例。通过PHP的Smarty组件进行页面缓存,减少数据库查询次数,提高网站加载速度。文章详细展示了如何根据不同URL参数生成对应的缓存文件,并通过具体代码示例说明了整个过程。

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

2012-11-15

概念:

为了处理同样的显示界面,不同的显示内容。内容有url参数决定

。同一个模板可以生成多个缓存文件(有多少中内容显示就有多少个缓存

文件),多个缓存缓存文件用display("*tpl",$_GET["id"]);在PHP中可

以用is_cached判断模板是否被缓存,is_cached格式和display完全一样

 

做的实例:

init.inc.php文件

<?php
include "libs/Smarty.class.php";
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/templates_c";
$smarty->config_dir="demo/config";
$smarty->cache_dir="demo/cache";
?>

-----------------------------------------------------

Stu.php文件

<?php
include "init.inc.php";

$smarty->caching=1;
if(!$smarty->is_cached("Stu2.tpl")){

mysql_connect('localhost','root','123') or die('连接数据库失败!');
mysql_select_db('xx');
mysql_query("set names utf8");
$q=mysql_query("select * from stu");
$arrqy=array();
$i=0;
while($row=mysql_fetch_assoc($q)){
 $array[$i]=$row;
 $i++;
 }
//var_dump($array);

$smarty->assign('array',$array);}
$smarty->display("Stu.tpl");
?>

---------------------------------------------

Stu.tpl文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>各学生详细信息</title>
</head>

<body>

<table border="1">
<tr><th>学号</th><th>姓名</th><th>出生日期</th></tr>

<{section name=stu loop=$array}>
<tr>
<td><{$array[stu].id}></td>
<td><{$array[stu].name}></td>
<td><{$array[stu].birthday}></td>
<td><a href="Stu2.php?id=<{$array[stu].id}>">详细信息</a></td>
</tr>
<{/section}>

</table>

</body>
</html>

---------------------------------------------------------------

Stu2.php文件

<?php
include "init.inc.php";
$smarty->caching=1;
if(!$smarty->is_cached("Stu2.tpl")){
mysql_connect('localhost','root','123') or die('连接数据库失败!');
mysql_select_db('xx');
mysql_query("set names utf8");
$q=mysql_query('select * from stu where id='.$_GET['id']);
$array=array();
$i=0;
while($row=mysql_fetch_assoc($q)){
 $array[$i]=$row;
 $i++;
 }


//$smarty->assign('arr',$array);

$smarty->assign("a",$array);
echo "没有缓存设置、";
}


$smarty->display("Stu2.tpl"); 
?>

-------------------------------------------------------------

Stu2.tpl文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><{section name='cao' loop=$a}>
<{$a[cao].name}>的详细信息<{/section}>
</title>
</head>

<body>

<{section name='cao' loop=$a}>
学号:<{$a[cao].id}><br />
姓名:<{$a[cao].name}><br />
生日:<{$a[cao].birthday}><br />
<{/section}>
<a href="Stu.php">返回首页</a>

</body>
</html>

 

Edge浏览器(Microsoft Edge)允许用户手动清除特定网页或整个浏览数据的缓存,以提高性能并释放磁盘空间。以下是清除单个页面缓存的步骤: 1. **Windows**: - 打开 Microsoft Edge,点击右上角的三个点(设置图标)打开设置菜单。 - 滚动到“隐私、搜索和服务”部分,点击“网站设置” > “历史记录和Browsing Data”。 - 在左侧菜单中选择“图片、文件和其他媒体”,然后在右侧找到“选择要删除的内容”。 - 在时间范围里选择“自定义日期”,然后输入你想清除的特定页面加载日期,最后点击“删除”。 2. **Mac**: - 点击左上角的苹果菜单,选择“系统偏好设置” > “Safari”。 - 点击“高级”选项卡,在“存储”下拉菜单中,取消选中“保留Web内容的历史记录”。 - 回到“常规”标签页,如果需要针对某个特定网站清除缓存,按住`Command + Shift + K`组合键会打开开发者工具,选择“缓存”选项卡,然后删除对应页面。 3. **Clear Cache Manually (通过命令行)**: Windows 用户可以使用任务管理器中的“存储”标签来查看和删除缓存,但这不是专门针对Edge的。Mac用户可以通过终端运行`rm -rf ~/Library/Caches/com.microsoftedge*`来尝试清除Edge缓存,但请谨慎操作,因为可能会误删其他应用的缓存。 请注意,定期清理缓存可以帮助提升浏览速度,但也可能导致某些网站个性化设置丢失。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值