$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;
$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;
// 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
// 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' );
} );