SlideShare a Scribd company logo
1 of 27
Download to read offline
The Otto & Nacin Show


WordCamp San Francisco 2011
August 12, 2011
Nacin           Otto
Contributing Developers to WordPress
   Tech Ninjas at Audrey Capital
nacin@wordpress.org   otto@wordpress.org
@nacin                          @otto42
Transients, Caching, and
the Complexities of
Multisite
(A brief overview.)
Basic Caching with
Transients
Transients are ideal for:
Temporary data that can
expire 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 doesn't expire, but it might anyway
}

echo $value;
Always persistent:
When there's no external
object cache, WordPress
uses the options table.
Object Caching
Object caching is ideal for:
Caching an object (often a
query result) to reduce
overhead.
$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 caching
backend like APC,
Memcached, or WinCache.
Object caching is not ideal if
you need both data
persistence and code
portability.
No knowledge of the server
setup? Use transients.
// Cache functions can take an expiration too
wp_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 there's no external
object cache, WordPress
uses the sitemeta table.
It works in single-site!
When not running multisite,
it wraps *_transient().
Global Cache Groups


Cache groups ('activity') are site-
specific. Tell WordPress what isn't.
// 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 works
just fine in single-site.
And then there's
switch_to_blog()

With great power comes
great responsibility.
Rule 1
Don't 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

More Related Content

What's hot

Working with WP_Query in WordPress
Working with WP_Query in WordPressWorking with WP_Query in WordPress
Working with WP_Query in WordPresstopher1kenobe
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web frameworktaggg
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST APIWordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST APIBrian Hogg
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hopeMarcus Ramberg
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creationbenalman
 
Task 1
Task 1Task 1
Task 1EdiPHP
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)Beau Lebens
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Michael Peacock
 
Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development EnvironmentsBeau Lebens
 
Introduction to the WordPress Transients API
Introduction to the WordPress Transients APIIntroduction to the WordPress Transients API
Introduction to the WordPress Transients APItopher1kenobe
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Workhorse Computing
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)andrewnacin
 
Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019Adam Tomat
 
Cloud Automation with Opscode Chef
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode ChefSri Ram
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tCosimo Streppone
 

What's hot (20)

Working with WP_Query in WordPress
Working with WP_Query in WordPressWorking with WP_Query in WordPress
Working with WP_Query in WordPress
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
 
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST APIWordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
 
Task 1
Task 1Task 1
Task 1
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig
 
Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development Environments
 
Introduction to the WordPress Transients API
Introduction to the WordPress Transients APIIntroduction to the WordPress Transients API
Introduction to the WordPress Transients API
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
 
WCLV13 JavaScript
WCLV13 JavaScriptWCLV13 JavaScript
WCLV13 JavaScript
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
 
Selenium sandwich-2
Selenium sandwich-2Selenium sandwich-2
Selenium sandwich-2
 
Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019
 
Hooks WCSD12
Hooks WCSD12Hooks WCSD12
Hooks WCSD12
 
Cloud Automation with Opscode Chef
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode Chef
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
 

Viewers also liked

WordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPressWordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPressandrewnacin
 
eMusic: WordPress in the Enterprise
eMusic: WordPress in the EnterpriseeMusic: WordPress in the Enterprise
eMusic: WordPress in the EnterpriseScott Taylor
 
E-commerce & WordPress: Navigating the Minefield
E-commerce & WordPress: Navigating the MinefieldE-commerce & WordPress: Navigating the Minefield
E-commerce & WordPress: Navigating the MinefieldIngenesis Limited
 
Coding, Scaling, and Deploys... Oh My!
Coding, Scaling, and Deploys... Oh My!Coding, Scaling, and Deploys... Oh My!
Coding, Scaling, and Deploys... Oh My!Mark Jaquith
 
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...Shannon Smith
 
Don't Repeat Your Mistakes: JavaScript Unit Testing
Don't Repeat Your Mistakes: JavaScript Unit TestingDon't Repeat Your Mistakes: JavaScript Unit Testing
Don't Repeat Your Mistakes: JavaScript Unit Testingaaronjorbin
 
DNS exfiltration using sqlmap
DNS exfiltration using sqlmapDNS exfiltration using sqlmap
DNS exfiltration using sqlmapMiroslav Stampar
 
State of the Word 2016
State of the Word 2016State of the Word 2016
State of the Word 2016photomatt
 
Pushing Python: Building a High Throughput, Low Latency System
Pushing Python: Building a High Throughput, Low Latency SystemPushing Python: Building a High Throughput, Low Latency System
Pushing Python: Building a High Throughput, Low Latency SystemKevin Ballard
 
State of the Word 2015, WordCamp US
State of the Word 2015, WordCamp USState of the Word 2015, WordCamp US
State of the Word 2015, WordCamp USphotomatt
 
WordCamp Netherlands 2012: WordPress in 2012
WordCamp Netherlands 2012: WordPress in 2012WordCamp Netherlands 2012: WordPress in 2012
WordCamp Netherlands 2012: WordPress in 2012andrewnacin
 
Advanced Custom Fields が重すぎると感じるあなたへ
Advanced Custom Fields が重すぎると感じるあなたへ Advanced Custom Fields が重すぎると感じるあなたへ
Advanced Custom Fields が重すぎると感じるあなたへ タカシ キタジマ
 
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...andrewnacin
 
Twitter Presentation: #APIConSF
Twitter Presentation: #APIConSFTwitter Presentation: #APIConSF
Twitter Presentation: #APIConSFRyan Choi
 
Time to climb-- results of national study of disadvantaged entrepreneurs ...
Time to climb-- results of national study of disadvantaged entrepreneurs ...Time to climb-- results of national study of disadvantaged entrepreneurs ...
Time to climb-- results of national study of disadvantaged entrepreneurs ...Richard Swart, PhD
 
ColdFusion for Penetration Testers
ColdFusion for Penetration TestersColdFusion for Penetration Testers
ColdFusion for Penetration TestersChris Gates
 
Ako na rýchly web - WordCamp Žilina 2016 - xKatka
Ako na rýchly web - WordCamp Žilina 2016 - xKatkaAko na rýchly web - WordCamp Žilina 2016 - xKatka
Ako na rýchly web - WordCamp Žilina 2016 - xKatkaKatarina Novotna
 
Customize Your WordPress Theme the Right Way
Customize Your WordPress Theme the Right WayCustomize Your WordPress Theme the Right Way
Customize Your WordPress Theme the Right WayDustin Hartzler
 
The power of a video library
The power of a video libraryThe power of a video library
The power of a video libraryLauren Jeffcoat
 

Viewers also liked (20)

WordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPressWordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPress
 
eMusic: WordPress in the Enterprise
eMusic: WordPress in the EnterpriseeMusic: WordPress in the Enterprise
eMusic: WordPress in the Enterprise
 
How Testing Changed My Life
How Testing Changed My LifeHow Testing Changed My Life
How Testing Changed My Life
 
E-commerce & WordPress: Navigating the Minefield
E-commerce & WordPress: Navigating the MinefieldE-commerce & WordPress: Navigating the Minefield
E-commerce & WordPress: Navigating the Minefield
 
Coding, Scaling, and Deploys... Oh My!
Coding, Scaling, and Deploys... Oh My!Coding, Scaling, and Deploys... Oh My!
Coding, Scaling, and Deploys... Oh My!
 
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...
Taking WordPress to the World : Options for a Multilingual Site | WordCamp Sa...
 
Don't Repeat Your Mistakes: JavaScript Unit Testing
Don't Repeat Your Mistakes: JavaScript Unit TestingDon't Repeat Your Mistakes: JavaScript Unit Testing
Don't Repeat Your Mistakes: JavaScript Unit Testing
 
DNS exfiltration using sqlmap
DNS exfiltration using sqlmapDNS exfiltration using sqlmap
DNS exfiltration using sqlmap
 
State of the Word 2016
State of the Word 2016State of the Word 2016
State of the Word 2016
 
Pushing Python: Building a High Throughput, Low Latency System
Pushing Python: Building a High Throughput, Low Latency SystemPushing Python: Building a High Throughput, Low Latency System
Pushing Python: Building a High Throughput, Low Latency System
 
State of the Word 2015, WordCamp US
State of the Word 2015, WordCamp USState of the Word 2015, WordCamp US
State of the Word 2015, WordCamp US
 
WordCamp Netherlands 2012: WordPress in 2012
WordCamp Netherlands 2012: WordPress in 2012WordCamp Netherlands 2012: WordPress in 2012
WordCamp Netherlands 2012: WordPress in 2012
 
Advanced Custom Fields が重すぎると感じるあなたへ
Advanced Custom Fields が重すぎると感じるあなたへ Advanced Custom Fields が重すぎると感じるあなたへ
Advanced Custom Fields が重すぎると感じるあなたへ
 
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...
Challenges Building the WordPress REST API (API Strategy & Practice, Chicago ...
 
Twitter Presentation: #APIConSF
Twitter Presentation: #APIConSFTwitter Presentation: #APIConSF
Twitter Presentation: #APIConSF
 
Time to climb-- results of national study of disadvantaged entrepreneurs ...
Time to climb-- results of national study of disadvantaged entrepreneurs ...Time to climb-- results of national study of disadvantaged entrepreneurs ...
Time to climb-- results of national study of disadvantaged entrepreneurs ...
 
ColdFusion for Penetration Testers
ColdFusion for Penetration TestersColdFusion for Penetration Testers
ColdFusion for Penetration Testers
 
Ako na rýchly web - WordCamp Žilina 2016 - xKatka
Ako na rýchly web - WordCamp Žilina 2016 - xKatkaAko na rýchly web - WordCamp Žilina 2016 - xKatka
Ako na rýchly web - WordCamp Žilina 2016 - xKatka
 
Customize Your WordPress Theme the Right Way
Customize Your WordPress Theme the Right WayCustomize Your WordPress Theme the Right Way
Customize Your WordPress Theme the Right Way
 
The power of a video library
The power of a video libraryThe power of a video library
The power of a video library
 

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

Intro to advanced caching in WordPress
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPressMaor Chasen
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordCamp Kyiv
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingErick Hitter
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Cliff Seal
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
WordPress as an application framework
WordPress as an application frameworkWordPress as an application framework
WordPress as an application frameworkDustin Filippini
 
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014Cliff Seal
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformanceJonas De Smet
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWordCamp Indonesia
 
Developing cacheable backend applications - Appdevcon 2019
Developing cacheable backend applications - Appdevcon 2019Developing cacheable backend applications - Appdevcon 2019
Developing cacheable backend applications - Appdevcon 2019Thijs Feryn
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with TransientsCliff Seal
 
Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!Konstantin Obenland
 

Similar to WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Multisite (20)

Intro to advanced caching in WordPress
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPress
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
WordPress as an application framework
WordPress as an application frameworkWordPress as an application framework
WordPress as an application framework
 
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
Fatc
FatcFatc
Fatc
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Memory Management in WordPress
Memory Management in WordPressMemory Management in WordPress
Memory Management in WordPress
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
 
Developing cacheable backend applications - Appdevcon 2019
Developing cacheable backend applications - Appdevcon 2019Developing cacheable backend applications - Appdevcon 2019
Developing cacheable backend applications - Appdevcon 2019
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with Transients
 
Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!
 

More from andrewnacin

Open Source (and you can too) - 2011 Teens in Tech Conference
Open Source (and you can too) - 2011 Teens in Tech ConferenceOpen Source (and you can too) - 2011 Teens in Tech Conference
Open Source (and you can too) - 2011 Teens in Tech Conferenceandrewnacin
 
WordCamp Columbus 2011 - What's Next for WordPress
WordCamp Columbus 2011 - What's Next for WordPressWordCamp Columbus 2011 - What's Next for WordPress
WordCamp Columbus 2011 - What's Next for WordPressandrewnacin
 
TEDxYouth@DowntownDC
TEDxYouth@DowntownDCTEDxYouth@DowntownDC
TEDxYouth@DowntownDCandrewnacin
 
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)andrewnacin
 
Hidden Features (WordPress DC)
Hidden Features (WordPress DC)Hidden Features (WordPress DC)
Hidden Features (WordPress DC)andrewnacin
 
Lightning Talk: Mistakes (WordCamp Phoenix 2011)
Lightning Talk: Mistakes (WordCamp Phoenix 2011)Lightning Talk: Mistakes (WordCamp Phoenix 2011)
Lightning Talk: Mistakes (WordCamp Phoenix 2011)andrewnacin
 
WordPress at Web Content Mavens (Jan. 2011)
WordPress at Web Content Mavens (Jan. 2011)WordPress at Web Content Mavens (Jan. 2011)
WordPress at Web Content Mavens (Jan. 2011)andrewnacin
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPandrewnacin
 
What's Next for WordPress at WordCamp Netherlands
What's Next for WordPress at WordCamp NetherlandsWhat's Next for WordPress at WordCamp Netherlands
What's Next for WordPress at WordCamp Netherlandsandrewnacin
 
What's Next for WordPress: WordCamp Birmingham 2010
What's Next for WordPress: WordCamp Birmingham 2010What's Next for WordPress: WordCamp Birmingham 2010
What's Next for WordPress: WordCamp Birmingham 2010andrewnacin
 
WordPress 3.0 at DC PHP
WordPress 3.0 at DC PHPWordPress 3.0 at DC PHP
WordPress 3.0 at DC PHPandrewnacin
 
Advanced and Hidden WordPress APIs
Advanced and Hidden WordPress APIsAdvanced and Hidden WordPress APIs
Advanced and Hidden WordPress APIsandrewnacin
 

More from andrewnacin (12)

Open Source (and you can too) - 2011 Teens in Tech Conference
Open Source (and you can too) - 2011 Teens in Tech ConferenceOpen Source (and you can too) - 2011 Teens in Tech Conference
Open Source (and you can too) - 2011 Teens in Tech Conference
 
WordCamp Columbus 2011 - What's Next for WordPress
WordCamp Columbus 2011 - What's Next for WordPressWordCamp Columbus 2011 - What's Next for WordPress
WordCamp Columbus 2011 - What's Next for WordPress
 
TEDxYouth@DowntownDC
TEDxYouth@DowntownDCTEDxYouth@DowntownDC
TEDxYouth@DowntownDC
 
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)
Ask Not What WordPress Can Do For You (Ignite - WordCamp Seattle)
 
Hidden Features (WordPress DC)
Hidden Features (WordPress DC)Hidden Features (WordPress DC)
Hidden Features (WordPress DC)
 
Lightning Talk: Mistakes (WordCamp Phoenix 2011)
Lightning Talk: Mistakes (WordCamp Phoenix 2011)Lightning Talk: Mistakes (WordCamp Phoenix 2011)
Lightning Talk: Mistakes (WordCamp Phoenix 2011)
 
WordPress at Web Content Mavens (Jan. 2011)
WordPress at Web Content Mavens (Jan. 2011)WordPress at Web Content Mavens (Jan. 2011)
WordPress at Web Content Mavens (Jan. 2011)
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
What's Next for WordPress at WordCamp Netherlands
What's Next for WordPress at WordCamp NetherlandsWhat's Next for WordPress at WordCamp Netherlands
What's Next for WordPress at WordCamp Netherlands
 
What's Next for WordPress: WordCamp Birmingham 2010
What's Next for WordPress: WordCamp Birmingham 2010What's Next for WordPress: WordCamp Birmingham 2010
What's Next for WordPress: WordCamp Birmingham 2010
 
WordPress 3.0 at DC PHP
WordPress 3.0 at DC PHPWordPress 3.0 at DC PHP
WordPress 3.0 at DC PHP
 
Advanced and Hidden WordPress APIs
Advanced and Hidden WordPress APIsAdvanced and Hidden WordPress APIs
Advanced and Hidden WordPress APIs
 

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

  • 1. The Otto & Nacin Show WordCamp San Francisco 2011 August 12, 2011
  • 2. Nacin Otto Contributing Developers to WordPress Tech Ninjas at Audrey Capital nacin@wordpress.org otto@wordpress.org @nacin @otto42
  • 3. Transients, Caching, and the Complexities of Multisite (A brief overview.)
  • 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.
  • 10. Object caching is ideal for: Caching an object (often a query result) to reduce overhead.
  • 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 caching backend like APC, Memcached, or WinCache.
  • 13. Object caching is not ideal if you need both data persistence and code portability. No knowledge of the server setup? Use transients.
  • 14. // Cache functions can take an expiration too wp_cache_set( $id, $activity_object , 'activity', $expires = 0 );
  • 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 there's no external object cache, WordPress uses the sitemeta table.
  • 19. It works in single-site! When not running multisite, it wraps *_transient().
  • 20. Global Cache Groups Cache groups ('activity') are site- specific. Tell WordPress what isn't.
  • 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 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' ); } );