WordCamp 2019 - Intro to WP Transients API
July 20, 2019•270 words
talk about caching basically
very specific type
transient - bit of data cached for indeterminate amount of time
two primary uses:
- time consuming queries - meta queries, cache the results in transient
- remote data fetched via the HTTP API - from external site, can set lifecycle
How do they work?
- give a name
- check to see if exists
- use it if there
- get it if not and save it for next time
Transient storage is handled by WP
- WP first checks for memcache redis apc etc
- falls back to wp options table, still faster though
Transient functions are just set/get/delete - set updates anything that exists
nofoundrows = true (turn off for not worrying about pagination)
Ugly query:
Transients:
WP has time constants like HOURINSECONDS so you can multiply that out
Managing changed data:
- have a hook that fires after update
- make sure it isn't a revision, other subtle gotchas to look out for
- can set transient to never die
- if you can regenerate the transient after deletion, you should
Plugins
- Pippin's Transient Manager (Williamson) - see all transients for your site
- Delete Expired Transients - what it says, because they are not always deleted
- Query Monitor - can show you which transients were changed in this page load
WordPress doesn't delete expired data, you always have to refresh
How to do transient deletion with wildcards?
$wpdb->query( "DELETE FROM $wpdb->options
WHERE option_name
LIKE ('transientgalleries_%')" );
unless in memcached or redis, then you have to use their way to interact with it