首页 > 开发 > PHP > 正文

对一个cache类的实际应用

2024-05-04 22:59:46
字体:
来源:转载
供稿:网友

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
$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("<!--show_execute_time-->",$execute_time."<br>缓存版本:".$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("<!--show_execute_time-->",$execute_time."<br>缓存版本:".$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内容如下
<?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='')
     {
            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);
             }
         }
     }
 
}
?>

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表