标签:
PHP ob函数介绍:
<?php
/**
* ob函数
*/
ob_start(); //php.ini中未开启output_buffering
echo "content of buffer in memory<br/>";
echo ob_get_contents() . "<br/>"; //获取内存中的数据
echo ob_get_clean();//获取内存中的数据并且清空
file_put_contents("test.shtml", ob_get_clean());//写入文件
ob_clean();//清空内存数据
1、设置页面缓存时间
<?php
/**
* 缓存页面
*/
if (is_file("./index.shtml") && (time() - filemtime("./index.shtml")) < 10) {//存在静态文件而且时间小于5分钟,静态文件有效
require_once "./index.shtml";
} else {//静态文件失效(文件创建时间大于等于5分钟)或不存在
echo "content of buffer in memory";
file_put_contents("./index.shtml", ob_get_clean());
require_once "./index.shtml";
}
2、手动生成缓存
<?php
/**
* 直接生成静态缓存页面
*/
echo "content of buffer in memory";
file_put_contents("./index.shtml", ob_get_clean());
require_once "./index.shtml";
3、自动定时生成缓存
系统定时任务,linux - crom服务,windows - 任务计划程序。
*/1 * * * * /usr/bin/php-cgi /var/www/html/test.php (代表一分钟执行一次,前面的路径是php的路径,后面的是要执行脚本的路径)
标签:
原文地址:http://www.cnblogs.com/MrLenon/p/4323703.html