我参考一个PHP缓存文件写了一个类,可是这个类无法执行回调函数,不知道错在哪里,望高手帮忙看看!
原文件源码[能正常生成缓存]:
<?php
define('CACHE_ROOT', dirname(__FILE__).'/cache'); //缓存存放目录
define('CACHE_TIME', 18000);//缓存时间 单位秒
define('CACHE_FIX','.html');
$CacheName=md5($_SERVER['REQUEST_URI']).CACHE_FIX; //缓存文件名
$CacheDir=CACHE_ROOT.'/'.substr($CacheName,0,2);
$CacheUrl=$CacheDir.'/'.$CacheName;//缓存文件的完整路径//GET方式请求才缓存,POST之后一般都希望看到最新的结果
if($_SERVER['REQUEST_METHOD']=='GET'){
//如果缓存文件存在,并且没有过期,就把它读出来。
if(file_exists($CacheUrl) && time()-filemtime($CacheUrl)<CACHE_TIME){
$fp=fopen($CacheUrl,'rb');
fpassthru($fp);
fclose($fp);
exit;
}elseif(!file_exists($CacheDir)){
if(!file_exists(CACHE_ROOT)){
mkdir(CACHE_ROOT,0777);
chmod(CACHE_ROOT,0777);
}
mkdir($CacheDir,0777);
chmod($CacheDir,0777);
}
//回调函数,当程序结束时自动调用此函数
function AutoCache($contents){
global $CacheUrl;
$fp=fopen($CacheUrl,'wb');
fwrite($fp,$contents);
fclose($fp);
chmod($CacheUrl,0777);
//生成新缓存的同时,自动删除所有的老缓存,以节约空间,可忽略。
DelOldCache();
return $contents;
}
function DelOldCache(){
chdir(CACHE_ROOT);
foreach (glob("*/*".CACHE_FIX) as $file){
if(time()-filemtime($file)>CACHE_TIME)unlink($file);
}
}
//回调函数 AutoCache
ob_start('AutoCache');
}else{
//不是GET的请求就删除缓存文件。
if(file_exists($CacheUrl))unlink($CacheUrl);
}
?>下面的源码是我改成类后的[能生成目录但无法生成缓存文件],ob_start('AutoCache') 这句没有执行,不知道错在哪里!
<?php
class jm_cache{
public function __construct($file_dir,$cache_time=1800,$file_two=0,$file_fix='.htm'){
$this->cache_root=dirname(__FILE__).'/../cache';//缓存存放目录
$this->file_dir=$file_dir;
$this->cache_time=$cache_time;
$this->file_two=$file_two;
$this->file_fix=$file_fix;
$this->file_name=md5($_SERVER['REQUEST_URI']).$this->file_fix;//缓存文件名
$this->cache_file=$this->cache_dir=$this->cache_root.'/'.$this->file_dir;//缓存的二级文件夹
if($this->file_two==1)$this->cache_dir=$this->cache_root.'/'.$this->file_dir.'/'.substr($this->file_name,0,2);//缓存的最终文件夹
$this->cache_url=$this->cache_dir.'/'.$this->file_name;//文件存放的完整路径
//GET方式请求才缓存,POST之后一般都希望看到最新的结果
if($_SERVER['REQUEST_METHOD']=='GET'){
//如果缓存文件存在,并且没有过期,就把它读出来。
if(file_exists($this->cache_url) && time()-filemtime($this->cache_url)<$this->cache_time){
$fp=fopen($this->cache_url,'rb');
fpassthru($fp);
fclose($fp);
exit;
}elseif(!file_exists($this->cache_dir)){//判断文件夹是否存在,不存在则创建
if(!file_exists($this->cache_file)){
if(!file_exists($this->cache_root)){
mkdir($this->cache_root,0777);
chmod($this->cache_root,0777);
}
mkdir($this->cache_file,0777);
chmod($this->cache_file,0777);
if($this->file_two==1){
mkdir($this->cache_dir,0777);
chmod($this->cache_dir,0777);
}
}
}
//回调函数 AutoCache
ob_start("AutoCache");
}else{
//不是GET的请求就删除缓存文件
if(file_exists($this->cache_url))unlink($this->cache_url);
}
}
function AutoCache($contents){
$fp=fopen($this->cache_url,'wb');
fwrite($fp,$contents);
fclose($fp);
chmod($this->cache_url,0777);
//生成新缓存的同时,自动删除所有的老缓存,以节约空间,可忽略。
$this->DelOldCache();
return $contents;
}
function DelOldCache(){
chdir($this->cache_root);
foreach (glob("*/*".$this->file_fix) as $file){
if(time()-filemtime($file)>$this->cache_time)unlink($file);
}
}
}
?>
原文件源码[能正常生成缓存]:
<?php
define('CACHE_ROOT', dirname(__FILE__).'/cache'); //缓存存放目录
define('CACHE_TIME', 18000);//缓存时间 单位秒
define('CACHE_FIX','.html');
$CacheName=md5($_SERVER['REQUEST_URI']).CACHE_FIX; //缓存文件名
$CacheDir=CACHE_ROOT.'/'.substr($CacheName,0,2);
$CacheUrl=$CacheDir.'/'.$CacheName;//缓存文件的完整路径//GET方式请求才缓存,POST之后一般都希望看到最新的结果
if($_SERVER['REQUEST_METHOD']=='GET'){
//如果缓存文件存在,并且没有过期,就把它读出来。
if(file_exists($CacheUrl) && time()-filemtime($CacheUrl)<CACHE_TIME){
$fp=fopen($CacheUrl,'rb');
fpassthru($fp);
fclose($fp);
exit;
}elseif(!file_exists($CacheDir)){
if(!file_exists(CACHE_ROOT)){
mkdir(CACHE_ROOT,0777);
chmod(CACHE_ROOT,0777);
}
mkdir($CacheDir,0777);
chmod($CacheDir,0777);
}
//回调函数,当程序结束时自动调用此函数
function AutoCache($contents){
global $CacheUrl;
$fp=fopen($CacheUrl,'wb');
fwrite($fp,$contents);
fclose($fp);
chmod($CacheUrl,0777);
//生成新缓存的同时,自动删除所有的老缓存,以节约空间,可忽略。
DelOldCache();
return $contents;
}
function DelOldCache(){
chdir(CACHE_ROOT);
foreach (glob("*/*".CACHE_FIX) as $file){
if(time()-filemtime($file)>CACHE_TIME)unlink($file);
}
}
//回调函数 AutoCache
ob_start('AutoCache');
}else{
//不是GET的请求就删除缓存文件。
if(file_exists($CacheUrl))unlink($CacheUrl);
}
?>下面的源码是我改成类后的[能生成目录但无法生成缓存文件],ob_start('AutoCache') 这句没有执行,不知道错在哪里!
<?php
class jm_cache{
public function __construct($file_dir,$cache_time=1800,$file_two=0,$file_fix='.htm'){
$this->cache_root=dirname(__FILE__).'/../cache';//缓存存放目录
$this->file_dir=$file_dir;
$this->cache_time=$cache_time;
$this->file_two=$file_two;
$this->file_fix=$file_fix;
$this->file_name=md5($_SERVER['REQUEST_URI']).$this->file_fix;//缓存文件名
$this->cache_file=$this->cache_dir=$this->cache_root.'/'.$this->file_dir;//缓存的二级文件夹
if($this->file_two==1)$this->cache_dir=$this->cache_root.'/'.$this->file_dir.'/'.substr($this->file_name,0,2);//缓存的最终文件夹
$this->cache_url=$this->cache_dir.'/'.$this->file_name;//文件存放的完整路径
//GET方式请求才缓存,POST之后一般都希望看到最新的结果
if($_SERVER['REQUEST_METHOD']=='GET'){
//如果缓存文件存在,并且没有过期,就把它读出来。
if(file_exists($this->cache_url) && time()-filemtime($this->cache_url)<$this->cache_time){
$fp=fopen($this->cache_url,'rb');
fpassthru($fp);
fclose($fp);
exit;
}elseif(!file_exists($this->cache_dir)){//判断文件夹是否存在,不存在则创建
if(!file_exists($this->cache_file)){
if(!file_exists($this->cache_root)){
mkdir($this->cache_root,0777);
chmod($this->cache_root,0777);
}
mkdir($this->cache_file,0777);
chmod($this->cache_file,0777);
if($this->file_two==1){
mkdir($this->cache_dir,0777);
chmod($this->cache_dir,0777);
}
}
}
//回调函数 AutoCache
ob_start("AutoCache");
}else{
//不是GET的请求就删除缓存文件
if(file_exists($this->cache_url))unlink($this->cache_url);
}
}
function AutoCache($contents){
$fp=fopen($this->cache_url,'wb');
fwrite($fp,$contents);
fclose($fp);
chmod($this->cache_url,0777);
//生成新缓存的同时,自动删除所有的老缓存,以节约空间,可忽略。
$this->DelOldCache();
return $contents;
}
function DelOldCache(){
chdir($this->cache_root);
foreach (glob("*/*".$this->file_fix) as $file){
if(time()-filemtime($file)>$this->cache_time)unlink($file);
}
}
}
?>
解决方案 »
- 做个东西, 但是没什么头绪.
- PHP有没有现成的资源可以编写WEBSERVICE协议的服务
- 如何通过脚本能限制打印网页的URL地址
- 一个关于附件下载的权限问题,想了很久,不知道怎样解决!
- 我的网站是采用PHP和MYSQL架构的,会频繁读取数据库,这样倒致网站速度变慢,请问如何加速?给分200
- php能做webmail系统吗?
- 谁有比较安全的登录系统?
- 哥们兄弟来了公司要用PHP做开发了唉来者有分送
- 请教一个不复杂的SQL问题
- 怎么让这个搞成一个二维数组,js控制,jquery,都可以,提交到后台,后台去解析这个二维数组??急
- 正则表达式匹配问题
- order by pid desc加到这个语句的那个位置?
ob_start("AutoCache");你的 AutoCache 不是类 jm_cache 的方法么?
要这样写
ob_start( array($this, "AutoCache") );