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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.