Speed Things Up With Transients


Speaker: Cliff Seal
Twitter: @cliffseal
Presentation Slides: logos-creative.com/wcatl

What is the transients API

  • simple way to store cached data in the database temporarily by giving it a name and timeframe after which it will expire
  • like options API, but with expiration
  • uses fast memory (if configured)
  • uses database otherwise

What can you do with it?

  • external APIs – tweets, friends, scrobbles
  • cache expensive queries – tag cloud, ratings, custom queries

set_transient

  • $transient
    • (string) a unique identifiedr for your cached data
    • 45 characters or less in length
    • For multi-site transients, 40 characters or less in length
  • $value
    • (array|object) Data to save, either a regular variable or an array/object
    • Handles serialization for you
  • $expiration
    • (integer) number of seconds to keep the data before refreshing
    • built in wordpress time functions
  • get_transient($transient);
    • if the transient does not exist, or has expired, returns false
    • an integer value of zero/empty or could be the stored data
    • should not be used to hold plain boolean values; array or integers instead
  • delete_transient($transient);
  • multi-site
    • set_site_transient
    • get_site_transient
    • delete_site_transient
  • warnings
    • everything works on request
    • expired != deleted, unless requested
    • unrequested, undeleted transients stay until you remove them explicitly
  • scalar
    • 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)
  • infinite
    • 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
  • caching
    • 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)
  • 3 useful tools
    • TLC Transients – supports soft-expiration, background updating
    • Debug Bar Transients – adds panel to debug bar with transient info
    • Artiss Transient Cleaner – Deletes expired transients and optimizes table
  • Lots of code examples, be sure to checkout the presenter slides!