Alisa Herr

my picture
  • NC native
  • I love food
  • Lead Web Developer at Cuberis
  • Taught myself how to code in 1996
  • Started using WordPress in 2009

Get in touch!

@isabisa
alisa@cuberis.com

About Transients and the API

What is a transient?

  • A piece of information that you need to access periodically, which is stored temporarily in a cache.
https://flic.kr/p/7CQL55

What is the Transients API?

  • Key-value pairs with expiration time
  • Value can be a string, array, or object
  • API handles serialization automatically

Why should I use it?

  • Server load optimization
  • Response time optimization
https://flic.kr/p/A5aTo

What can I use it for?

  • Long-running tasks / slow SQL queries
  • Fragment caching
  • Responses from requests of external APIs

What should I NOT use it for?

  • Single-use data storage
  • Anything you can't recreate when needed
  • Something you need to be in the database, guaranteed

How the Transients API works

The functions of the API

Saving a transient

        set_transient( $transient, $value, $expiration );
      

Retrieving a transient

        get_transient( $transient );
      

Deleting a transient

        delete_transient( $transient );
      

How transients are stored

  • Options table
  • Object cache
https://flic.kr/p/bxfJwH

How expiration works

  • Expiration time = maximum age
  • Won't expire on their own
  • The function get_transient() checks if transient is old
    • If yes, it deletes it
    • If no, it returns it
https://flic.kr/p/wmHk6

How to use the Transients API

Live demo

  1. Getting some data from an external API without using a transient
  2. Getting the same data from a transient (without object caching)
  3. Getting the same data from a transient (with object caching)

Edit page »

Speed Comparisons

Without Transients APITransients API (default)Transients API (object caching)
Time (s)0.394720.000910.00025
Time (s)0.332020.000990.00026
Time (s)0.248120.001110.00022
Average (s)0.324950.001000.00024
Improvement--->300x faster>1300x faster

Get out there and use the
Transients API already!

Resources

Thank You!

Questions?