Jump to content

Looking for information on caching downloaded curl and API content in PHP


Drew121370

Recommended Posts

I would like to be able to cache data that is pulled down from the web. Such data like html or text from web pages, images and the data from API's etc. That content/cached copies would get saved to a file. Then, when downloading any additional content, that file would be checked first before downloading another cached copy.  

 

Any information, tutorials or book suggestions on learning how to do this, would be appreciated.

 

Thank you-

Drew

Link to comment
Share on other sites

You need a tutorial for this?

 

Example:

$file = "/path/to/cache/" . md5($url);
$expires = 86400; // one day

if (file_exists($file) && filemtime($file) < time() - $expires) {
	$data = // get data from $url
	file_put_contents($file, $data);
} else {
	$data = file_get_contents($file);
}
Link to comment
Share on other sites

Well, I guess I applaud that. Not sure how many tutorials there are that don't also cover a whole bunch of other stuff about caching... not that it would be bad to learn it all but it may be more than you're looking to deal with.

 

Caching is basically two parts:

1. If there is cached data, decide whether it's still good

2. If you need to get the data, get it and cache it

 

The rabbit hole goes quite deep but on the surface it's straightforward: for a file cache, check the file exists and is recent enough (modification time is easiest) and load from it if so, otherwise do the work and stuff it into the same file you looked for.

Link to comment
Share on other sites

If the target sites provide caching information (ETags, an Expires header, ...), then you should use that rather than inventing your own rules -- which will likely lead to stale data and unnecessary downloads.

 

So your cache should essentially work like the browser cache: It makes one initial download and stores the file together with all relevant meta data. When the file is needed again, the cache can either look up the expiry date or make a conditional request.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.