开发者

php cache xml file for 1 hour

I am trying to work out how to download an 开发者_运维问答xml file to my server and keep it for 1 hour and then download it again, to cache it to speed my site up, but not having to much joy.

so far I just download it each time:

$data = file_get_contents('http://www.file.com/file.xml'); 
$fp = fopen('./file.xml', 'w+'); 
fwrite($fp, $data); 
fclose($fp); 

Can anyone help me out with adding in some caching for this please?

Thanks in advance

Richard


The best solution is to write a CRONJOB that runs once an hour, and generates the xml.

If you are somehow unable to, you could do the following solution using file modification time.

$sFileName = './file.xml';
$iCurrentTime = time();
$iFiletime = filemtime($sFileName);
if ($iFiletime < $iCurrentTime - 3600) {
    $data = file_get_contents('http://www.file.com/file.xml'); 
    $fp = fopen($sFileName, 'w+'); 
    fwrite($fp, $data); 
    fclose($fp); 
}    


in some cases this is what caching means (what you wrote),

Unless you want to save it in memory (actual cache). in this case you need http://memcached.org

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜