【求】PHP缓存类

来源:百度知道 编辑:UC知道 时间:2024/06/18 15:59:14
功能如下:
主要用于缓存数据库调用的数据。
能够按时更新(调用指定函数)。
按需调用(而不是一下子全调用)。
谢谢!

<?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=@fo