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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

5,956

Published on

"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.

2 Comments
4 Likes
Statistics
Notes
  • WPTV link: http://wordpress.tv/2011/09/07/otto-wood-andrew-nacin-the-otto-and-nacin-show/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Will this be on wordpress.tv soon? I really need more info on the switch to blog doom you mention in the slides.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
5,956
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
38
Comments
2
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. The Otto & Nacin ShowWordCamp San Francisco 2011August 12, 2011
  • 2. Nacin OttoContributing Developers to WordPress Tech Ninjas at Audrey Capitalnacin@wordpress.org otto@wordpress.org@nacin @otto42
  • 3. Transients, Caching, andthe Complexities ofMultisite(A brief overview.)
  • 4. Basic Caching withTransients
  • 5. Transients are ideal for:Temporary data that canexpire 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 doesnt expire, but it might anyway}echo $value;
  • 8. Always persistent:When theres no externalobject cache, WordPressuses the options table.
  • 9. Object Caching
  • 10. Object caching is ideal for:Caching an object (often aquery result) to reduceoverhead.
  • 11. $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
  • 12. Ideally persistent:Use an object cachingbackend like APC,Memcached, or WinCache.
  • 13. Object caching is not ideal ifyou need both datapersistence and codeportability.No knowledge of the serversetup? Use transients.
  • 14. // Cache functions can take an expiration toowp_cache_set( $id, $activity_object , activity, $expires = 0 );
  • 15. Enter Multisite(Run.)
  • 16. Site Transients!Well, network-wide transients.Pardon the terminology.
  • 17. $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;
  • 18. Always persistent:When theres no externalobject cache, WordPressuses the sitemeta table.
  • 19. It works in single-site!When not running multisite,it wraps *_transient().
  • 20. Global Cache GroupsCache groups (activity) are site-specific. Tell WordPress what isnt.
  • 21. // 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 );
  • 22. As you might expect, it worksjust fine in single-site.
  • 23. And then theresswitch_to_blog()With great power comesgreat responsibility.
  • 24. Rule 1Dont 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 );} );
  • 27. Thanks! Questions?@otto42 @nacin

×