您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

对一个cache类的实际应用

2024/12/9 0:23:08发布15次查看
cache
class_cache.php:
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{
$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
$cache_file=$_server['php_self'].?.$_server['query_string'];
if($_server['php_self']==/index.php)
$cache_file=___index.php;
$cache_file=preg_replace(array(/\//,/\?/),array(,),$cache_file);
//echo $cache_file;
if($is_cache==1)
{
$cache=new cache($cache_time,$cachedirectory);
if($cache->incache($cache_file))
{
$output=$cache->readcache();
$cachetime=$cache->returncachetime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace(,$execute_time.
缓存版本:.$cachetime,$output);
print($output);
exit;
}else
ob_start();
}
function all_cache()
{
global $is_cache;
global $cache_file;
global $cache;
if($is_cache==1)
{
//这里是输出的内容
$output = ob_get_clean();
ob_end_clean(); 
$cache->saveincache($output,$cache_file); 
$cachetime=$cache->returncachetime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace(,$execute_time.
缓存版本:.$cachetime,$output);
print($output);
//exit; 

}
?>
用法
在页面开头引用
require(cache.php)?>
在页面最后加上
all_cache();?>
实际应用http://www.scmetals.com
class_cache类 原贴:http://www.phpx.com/happy/thr83014.html
class_cache.php内容如下
cacheduration = 0;
        $this->cachefilename = '';
        $this->cachedirectory = '.';
        $this->updatecache($cacheduration,$cachedirectory);
    }
function _makecachefolder()
    {
        if (!is_dir($this->cachedirectory))
        {
            $temp = explode('/',$this->cachedirectory);
            $cur_dir = '';
            for($i=0;$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','');
                    }
                }
            }
        }
}
function getcachefilename()
    {
        return $this->cachefilename;
    }
function _setcachefilename($contents)
     {
        $this->cachefilename = $this->cachedirectory.'/'.md5($contents).'.txt';
     }
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 updatecache($cacheduration=3600,$cachefolder='./cache')
    {
        $this->cacheduration = $cacheduration;
        $this->cachedirectory = $cachefolder;
        $this->_makecachefolder();
    }
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);
     }
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);
             }
         }
     }
}
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product