首頁 php教程 PHP源码 一个很方便的PHP缓存类!

一个很方便的PHP缓存类!

May 25, 2016 pm 05:12 PM

php代码

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)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");
	  ob_start(array($this, "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);
    }
  }
}
?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)