SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite
5.
Transients are ideal for:
Temporary data that can
expire at will.
6.
$value = get_transient( 'big_data' );
if ( false === $value ) {
// do something that takes a fair amount of time
$response = wp_remote_get( $url );
$value = wp_remote_retrieve_body( $response );
set_transient( 'big_data', $value, 60 * 60 * 24 );
}
echo $value;
7.
$value = get_transient( 'big_data' );
if ( false === $value ) {
// do something that takes a fair amount of time
$response = wp_remote_get( $url );
$value = wp_remote_retrieve_body( $response );
set_transient( 'big_data', $value );
// this one doesn't expire, but it might anyway
}
echo $value;
8.
Always persistent:
When there's no external
object cache, WordPress
uses the options table.
22.
As you might expect, it works
just fine in single-site.
23.
And then there's
switch_to_blog()
With great power comes
great responsibility.
24.
Rule 1
Don't use it.
If you do, cache around it.
25.
// Example is a widget with posts from another site:
$posts = wp_cache_get( 'recent_posts', $blog_id );
if ( false === $posts ) {
switch_to_blog( $blog_id );
$posts = new WP_Query( . . . )->posts;
wp_cache_set( $blog_id, $posts, 'recent_posts' );
restore_current_blog(); // OMG BBQ
}
// do something with $posts
26.
// And for some extra credit:
add_action( 'init', function() {
wp_cache_add_global_groups( 'recent_posts' );
} );
add_action( 'save_post', function() {
global $blog_id;
if ( ! in_array( $blog_id, array( 1, 2 3, 4 ) )
return;
wp_cache_delete( $blog_id, 'recent_posts' );
// And for even more extra credit:
$posts = new WP_Query( . . . )->posts;
wp_cache_add( $blog_id, $posts, 'recent_posts' );
} );