改写的缓存类文件 php

View Code 
  1 <?php
  2 class cache{
  3 
  4 var $cache_dir="data/";
  5 
  6 var $cacheLimitTime=0;
  7 
  8 var $cacheFileName="";
  9 
 10 var $cacheFileExt="";
 11 
 12 
 13 //构造函数 缓存更新时间
 14 function cache($cacheLimitTime,$filename,$dir,$cacheFileExt)
 15 {
 16     if(intval($cacheLimitTime))
 17     {
 18         $this->cache_dir.=$dir;
 19         $this->cacheLimitTime=$cacheLimitTime;
 20         $this->cacheFileExt=$cacheFileExt;
 21         $this->cacheFileName=$this->getCacheFileName($filename);
 22         ob_start();
 23     }
 24 
 25 }
 26 
 27 //检查缓存文件是否在设置更新时间内
 28 //返回 如果在更新时间内就返回true 否则返回false
 29 function cacheCheck()
 30 {
 31     if(file_exists($this->cacheFileName))
 32     {
 33         $Ctime=$this->getFileGreateTime($this->cacheFileName);
 34         if($Ctime+$this->cacheLimitTime>time())
 35         {
 36            return true;
 37         }
 38     }
 39     return  false;
 40 }
 41 //检查缓存文件是否在设置更新时间内
 42 //返回 如果在更新时间内就返回内容 否则返回false
 43 function get_cacheContent()
 44 {
 45     if(file_exists($this->cacheFileName))
 46     {
 47         $Ctime=$this->getFileGreateTime($this->cacheFileName);
 48         if($Ctime+$this->cacheLimitTime>time())
 49         {
 50            return file_get_contents($this->cacheFileName);
 51            ob_end_flush();
 52         }
 53     }
 54     return  false;
 55 }
 56 //检查缓存文件是否在设置更新时间内
 57 //如果在更新时间内就输出内容 否则返回false
 58 function output_cacheContent()
 59 {
 60     if(file_exists($this->cacheFileName))
 61     {
 62         $Ctime=$this->getFileGreateTime($this->cacheFileName);
 63         if($Ctime+$this->cacheLimitTime>time())
 64         {
 65            echo file_get_contents($this->cacheFileName);
 66            ob_end_flush();
 67            exit;
 68         }
 69     }
 70 }
 71 
 72 //缓存文件或输出静态
 73 
 74 function caching($data='',$staticFileName='')
 75 {
 76     if(!$data)
 77     {
 78         if($this->cacheFileName)
 79         {
 80             $cacheContent=ob_get_contents();
 81             ob_end_flush();
 82         }
 83     }else{
 84         $cacheContent=$data;
 85     }    
 86     if($staticFileName)
 87     {
 88         $this->saveFile($staticFileName,$cacheContent);
 89     }
 90     if($this->cacheLimitTime)
 91     {
 92         $this->saveFile($this->cacheFileName,$cacheContent);
 93     }
 94 
 95 }
 96 
 97 //清除缓存文件
 98 //指定文件或全部
 99 //返回 成功 失败
100 function clearCache($fileName='all')
101 {
102     if($fileName!="all")
103     {
104         $hash_dir = $this->cache_dir . '/' . substr(md5($fileName), 0, 2)."/";
105         $fileName=$hash_dir.strtoupper(md5($fileName)).".".$this->cacheFileExt;
106         if(file_exists($fileName))
107         {
108             return @unlink($fileName);
109         }else{
110             return false;
111         }
112     }
113     
114     if(is_dir($this->cache_dir))
115     {
116         if ($dh = opendir($this->cache_dir)) {
117             while (($file = readdir($dh)) !== false) {
118                 $check=is_dir($file);
119                 if(!$check)
120                 {
121                     @unlink($this->cache_dir.$file);
122                 }
123             }
124             @closedir($dh);
125             return true;
126         }else{
127             return false;
128         }
129     }else{
130         return false;
131     }
132 
133 }
134 
135 
136 //根据当前动态文件生成静态文件
137 function getCacheFileName($filename)
138 {
139     $hash_dir = $this->cache_dir . substr(md5($_SERVER['REQUEST_URI']), 0, 2)."/";
140     return $hash_dir.strtoupper(md5($_SERVER['REQUEST_URI']))."_".$filename.".".$this->cacheFileExt;
141 }
142 
143 
144 //缓存文件的建立时间
145 //缓存文件名 含相对路径
146 function getFileGreateTime($fileName)
147 {
148     if(!trim($fileName))
149     {
150         return 0;
151     }
152     if(file_exists($fileName))
153     {
154         return intval(filemtime($fileName));
155     }else{
156         return 0;
157     }
158 
159 }
160 
161 //保存文件
162 function savefile($fileName,$text)
163 {
164     if(!$fileName && !$text)
165     {
166         return false;
167     }
168     
169     if($this->makeDir(dirname($fileName)))
170     {
171         if($fp=fopen($fileName,'w'))
172         {
173             if(@fwrite($fp,$text))
174             {
175                 fclose($fp);
176                 return true;
177             }else{
178                 fclose($fp);
179                 return false;
180             }
181         }
182     }
183 
184     return false;
185 }
186 
187 //连续见目录 
188 //$dir 目录字符串 
189 function makeDir($dir$mode = 0777)
190 {
191     if(!$dir)
192     {
193         return 0;
194     }
195     $dir=str_replace("\\","/",$dir);
196     $mdir="";
197     foreach(explode("/",$diras $val)
198     {
199         $mdir.=$val."/";
200         if($val==".." || $val=="." || trim($val)=="") continue;
201         if(!file_exists($mdir))
202         {
203             if(!@mkdir($mdir,$mode))
204             {
205                 return false;
206             }
207         }
208     }
209     return true;
210     
211     
212 }
213 
214 
215 }
216 
217 
218

<?php

$expire=86400*7; //设置过期时间
$dir="api/";//缓存路径
$ext="txt";//后缀名

$cache_id=$jid."_name";

$cache=new cache($expire,$cache_id,$dir,$ext);

if(!$cache->cachecheck()) {

$data=journals_profile($jid);//

 

if(!$cache->cachecheck())//判断缓存文件否过期或存在 过期或不存在就更新并且输出
{
echo json_encode($data);
$data = json_encode($data);
$cache->caching($data);//更新缓存
}else{
$cache->output_cacheContent();

?> 

posted @ 2012-07-24 10:02  龙圆  阅读(252)  评论(0编辑  收藏  举报