缓存

1.什么是缓存(cache)?
内存(临时存放数据,解决cpu和外部设备之间速度不匹配)
高速缓存(cache)(解决cpu和内存之间速度不匹配的问题)
作用:提高计算机数据的访问速度
网站设计中(软件开发中)
缓存的概念:在PHP中,缓存就是就是硬盘中的一块区域,将生成的不变的页面放入这块区域中,相当于直接放入了缓存。当用户重复访问相同页面的时候,直接从缓存区域调用页面。
2.如何应用缓存(smarty中)

(1)单页面单缓存(建立缓存)
步骤1:开启缓存($smarty->caching=true|$smarty->caching=2设置lifetime)
步骤2:设置缓存目录($smarty->cache_dir='')
步骤3:设置缓存文件的生存时间($smarty->cache_lifetime)
步骤4:利用display()或fetch()生成缓存文件

(2).单页面 多缓存
为了处理同样的显示界面,不同的显示内容。内容有url参数决定。同一个模板可以生成多个缓存文件(有多少中内容显示就有多少个缓存文件),多个缓存缓存文件用display("*tpl",$_GET["id"]);在PHP中可以用is_cached判断模板是否被缓存,is_cached格式和display完全一样。

eg.connect_mysql.php //这是链接数据库的文件

<?php
$stu=mysql_connect('localhost','root','');
mysql_select_db('xsxx',$stu);
mysql_query('set names utf8');
?>

stu_info.php  //这是从数据库获取的所有数据以表格输出的php文件他是加载的stu_info.tpl的模板

<meta charset='utf-8'>
<?php
include("connect_mysql.php");
include("../libs/Smarty.class.php");

    $smarty=new Smarty();//初始化对象
$smarty->template_dir="../demo/templates";//设置模版目录
$smarty->compile_dir="../demo/templates_c";//设置编译目录
//***********************************
//设置定界符
//*************************
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>" ;
$smarty->caching=1;
$smarty->cache_dir="../demo/caching";
$smarty->config_dir="../demo/config";
if(!$smarty->is_cached('stu_info.tpl')){
    $q="select *from stu";
mysql_query("set names utf8");
$result=mysql_query($q);
$i=0;
$array=array();
while($row=mysql_fetch_assoc($result)){
    $array[$i]=$row;
    $i++;
    }
    $smarty->assign("array",$array);
    echo"当前模版没有被缓存";
    }
//var_dump($array);
$smarty->display("stu_info.tpl");
?>

datails.php  // 这是根据stu.info.tpl中的href="datails.php?id=<{$array[stu].id}的链接跳转到此页面

<?php
include ('connect_mysql.php');
include("../libs/Smarty.class.php");

    $smarty=new Smarty();//初始化对象
$smarty->template_dir="../demo/templates";//设置模版目录
$smarty->compile_dir="../demo/templates_c";//设置编译目录
//***********************************
//设置定界符
//*************************
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>" ;
$smarty->caching=1;
$smarty->cache_dir="../demo/caching";
$smarty->config_dir="../demo/config";
if(!$smarty->is_cached("stu_details.tpl",$_GET['id'])){
$q="select *from stu where id=".$_GET['id'];
$result=mysql_query($q);
$array=array();
$i=0;
while($row=mysql_fetch_assoc($result)){
    $array[$i]=$row;
    $i++;
    }
    $smarty->assign("stu_details",$array);
    echo "缓存没有被设置";
    }
$smarty->display("stu_details.tpl",$_GET['id']);
?>

stu_info.tpl

<{config_load file="a.conf"}>
<!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>
<center>
<table border="1"  bgcolor="<{#table_bgcolor#}>">
<tr bgcolor="<{#head_color#}>">
<th>学号</th><th>姓名</th><th>出生日期</th><th>成绩</th><th>信息</th>
</tr>
<{section name=stu loop=$array}>
<{if $smarty.section.stu.index is odd}>
<tr bgcolor="<{#row_color#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].sname}></td>
<td><{$array[stu].birthday}></td>
<td><{$array[stu].score}></td>
<td><a href="datails.php?id=<{$array[stu].id}>">细信息</a></td></tr>
<{else}>
<tr bgcolor="<{#row_color2#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].sname}></td>
<td><{$array[stu].birthday}></td>
<td><{$array[stu].score}></td>
<td><a href="datails.php?id=<{$array[stu].id}>">细信息</a></td>
</tr>
<{/if}>
<{/section}>
</table>

</center>
</body>
</html>
stu_details.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>
<{section name=stu_detail loop=$stu_details}>
<h1><{$stu_detaails[stu_detail].sname}>的详细信息</h1>
<hr />
学号:<{$stu_details[stu_detail].id}><br />
姓名:<{$stu_details[stu_detail].sname}><br />
出生日期:<{$stu_details[stu_detail].birthday}><br />
成绩:<{$stu_details[stu_detail].score}><br />
<{/section}>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值