静态化
2016-01-16 10:33
225 查看
写了一个demo,供大家参考,具体代码如下:
header("content-type:text/html;charset=utf8");
// 拼接文件路径.
$dir = "./cache/";
if ( !is_dir($dir) ) {
mkdir($dir);
}
$url = $dir . intval($_GET['id']);
// 文件是否存在,存在直接读取并输出文件.
if (file_exists($url)) {
// 文件创建时间.
$ctime = filemtime($url);
// 文件是否过期,过期删除.
if(time() - $ctime > 36) {
unlink($url);
} else {
echo file_get_contents($url);
echo 1;
exit;
}
}
// 开启缓冲区.
ob_start();
// PDO链接mysql读取数据.
$db = new PDO("mysql:host=127.0.0.1;dbname=test", 'root', '123456');
$re = $db->query("select * from bank");
$info = $re->fetchAll(PDO::FETCH_ASSOC);
// 展示页面.
?>
<html>
<head>
<title>静态化</title>
</head>
<body>
<table cellspacing="0" border="1">
<tr>
<th>name</th>
<th>money</th>
</tr>
<?php foreach ($info as $v) { ?>
<tr>
<td><?php echo $v['name']; ?></td>
<td><?php echo $v['money']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
// 获取缓冲区内容.
$mess = ob_get_contents();
// 关闭缓冲区并清空缓冲区内容
ob_end_clean();
/**
* ob_clean 清空缓冲区内容.
* ob_end_clean 关闭并清空缓冲区内容.
* ob_flush 将缓冲区内容输出并清空缓冲区.
* ob_end_flush 关闭将缓冲区内容输出并清空缓冲区.
*/
echo $mess;
// 缓冲区内容写入文件.
file_put_contents($url, $mess);
echo 2;
直接访问 就能看到想要的结果了。
只是提供一个参考方案,具体请根据实际业务进行实践!!!
header("content-type:text/html;charset=utf8");
// 拼接文件路径.
$dir = "./cache/";
if ( !is_dir($dir) ) {
mkdir($dir);
}
$url = $dir . intval($_GET['id']);
// 文件是否存在,存在直接读取并输出文件.
if (file_exists($url)) {
// 文件创建时间.
$ctime = filemtime($url);
// 文件是否过期,过期删除.
if(time() - $ctime > 36) {
unlink($url);
} else {
echo file_get_contents($url);
echo 1;
exit;
}
}
// 开启缓冲区.
ob_start();
// PDO链接mysql读取数据.
$db = new PDO("mysql:host=127.0.0.1;dbname=test", 'root', '123456');
$re = $db->query("select * from bank");
$info = $re->fetchAll(PDO::FETCH_ASSOC);
// 展示页面.
?>
<html>
<head>
<title>静态化</title>
</head>
<body>
<table cellspacing="0" border="1">
<tr>
<th>name</th>
<th>money</th>
</tr>
<?php foreach ($info as $v) { ?>
<tr>
<td><?php echo $v['name']; ?></td>
<td><?php echo $v['money']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
// 获取缓冲区内容.
$mess = ob_get_contents();
// 关闭缓冲区并清空缓冲区内容
ob_end_clean();
/**
* ob_clean 清空缓冲区内容.
* ob_end_clean 关闭并清空缓冲区内容.
* ob_flush 将缓冲区内容输出并清空缓冲区.
* ob_end_flush 关闭将缓冲区内容输出并清空缓冲区.
*/
echo $mess;
// 缓冲区内容写入文件.
file_put_contents($url, $mess);
echo 2;
直接访问 就能看到想要的结果了。
只是提供一个参考方案,具体请根据实际业务进行实践!!!
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
相关文章推荐
- 浅析SQL Server中的执行计划缓存(上)
- Enterprise Library for .NET Framework 2.0缓存使用实例
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- C#中缓存的基本用法总结
- wap开发中如何有效的利用缓存减少消息的传送量
- PHP基于文件存储实现缓存的方法
- smarty缓存用法分析
- 网页的HTML静态化_网站优化之谈
- 引用全局程序集缓存内的程序集的方法
- asp Response.flush 实时显示进度
- C#实现清除IE浏览器缓存的方法
- ASP.NET缓存管理的几种方法
- PHP文件缓存类实现代码
- 清除aspx页面缓存的程序实现方法
- C#缓存之SqlCacheDependency用法实例总结
- jQuery数据缓存用法分析
- Jquery validation remote 验证的缓存问题解决方法
- cos-html-cache关于WordPress的静态化方法
- IE9下Ajax无法刷新数据的缓存问题解决方法