Reputation: 1
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
Upvotes: 0
Views: 1802
Reputation: 14951
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);
}
Upvotes: 5
Reputation: 6152
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
Upvotes: 1