WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite
Upcoming SlideShare
Loading in...5
×
 

WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite

on

  • 6,035 views

"The Otto & Nacin Show" at WordCamp San Francisco, on August 12, 2011, covered transients, caching, and how multisite complicates these.

"The Otto & Nacin Show" at WordCamp San Francisco, on August 12, 2011, covered transients, caching, and how multisite complicates these.

Statistics

Views

Total Views
6,035
Views on SlideShare
5,738
Embed Views
297

Actions

Likes
3
Downloads
36
Comments
1

11 Embeds 297

http://www.arastoo.com 158
http://wpspeciali.st 93
http://lanyrd.com 14
http://paper.li 11
https://twitter.com 8
http://localhost 5
http://twitter.com 3
http://0.highlight_server.history.ya.borg.google.com:25414 2
http://a0.twimg.com 1
http://duckduckgo.com 1
http://us-w1.rockmelt.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite Presentation Transcript

  • The Otto & Nacin ShowWordCamp San Francisco 2011August 12, 2011
  • Nacin OttoContributing Developers to WordPress Tech Ninjas at Audrey Capitalnacin@wordpress.org otto@wordpress.org@nacin @otto42
  • Transients, Caching, andthe Complexities ofMultisite(A brief overview.)
  • Basic Caching withTransients
  • Transients are ideal for:Temporary data that canexpire at will.
  • $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 doesnt expire, but it might anyway}echo $value;
  • Always persistent:When theres no externalobject cache, WordPressuses the options table.
  • Object Caching
  • Object caching is ideal for:Caching an object (often aquery result) to reduceoverhead.
  • $activity_object = wp_cache_get( $id, activity );if ( false === $activity_object ) { // begrudgingly fetch our object $activity_object = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->activity WHERE ID = %d", $id ) ); wp_cache_set( $id, $activity_object , activity );}// do something with $activity_object
  • Ideally persistent:Use an object cachingbackend like APC,Memcached, or WinCache.
  • Object caching is not ideal ifyou need both datapersistence and codeportability.No knowledge of the serversetup? Use transients.
  • // Cache functions can take an expiration toowp_cache_set( $id, $activity_object , activity, $expires = 0 );
  • Enter Multisite(Run.)
  • Site Transients!Well, network-wide transients.Pardon the terminology.
  • $value = get_site_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_site_transient( big_data, $value, 86400 );}echo $value;
  • Always persistent:When theres no externalobject cache, WordPressuses the sitemeta table.
  • It works in single-site!When not running multisite,it wraps *_transient().
  • Global Cache GroupsCache groups (activity) are site-specific. Tell WordPress what isnt.
  • // WordPress core (simplified)wp_cache_add_global_groups( array( users, user_meta, site-transient, site-options, site-lookup, blog-lookup, blog-details, ));// You (not simplified)wp_cache_add_global_groups( activity );
  • As you might expect, it worksjust fine in single-site.
  • And then theresswitch_to_blog()With great power comesgreat responsibility.
  • Rule 1Dont use it.If you do, cache around it.
  • // 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 );} );
  • Thanks! Questions?@otto42 @nacin