<a style="CURSOR: pointer" data="38782" class="copybut" id="copybut38782" onclick="doCopy('code38782')"> 代码如下:<div class="codebody" id="code38782"><?php
page_init();//页面缓存初始化
ob_start();//开启缓存
...//代码段
page_cache(60);//一般是最后一行
?>
例2,下面做个示例来说明PHP页面缓存技术,代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="78303" class="copybut" id="copybut78303" onclick="doCopy('code78303')"> 代码如下:<div class="codebody" id="code78303"><?php
$_time =10;
$dir="D:php";
function cache_start($_time,$dir)
{
$cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
$cachetime = $_time;
ob_start();
if(file_exists($cachefile) && (time()-filemtime($cachefile) < $cachetime))
{
include($cachefile);
ob_end_flush();
exit;
}
}
function cache_end($dir)
{
$cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
$fp = fopen($cachefile,'w');
fwrite($fp,ob_get_contents());
fclose($fp);
ob_end_flush();
}
cache_start($_time,$dir);
//以下是输出的内容,放在cache_start和cache_end两个方法之间
for ($i=0;$i<5;$i++)
{
echo $i;
sleep(1);
}
cache_end($dir);
?>
例,利用生成文件做缓存,代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="59740" class="copybut" id="copybut59740" onclick="doCopy('code59740')"> 代码如下:<div class="codebody" id="code59740"><?php
ob_start();
/**
- @author 何名慧
- @copyright 2009-3-13
- @param string $cache_folder 缓文件夹
- @param int $cache_create_time 文件缓存时间
- @example $cache=new Esj_Cache('./_cache',100)
- @example $cache->read_cache() 读取缓存并输出
- @example $cache->creatre_cache() 创建缓存文件(放在文件未尾)
- @example $cache->list_file() 返回所有缓存文件列表
- @example $cache->del_file() 删除所有缓存文件
*/
class Esj_Cache{
private $cache_folder=null;//cacher文件夹
private $wroot_dir=null;//站点目录
private $cacher_create_time=null;//cacher文件的建立时间
public function __construct($cache_foldername,$cacher_time=100)
{
ob_start();
$this->wroot_dir=$_SERVER['DOCUMENT_ROOT'];
$this->cache_folder=$cache_foldername;
$this->cacher_create_time=$cacher_time;
}
public function read_cache()
{
try {
if(self::create_folder($this->cache_folder))
{
self::get_cache();//输出缓存文件信息
}else
{
echo "缓存文件夹创建失败!";
return false;
}
}catch(Exception $e){
echo $e;
return false;
}
}
//测试缓存文件夹是否存在
private function exist_folder($foler)
{
if(file_exists($this->wroot_dir."/".$foler)){
return true;
}else {
return false;
}
}
//建立一个新的文件夹
private function create_folder($foler)
{
if(!self::exist_folder($foler))
{
try{
mkdir($this->wroot_dir."/".$foler,0777);
chmod($this->wroot_dir."/".$foler,0777);
return true;
}catch (Exception $e)
{
self::get_cache();//输出缓存
return false;
}
return false;
}
else
{
return true;
}
}
//读取缓存文件
private function get_cache()
{
$file_name=self::get_filename();
if (file_exists($file_name)&&((filemtime($file_name)+$this->cacher_create_time) > time()))
{
$content=file_get_contents($file_name);
if($content)
{
echo $content;
ob_end_flush();
exit;
}else
{
echo "文件读取失败";
exit;
}
}
}
//返回文件的名字
private function get_filename()
{
$filename=$file_name=$this->wroot_dir.'/'.$this->cache_folder.'/'.md5($_SERVER['QUERY_STRING']).".html";
return $filename;
}
//建立缓存文件
public function create_cache()
{
$filename=self::get_filename();
if($filename!="")
{
try{
file_put_contents($filename,ob_get_contents());
return true;
}catch (Exception $e)
{
echo "写缓存失败:".$e;
exit();
}
return true;
}
}
// 取得缓存中的所有文件
public function list_file()
{
$path=$this->cache_folder;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if($file!="." && $file!="..") {
$path1=$path."/".$file;
if(file_exists($path1))
{
$result[]=$file;
}
}
}
closedir($handle);
}
return $result;
}
//删除缓存中的所有文件
public function del_file()
{
$path=$this->cache_folder;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if($file!="." && $file!="..") {
$path1=$path."/".$file;
if(file_exists($path1))
{
unlink($path1);
}
}
}
closedir($handle);
}
return true;
}
}
?>
希望本文所述对大家的php程序设计有所帮助。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!