logo   

【求】PHP缓存类_百度知道

<?php
function cache_isvalid($cacheid,$expire=300) {
@clearstatcache();
if (!@file_exists($cacheid)) return false;
if (!($mtime=@filemtime($cacheid))) return false;
$nowtime=mktime();
if (($mtime+$expire)<$nowtime) {
return false;
}else{
return true;
}
}

function cache_write($cacheid,$cachecontent) {
$retry=100;
for ($i=0;$i<$retry;$i++) {
$ft=@fopen($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($cacheid,0777);
return true;
}

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

function cache_clear_expired($cachedirname,$expire=300) {
$cachedir=@opendir($cachedirname);
while (false!==($userfile=@readdir($cachedir))) {
if ($userfile!="." and $userfile!=".." and substr($userfile,-4,4)=='.htm') {
$cacheid=$cachedirname.'/'.$userfile;
if (!cache_isvalid($cacheid,$expire)) @unlink($cacheid);
}
}
@closedir($cachedir);
}

?>
Tags:
访问文章出处: http://zhidao.baidu.com/question/20488927.html(【求】PHP缓存类_百度知道)

上一篇: PHP缓存的实现 - arcow的专栏 - CSDNBlog - 忧忧狼 - 网易博客
下一篇: (转)PHP截取汉字乱码时发生乱码解决办法 - PHP基础编程(问答求助) - PHP China | 中国开源之路 PHPChina|php论坛|Zend中国 - Powered by Discuz!