WordPress has a few built-in ways to cache data that enable rapid development. Understanding your options and how to use them properly in your context is crucial to a performant and scalable site. The Transients API provides a powerful and easy way to store data with an expiration, and it comes with a few under-the-hood perks as well.
Join me in looking at the benefits you can gain from understanding and implementing “transients”. When we’re done, you’ll know what this API is, when it should be used, how to use it, and how to scale it. I’ll give real, useful code examples that you can implement immediately—without boring you to death. You’ll be able to do anything from caching data from a external API (like recent tweets) to storing a large, complex query.
We’ll also cover some of the more obscure aspects of this method, like:
-Object caching/Memcached
-Autoloading
-Race Conditions
-Expired transient cleanup
-Options table bloat
Do yourself and your visitors a favor by utilizing the Transients API. And, as you’ll see in this session, knowing how to use it will make all WordPress’s caching techniques easy to implement.
2. The Transients API
!
“offers a simple and standardized
way of storing cached data in the
database temporarily by giving it a
custom name and a timeframe after
which it will expire and be deleted.”
3. The Transients API
!
✓ like Options API, but with expiration
✓ uses fast memory (if configured)
✓ uses database otherwise
32. cachebigquery
function popular_posts_panic( $num=10 ) {
if ( false === ( $popular =
get_transient( 'popular_posts' . $num ) ) ) {
return '';
} else {
$query = get_transient( 'popular_posts' . $num );
}
!
return $query;
}
Thanks to Andrew Gray for the idea.
Once a transient expires, it remains so until the new result is saved.
Heavy traffic can mean too many concurrent MySQL connections.
39. scalarvalues
✓ accepts scalar values (integer, float,
string or boolean) & non-scalar
serializable values (arrays, some
objects)
!
✓ SimpleXMLElement will FREAK. OUT.,
so convert it to a string or array of
objects (i.e. simplexml_load_string)
40. infinitetransients
✓ transients set without an expiration
time are autoloaded
!
✓ if you don’t need it on every page, set
an expiration (even if it’s a year)
!
✓ consider the Options API for non-
transient data
41. cachinggotchas
✓ in some shared hosting environments,
object caching can be slower than
using the database; check your host’s
recommended settings
!
✓ always use the Transients API to access
transients; don’t assume they’re in the
database (or vice versa)
43. TLC Transients
Supports soft-
expiration,
background updating
usefultools
Artiss Transient Cleaner
Deletes expired
transients and optimizes
table
Debug Bar Transients
Adds panel to Debug
Bar with transient info