Using WordPress Transients with External Data

The transients feature of WordPress is a very useful tool for local data cache when pulling from a remote data source (web service/API).

A typical workflow goes something like this:

  1. Get remote data.
  2. Store it locally as transient data with a timeout.
  3. Once the data times out, get remote data again.

That’s the basics and will work well most of the time. However there are a few considerations to keep in mind when using transients to store this data.

  • When your transient expires, it is deleted from the WordPress database.
  • You cannot trust your remote data source to be accessible 100% of the time.

To account for the situations where a transient has expired and you can’t reach your remote data source, you will want to store the latest copy of your remote data in a more permanent location (probably as an option or post meta, depending on the nature of the data). With this copy of the data still available, you can insulate yourself from situations where your remote data source is unavailable.

Here is a better workflow that does error handling in a graceful and localized way:

  1. Get remote data.
  2. Store it locally as transient data with a timeout.
  3. Also store it locally as non-transient data (as an option or post meta).
  4. Once the data times out, get remote data again.
  5. If remote data request fails, fall back on the non-transient copy of the data and try to get the remote data again next time.

You may want to implement some throttling as well (re-check every minute rather than every request), if so – you can use a transient to easily implement the throttle duration.

Hopefully this is useful to other WordPress devs out there. Other (better?) suggestions on how to handle this? Share them in the comments.