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

6,573 views
6,466 views

Published on

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

2 Comments
5 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
6,573
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
39
Comments
2
Likes
5
Embeds 0
No embeds

No notes for slide

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

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

×