Cache Data with the WordPress Transients API

WP Engineer had an interesting post recently about a WordPress “Transients API” that is used for caching bits of data temporarily. I often use the Options API to cache things from external servers, such as Twitter statuses, so I don’t hit Twitter’s servers more often than necessary (which would slow down page loads). The Transients API is similar, but with the addition of an expiration field. This makes it a much easier solution, even without its other added benefit.

Also of note is that Transients are inherently sped up by caching plugins, where normal options are not. A memcached plugin, for example, would make WordPress store transient values in fast memory instead of in the database. For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time.

It’s simple enough to use the API. You just call the set_transient(), get_transient() and delete_transient() functions where appropriate. You can read up on the usage over at WP Engineer or the WordPress Codex.