php缓存类

<?php 
class h863_cache
{
    
//dir_cache: 缓存文件的路径;
    private static $path=dir_cache;
    
static function cache_isvalid($cacheid,$expire=300) {
        @
clearstatcache();
        
if (!@file_exists(self::$path.$cacheid)) return false;
        
if (!($mtime=@filemtime(self::$path.$cacheid))) return false;
        
$nowtime=mktime();
        
if (($mtime+$expire)<$nowtime) {
            
return false;
        }
else{
            
return true;
        }
    }

    
static function cache_write($cacheid,$cachecontent) {
        
$retry=100;
        
for ($i=0;$i<$retry;$i++) {
            
$ft=@fopen(self::$path.$cacheid,"wb");
            
if ($ft!=falsebreak;
            
if ($i==($retry-1)) return false;
        }
        @
flock($ft,LOCK_UN);
        @
flock($ft,LOCK_EX|LOCK_NB);
        
for ($i=0;$i<$retry;$i++) {
            
$tmp=@fwrite($ft,$cachecontent);
            
if ($tmp!=falsebreak;
            
if ($i==($retry-1)) return false;
        }
        @
flock($ft,LOCK_UN);
        @
fclose($ft);
        @
chmod(self::$path.$cacheid,0777);
        
return true;
    }

    
static function cache_fetch($cacheid) {
        
$retry=100;
        
for ($i=0;$i<$retry;$i++) {
            
$ft=@fopen(self::$path.$cacheid,"rb");
            
if ($ft!=falsebreak;
            
if ($i==($retry-1)) return false;
        }
        
$cachecontent='';
        
while (!@feof($ft)) {
            
$cachecontent.=@fread($ft,4096);
        }
        @
fclose($ft);
        
return $cachecontent;
    }

    
static function cache_delete($cacheid)
    {
        
try
        {
            @
unlink(self::$path.$cacheid);
        }
        
catch(Exception $e)
        {
        }
    }
    
    
static function cache_clear_expired($cachedirname,$expire=300) {
        
$cachedir=@opendir(self::$path.$cachedirname);
        
while (false!==($userfile=@readdir($cachedir))) {
            
if ($userfile!="." and $userfile!=".." and substr($userfile,-4,4)=='.htm') {
                
$cacheid=self::$path.$cachedirname.'/'.$userfile;
                
if (!cache_isvalid($cacheid,$expire)) @unlink($cacheid);
            }
        }
        @
closedir($cachedir);
    }
}
?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值