PHP Class to Cache Remote Content by URL

While developing the NextGEN Facebook OG plugin for WordPress, which adds social buttons from Facebook, Google+, LinkedIn, etc. to content and pages (along with several other features), I found the response time from these websites to be disappointing at times. When speed testing the pages of my websites, the JavaScript and images from these social elements would sometimes be a significant part of the total page load time. You can’t really save a copy of these files and serve them yourself, because they are frequently updated. You could create a cronjob to update them on a regular basis, but the maintenance of this can be cumbersome (as you add or remove files, etc.). It’s much easier to use a PHP method that caches and refreshes the remote files, and translate the URL at the same time. For example, something like:

A complete example, including the setup of variables, etc., might look like this:

And here’s another example from the NextGEN Facebook OG plugin that creates the cache object in the __construct() method, defines the variable values in the setup_cache_vars() method, and uses get_cache_url() method to wrap the whole thing. In practice, this means I can use the following code to add a JavaScript URL:

The __construct() method includes the cache.php code and defines the cache object.

The setup_cache_vars() method is called to adjust the cache variables (cache directory path, local URL base, expiration time, etc.).

And finally the get_cache_url() method does a few checks and returns the modified URL.

The setup_cache_vars() method is called once from another method (not shown here).

I’ve only found two issues (so far) when hosting remote content; some source files, like the Google+ plusone.js javascript file, may change depending on the user agent. In this case, you may want to define the $cache->user_agent variable (using a constant, as shown in the setup_cache_vars() method above). The default user agent in the ngfbCache() class is the one provided by the browser. Consider that crawlers may refresh the cache files, so
hard-coding a user agent may be desirable.

The $cache->base_url variable can be relative or include the protocol (http / https), and could be an alias to the
real cache folder location (which may be outside of the document root). And finally, note that the $cache->base_dir and $cache->base_url variables should end with a slash.

You can download the cache.php script here.