php的cache技术
yorgo 2001-07-25 03:22:02 Caching remote resources
We can use this function instead of fopen() to cache requests to remote servers to make the page load faster and so we don't annoy our content providers.
<?
function cached_open($url) {
$hash = md5($url);
if (!file_exists("/tmp/$hash") || time() - filemtime("/tmp/$hash") > 60*10) {
$in = fopen($url, "r");
$out = fopen("/tmp/$hash".getmypid(), "w");
while ($chunk = fread($in,4096)) {
fwrite($out,$chunk);
}
fclose($in);
fclose($out);
rename("/tmp/$hash".getmypid(), "/tmp/$hash");
}
return fopen("/tmp/$hash", "r");
}
欢迎大家讨论