日期:2013-04-16  浏览次数:20449 次

Class_Cache.php:
<?php
 
class cache
{
 
var $cacheDirectory;
 
var $cacheDuration=3600;
 
var $cacheFilename;
 
function cache($cacheDuration=3600,$cacheDirectory='./cache')
{
$this->cacheDuration = 0;
$this->cacheDirectory = '.';
$this->cacheFilename = '';
 
$this->updateCache($cacheDuration,$cacheDirectory);
}
 
function getCacheFilename()
{
return $this->cacheFilename;
}
 
function updateCache($cacheDuration=3600,$cacheFolder='./cache')
{
$this->cacheDuration = $cacheDuration;
$this->cacheDirectory = $cacheFolder;
$this->_makeCacheFolder();
}
 
function _makeCacheFolder()
{
/*if (!is_dir($this->cacheDirectory))
{
$temp = explode('/',$this->cacheDirectory);
$cur_dir = '';
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].'/';
 
if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
}
}*/
if (!is_dir($this->cacheDirectory))
{
$cur_dir=$this->cacheDirectory;
//echo $cur_dir;
if (@mkdir($cur_dir,777))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
 
}
 
function _writeFile($filename,$contents)
{
if (!file_exists($filename))
{
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}else{
unlink($filename);
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}
}
 
function _setCacheFilename($contents)
{
//$this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
 
/***********/
global $cache_file;
$this->cacheFilename = $this->cacheDirectory.'/'.$cache_file.'.txt';
/***********/
}
function returnCacheTime()
{
//return "asdfd";
$tim=filemtime($this->cacheFilename);
return date('Y年m月d日 H时i分s秒',$tim);
}
function inCache($contents,$sty='')
{
$this->_setCacheFilename($contents);
if($sty==1)
{
return file_exists($this->cacheFilename);
}else{
if(file_exists($this->cacheFilename))
{
$tim=filemtime($this->cacheFilename);
if((time()-$tim)>$this->cacheDuration)
{
return false;
}else{
return true;
}
}else{
return false;
}
}
}
 
function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp)) 
$contents .= fread($fp,4096);
fclose($fp);
}
return $contents;
}
 
function saveInCache($contents,$filename='')
{
if (trim($filename)=='') $filename = $contents;
if ($this->inCache($filename,1))
{
if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
{
@unlink($this->cacheFilename);
}
}
$this->_writeFile($this->cacheFilename,$contents);
}
 
}
?>
cache.php:
<?
 require_once("Class_Cache.php");?>
<?
//---------页面缓存----------
$is_cache=1;//是否缓存
$cache_time=300;//缓存时间
if ((strstr($script_name,"/member/") == true) || (strstr($script_name,"/common/") == true))
$is_cache=0;
$cacheDirectory=$_SERVER['DOCUMENT_ROOT']."/cache/";
if($_SERVER['QUERY_STRING']=='')
$cache_file=$_SERVER['PHP_SELF'];
else
$ca