Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Many Challenges of Object Caching in WordPress

The Many Challenges of Object Caching in WordPress

It's no secret that WordPress can be slow and tough to scale. A key ingredient to scaling WordPress is setting up object caching. Object caching reduces the number of heavy database calls that WordPress needs to satisfy web requests. My talk will discuss the many details of how WordPress loads a third-party object cache, the API that developers can use, and design patterns for effective object caching. Finally, I will share my personal experiences in using object caching to help scale enterprise-level WordPress installations.

Zack Tollman

November 18, 2015
Tweet

More Decks by Zack Tollman

Other Decks in Technology

Transcript

  1. $related = wp_cache_get( get_the_ID(), ‘related’); if ( false === $related

    ) { $related = generate_related( get_the_ID() ); wp_cache_set( get_the_ID(), $value, ‘related’, 86400 ); } foreach ( $related as $related_post ) { // Display the related post }
  2. $related = wp_cache_get( get_the_ID(), ‘related’ ); if ( empty( $related

    ) ) { $related = get_default_related(); } foreach ( $related as $related_post ) { // Display the related post }
  3. add_action( ‘init’, function() { wp_schedule_event( time(), 'hourly', ‘update_tweets’ ); }

    ); function update_tweets() { $tweets = generate_tweets(); if ( empty( $tweets ) ) { $tweets = ‘none-available’; } wp_cache_set( ‘tweets’, $tweets ); }
  4. $tweets = wp_cache_get( ‘tweets’ ); if ( empty( $tweets )

    || ‘none- available’ === $tweets) { $tweets = get_default_tweets(); } foreach ( $tweets as $tweets ) { // Get your social on }
  5. $related = wp_cache_get( get_the_ID(), ‘related’ ); if ( false ===

    $value ) { queue_related( get_the_ID() ); $related = wp_cache_get( get_the_ID(), ‘related-stale’ ); } foreach ( $related as $related_post ) { // Display the related post }
  6. function queue_related( $post_id ) { $lock = wp_cache_get( $post_id, ‘related-

    lock’ ); if ( $lock ) { return; } else { wp_cache_set( $post_id, 1, ‘related-lock’ ); } $related = generate_related( $post_id ); wp_cache_set($post_id, $related, ‘related’, 300); wp_cache_set($post_id, $related, ‘related- stale’); wp_cache_delete( $post_id, ‘related-lock’ ); }
  7. $related = tlc_transient( ‘related-’ . $post_id ) ->updates_with( ‘queue_related’, [

    $post_id ] ) ->expires_in( 300 ) ->get(); foreach ( $related as $related_post ) { // Display the post } ———————————————— function queue_related( $post_id ) { return generate_related( $post_id ); }