Drew121370 Posted April 7, 2017 Share Posted April 7, 2017 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 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); } Quote Link to comment Share on other sites More sharing options...
Drew121370 Posted April 7, 2017 Author Share Posted April 7, 2017 Hi: I appreciate your help. The reason I mentioned a tutorial is because I was not going to come right out and ask for the code. I thought a tutorial would be good for learning purposes. Thanks again:) Drew Quote Link to comment Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 8, 2017 Share Posted April 8, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.