Alex's Blog

program 于网络中

贴一个Cache类个人用得不错--by abbish

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);

             }

         }

     }



}

?>
#  www.openshow.org

#  寻求合作开源技术网站

#  联系QQ:47641788



Class abbish

{

    var
$username;

    var
$mywebsite;

    function
abbish()

    {

        
$this->username="abbish";

        
$this->mywebsite=array("Downv"=>"http://www.downv.com","OpenShow"=>"http://www.openshow","More"=>"... ...");

    }



    function
output()

    {

        print
$this->username;

        
print_r $this->mywebsite;

    }

}
阅读更多
个人分类: 代码收藏
想对作者说点什么? 我来说一句

SqlDbx数据库访问工具

2017年07月24日 834KB 下载

ado 的一个不错的操作,类

2009年11月18日 14KB 下载

ADO.NET类的继承

2011年12月14日 2.12MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭