贴一个Cache类个人用得不错

PHP代码:
   
   

<?php

class cache
{
    
    var
$cacheDirectory;
    
    var
$cacheDuration=3600;
    
    var
$cacheFilename;

    function
cache($cacheDuration=3600,$cacheDirectory='./cache')
    {
        
$this->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<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','');
                    }
                }
            }
        }
        
    }

    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='')
     {
            
$tim=filemtime($this->cacheFilename);
            if (
trim($filename)=='') $filename = $contents;
            if (
$this->inCache($filename,1)&&(time()-$tim)>$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);
             }
         }
     }

}
?>

PHP代码:
  
  

使用方法:
include_once(_CONPATH_."Class/Class_Cache.php");
$cache=new cache();
if(
$cache->incache($_SERVER['QUERY_STRING']))
    {
        
$output=$cache->readcache();
    }
    else
    {
        
ob_start();
        
//这里是输出的内容
        
$output = ob_get_clean();
        
$cache->saveInCache($output,$_SERVER['QUERY_STRING']);
    }
    unset(
$cache);
    print(
$output);
    exit;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值