The latest version of ND.edu makes use of A LOT of external data sources. To make sure the site runs reliably, even in the case of an outage by one of the external feeds, I put together a fairly simple PHP class to handle the local caching of feeds.
It basically works like this:
- Create the class with the local file-name (whatever you want) and the external source (and an optional expires)
- The class will check if the local file exists and if it has expired
- If it has expired, it will try and fetch the remote source
- If the remote source is a-ok, it will pull a fresh copy and save it to a “cache” folder
- If remote source is unavailable or too slow, the class will fall-back to the expired (cached) file
Since I’m dealing with a variety of data sources, I’m doing most of my error handling in the calling function and not the class. And the reason I have it fall-back to the cached file, even if it’s technically expired is because in the use-case of this site, it’s better to have outdated data than nothing at all.
If any PHP gurus feel inclined to look it over, please make suggestions for changes or improvements. Feedback would be welcome. Feel free to leave comments here or fork it on GitHub. If you use it in a production site and it causes servers explode, I will deny any knowledge of the scripts existence, so use at your own risk.