5. Why caching is good for you?
A question you shouldn't ask your mom
1. It's easy
2. Your site = faster
3. Can save you $$
4. It will knock
your socks off
5. You'll sleep better
at night.
6. What we will talk about
● What is fragment / object caching
● Why caching is important
● How to cache
● What to cache
● When to cache
7. What we won't talk about
● Server setups
● Caching plugins
10. Transients are
● temporary bits of data (usually raw data)
● like options, but w/ expiration time
● always persistent out of the box
● stored in wp_options by default
● stored in memory if a caching backend (APC,
Memcache, XCache, etc.) is available
● available since 2.8
11. Use Transients when
● fetching data from a remote source
● performing an expensive* query or request
○ meta_query
○ tax_query
● data persistence is absolutely required**
* operations that require high CPU usage or high latency (Identify slow
queries with Debug Bar)
** ex. - when polling data from external sources
16. Multisite?
You're in luck! All Transients functions
have their network-wide counterparts.
get_site_transient();
set_site_transient();
delete_site_transient();
20. Instead, we can cache the
menu in a transient
and save on DB queries
21. // Get an existing copy of our transient data
$menu_html = get_transient( 'wcj_2013_menu' );
if ( false === $menu_html ) {
// data is not available, let's generate it
$menu_html = wp_nav_menu( array(
'theme_location' => 'primary', returns the
'echo' => false, menu instead
) ); of printing
set_transient( 'wcj_2013_menu', $menu_html, DAY_IN_SECONDS );
}
// do something with $menu_html
echo $menu_html; 60 * 60 * 24
22. // Get an existing copy of our transient data
$menu_html = get_transient( 'wcj_2013_menu' );
if ( false === $menu_html ) {
// data is not available, let's generate it
$menu_html = wp_nav_menu( array(
'theme_location' => 'primary',
'echo' => false,
) );
set_transient( 'wcj_2013_menu', $menu_html );
}
// do something with $menu_html
echo $menu_html; doesn't expire, but it might anyway
23. Now, let's invalidate
function wcj_2013_invldt_menu( $menu_id, $menu_data ) {
delete_transient( 'wcj_2013_menu' );
}
add_action( 'wp_update_nav_menu', 'wcj_2013_invldt_menu', 10, 2 );
this action runs whenever a menu is being saved
24. Use Transients when fetching
data from a remote source
http://...
https://...
25. Pick your poison
Twitter API
Facebook API last.fm API
Google+ API
36. Object Cache is
● non-persistent out of the box
● stored in PHP memory by default
● ideally* persistent when a caching backend is
available
● similar to Transients
● available since 2.0
* Success depends on server environment
37. Common use of the Object Cache API
DB
Is post_id
= 13 No SELECT *
cached? FROM
wp_posts
WHERE ID =
13
Yes
Do something with that Store results in Object
post Cache
40. Example
$song_obj = wp_cache_get( $id, 'songs' );
if ( false === $song_obj ) {
$song_obj = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM
$wpdb->songs WHERE ID = %d", $id )
);
wp_cache_set( $id, $song_obj, 'songs' );
}
// do something with $song_obj
41. Best Practices
for the best of us
● prefer refreshing the cache ONLY when data is
added/changed
● Avoid, if possible, caching on front end page requests
(instead, generate the data on an admin event*)
● Design to fail gracefully (never assume that data is in the
cache)
* useful events: publish_post, transition_post_status, created_
{$taxonomy}, edited_{$taxonomy}
42. Side-by-side comparison
Memory Cache
Out of the box
Backend
Transients API persistent - database Persistent
non-persistent -
Object Cache API Ideally Persistent
memory
http://wordpress.stackexchange.com/a/45137