PHP smarty缓存
2016-07-28 14:27
543 查看
缓存一个页面
test.php
test.html
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728141922950-665891509.png)
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728141942700-1561953393.png)
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728142033184-776553284.png)
分页缓存
testa.php
testa.heml
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728142519059-1651099173.png)
test.php
<?php //定义该页面缓存文件存放的路径 $filename = "../cache/cachetest.html"; //定义缓存有效期 $cachetime = 5; //判断缓存文件是否存在 if(!file_exists($filename) || filemtime($filename)+$cachetime<time()) { //开启内存缓存 ob_start(); include("../init.inc.php"); include("../DBDA.php"); $db = new DBDA(); $sql = "select * from car"; $attr = $db->Query($sql); $smarty->assign("car",$attr); $smarty->display("test.html"); //从内存缓存中获取页面代码 $content = ob_get_contents(); //将获取到的内容存放到缓存文件 file_put_contents($filename,$content); //清掉内存缓存 ob_flush(); echo "######################################"; } else { include($filename); }
test.html
<body> <h1>汽车信息</h1> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>汽车名称</td> <td>油耗</td> <td>价格</td> </tr> <{foreach $car as $v}> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td><{$v[4]}></td> <td><{$v[7]}></td> </tr> <{/foreach}> </table> </body>
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728141922950-665891509.png)
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728141942700-1561953393.png)
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728142033184-776553284.png)
分页缓存
testa.php
<?php //取当前页 $p=1; if(!empty($_GET["page"])) { $p = $_GET["page"]; } //定义缓存文件存放路径 $filename = "../cache/cahetesta{$p}.html"; //判断 if(!file_exists($filename) || filemtime($filename)+30<time()) { ob_start(); include("../init.inc.php"); include("../DBDA.php"); $db = new DBDA(); include("page.class.php"); $szs = "select count(*) from car"; $zs = $db->StrQuery($szs); $page = new Page($zs,5); $xinxi = $page->fpage(); $sql = "select * from car ".$page->limit; $attr = $db->Query($sql); $smarty->assign("car",$attr); $smarty->assign("xinxi",$xinxi); $smarty->display("testa.html"); $nr = ob_get_contents(); file_put_contents($filename,$nr); ob_flush(); echo "################################################"; } else { include($filename); }
testa.heml
<body> <h1>汽车信息</h1> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>汽车名称</td> <td>油耗</td> <td>价格</td> </tr> <{foreach $car as $v}> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td><{$v[4]}></td> <td><{$v[7]}></td> </tr> <{/foreach}> </table> <div><{$xinxi}></div> </body>
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728142519059-1651099173.png)
![](https://images2015.cnblogs.com/blog/904031/201607/904031-20160728142532309-1398282111.png)
相关文章推荐
- phpexcel导出超过26列解决方案
- php魔术方法
- php不用递归完成无限分类,从表设计入手完整演示过程
- 开源内容管理系统Joomla3.5发布 基于PHP 7
- 今天开始重新更新(ps:广接各种php相关的外包项目,有意者请留言或者私聊)
- Yii2.0 PHP使用Sphinx
- PHP header()函数常用方法
- **YII** 系列四数据库访问
- Matplotlib输出中文显示问题
- [ Laravel 5.1 文档 ] 服务 —— 任务调度
- php数组遍历
- PHP分词插件Demo
- php学习3----上传原理
- php实现带读写分离功能的MySQL类完整实例
- php学习2----分页原理
- PHP实现带重试功能的curl连接示例
- php学习1-----制作PHP留言板:
- php的存储过程
- PHP 入门学习笔记(一)
- TP框架常用配置