<?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!=false) break;
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!=false) break;
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!=false) break;
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);
}
}
?>
176

被折叠的 条评论
为什么被折叠?



