Skip to main content

Caching Responses of External HTTP Requests

Updated this week

With a feature built into the Seravo Plugin, it is possible to cache the responses received from HTTP requests made by the site to third party. For example, many paid plugins check their license against the plugin author's own server. If that server responds slowly or not at all, this may be reflected on your site as slowness or non-functionality. This can be avoided by caching the responses from external requests. The feature utilizes the WordPress Transients API. If you wish, you can review the feature's code via this link.

Initial Steps

First, determine what kind of external requests the site is making and which ones you want to cache. You can use a plugin like Log HTTP Requests to help map out these requests. In this article, we use the request made by the WordPress core to api.wordpress.org as an example.

By clicking the request link open, we can see that the HTTP method used is GET. This request method must be taken into account in the next step.

Activating the Feature

The caching functionality can be enabled by defining a filter in, for example, your child theme's functions.php file.

add_filter('seravo_cache_http_get_api_wordpress_org', '__return_true', 10, 1);

The desired address must be modified in the filter's name; in this case, api.wordpress.org has been converted to the format api_wordpress_org. Note that the HTTP method used is also part of the filter's name, which in this case is GET. The filter makes it possible to cache responses from both GET and POST requests.

Making a Test Request

First, clear all caches with the following command:

$ wp-purge-cache

Next, let's make a request to the target https://api.wordpress.org/events/1.0/, which was obtained from the Log HTTP Requests plugin, using WordPress's own wp_remote_get function:

$ wp eval "wp_remote_get('https://api.wordpress.org/events/1.0/');"

Checking Functionality

The functionality of the filter can be checked after making the test request. In the Seravo service, object caching is enabled by default on all sites, meaning that the WordPress transient cache data used here, is stored in the Redis object cache instead of the database. Retrieving data from the object cache is faster than fetching it from the database.

The cache keys created by the filter are prefixed with http_cache, which we can use to query the keys from Redis:

$ redis-cli keys "*http_cache*" 
1) "1:transient:http_cache_1e6b03e24f8a53cafdbd7418664184c2"

If for some reason the site does not use object cache, keys can be queried from the database with the following command:

$ wp transient list --fields=name,expiration --search="*http_cache*" --human-readable
+---------------------------------------------+------------+
| name | expiration |
+---------------------------------------------+------------+
| http_cache_1e6b03e24f8a53cafdbd7418664184c2 | 59 mins |
+---------------------------------------------+------------+
Did this answer your question?