日期:2011-08-16  浏览次数:20441 次

有些php网站由于前期设计以及其它原因 
网站是纯动态或者是大部分是动态的,对数据库的操作很频繁. 
造成网站访问速度很慢!想实现静态,又没有生成静态功能. 
因为我写了一个精简的生成静态的程序.很简单,一看就懂 
适合基本上所有系统 

要求 
1.系统支持php 
2.支持file_get_contents file_put_contents [也可以用同样作用的函数替换] 
3.最好支持gzcompress 

使用方法 
1.下载下面分享的huncun.php文件 
2.在网站根目录建文件夹huancun(或其它) 
3.复制huncun.php到huancun目录下 
4.在huancun目录下建文件夹cache目录 
5.在用网站公共文件头或都生成静态的网页头上 加载本文件 
include("/huancun/huancun.php"); 
6.清空缓存 参考http://www.oschina.net/code/snippet_202258_7018 

缺点 
1.使用本系统不能实时更新内容 必须清空缓存后才能更新或者是到了设定的缓存时间才会更新 
2.第一次访问速度比较慢,因为没有缓存.第二次就是调用缓存文件,那时候就快了 
3.缓存文件占磁盘空间,建议服务器或者空间比较大的使用.或者定期删除缓存文件

 

<?php
/*www.diyphp.net
*www.php100.com
*/
if($_GET['phphuancun']!="true"){
define("HC_PATH",dirname(__FILE__)."/cache/");
define("HC_TIME",1);
echo HC_getcache();exit;
}
function HC_getcache($iscache='')
{
$url="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$cacheurl=strpos($url,"?")?$url."&phphuancun=true":$url."?phphuancun=true";
$cachename=HC_PATH.md5($url).".c";
 
$cachetime=$iscache?time()+1:time()-(HC_TIME * 60*60);
if(file_exists($cachename) && filemtime($cachename)>=$cachetime){
$return=file_get_contents($cachename);$data=function_exists(gzcompress)?@gzuncompress($return):$return;
return unserialize($data);
}else{$return=file_get_contents($cacheurl);HC_writecache($cachename,$return);return $return;}
 
}
function HC_writecache($name,$array)
{
function_exists(gzcompress)?$return =gzcompress(serialize($array)):$return=serialize($array);
@file_put_contents($name,$return);
}
?>