代码如下 复制代码
cachetime = $cachetime;
}
private function run() {
/** 缓存时间大于0,检测缓存文件的修改时间,在缓存时间内为缓存文件名,超过缓存时间为false,
小于等于0,返回false,并清理已缓存的文件
**/
return $this->cachetime ? $this->checkcachefile() : $this->cleancachefile();
}
function getcache($visturl,$cachefiletype = 'html')
{
$this->setcachefile($visturl,$cachefiletype);
$filename=$this->checkcachefile();
if($filename)
{
$fp = fopen($filename,r);
$content_= fread($fp, filesize($filename));
fclose($fp);
return $content_;
}
else
{
return false;
}
}
private function setcachefile($visturl,$cachefiletype = 'html') {
if(empty($visturl)) {
/** 默认为index.html **/
$this->cachefile = 'index';
}else {
/** 传递参数为$_post时 **/
$this->cachefile = is_array($visturl) ? implode('.',$visturl) : $visturl;
}
$this->cachefile = $this->cachedir.'/'.md5($this->cachefile);
$this->cachefile.= '.'.$cachefiletype;
}
function setcachetime($t = 60) {
$this->cachetime = $t;
}
private function checkcachefile() {
if(!$this->cachetime || !file_exists($this->cachefile)) {return false;}
/** 比较文件的建立/修改日期和当前日期的时间差 **/
$gettime=(time()-filemtime($this->cachefile))/(60*1);
/** filemtime函数有缓存,注意清理 **/
clearstatcache();
$this->debug('time limit '.($gettime*60).'/'.($this->cachetime*60).'');
$this->cachefound = $gettime cachetime ? $this->cachefile : false;
return $this->cachefound;
}
function savetocachefile($visturl,$content,$cachefiletype = 'html') {
$this->setcachefile($visturl,$cachefiletype);
if(!$this->cachetime) {
return false;
}
/** 检测缓存目录是否存在 **/
if(true === $this->checkcachedir()) {
$cachefile = $this->cachefile;
$cachefile = str_replace('//','/',$cachefile);
$fp = @fopen($cachefile,wb);
if(!$fp) {
$this->debug('open file '.$cachefile.' fail');
}else {
if(@!fwrite($fp,$content)){
$this->debug('write '.$cachefile.' fail');
}else {
$this->debug('cached file');
};
@fclose($fp);
}
}else {
/** 缓存目录不存在,或不能建立目录 **/
$this->debug('cache folder '.$this->cachedir.' not found');
}
}
private function checkcachedir() {
if(file_exists($this->cachedir)) { return true; }
/** 保存当前工作目录 **/
$location = getcwd();
/** 把路径划分成单个目录 **/
$dir = split(/, $this->cachedir);
/** 循环建立目录 **/
$catcherr = true;
for ($i=0; $i if (!file_exists($dir[$i])){
/** 建立目录失败会返回false 返回建立最后一个目录的返回值 **/
$catcherr = @mkdir($dir[$i],0777);
}
@chdir($dir[$i]);
}
/** 建立完成后要切换到原目录 **/
chdir($location);
if(!$catcherr) {
$this->debug('create folder '.$this->cachedir.' fail');
}
return $catcherr;
}
private function cleancachefile() {
if(file_exists($this->cachefile)) {
@chmod($this->cachefile,777);
@unlink($this->cachefile);
}
/** 置没有缓存文件 **/
$this->cachefound = false;
return $this->cachefound;
}
function debug($msg='') {
if(debug) {
$this->debugmsg[] = '[cache]'.$msg;
}
}
function geterror() {
return empty($this->debugmsg) ? '' :
n.implode(
n,$this->debugmsg);
}
}/* end of class */
?>
http://www.bkjia.com/phpjc/444619.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444619.htmltecharticle本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用哦,有需要的朋友可参考参考。 代码如下...
