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. The Many Challenges
    of Object Caching in
    WordPress
    Zack Tollman @tollmanz

    View full-size slide

  2. I know what you are
    thinking

    View full-size slide

  3. “There are two hard
    things in computer
    science: cache
    invalidation, and
    naming things”

    View full-size slide

  4. Cache

    Rules

    Everything

    Around

    Me

    View full-size slide

  5. WordPress can

    get slow

    View full-size slide

  6. Database queries

    View full-size slide

  7. API requests

    View full-size slide

  8. Long running routines

    View full-size slide

  9. Once is usually
    enough

    View full-size slide

  10. Object caching can
    improve performance

    View full-size slide

  11. http://www.fs.usda.gov/

    View full-size slide

  12. Making data easier to
    access on subsequent

    requests

    View full-size slide

  13. 1. Caching primer

    2. Caching patterns

    View full-size slide

  14. wp-content/object-cache.php

    View full-size slide

  15. wp_cache_init();

    View full-size slide

  16. function wp_cache_init() {
    global $wp_object_cache;
    $wp_object_cache = new WP_Object_Cache();
    }

    View full-size slide

  17. APC
    https://wordpress.org/plugins/apc/

    View full-size slide

  18. Memcached
    https://github.com/tollmanz/
    wordpress-pecl-memcached-object-cache
    https://wordpress.org/plugins/memcached/

    View full-size slide

  19. “There are two hard
    things in computer
    science: cache
    invalidation, and
    naming things”

    View full-size slide

  20. Redis
    https://github.com/ericmann/Redis-Object-Cache/

    View full-size slide

  21. wp_cache_set($key,
    $value, $prefix, $ttl)

    View full-size slide

  22. wp_cache_get($key,
    $prefix)

    View full-size slide

  23. Grokking the
    WordPress Object Cache
    https://www.tollmanz.com/grokking-the-wp-object-cache/

    View full-size slide

  24. Caching patterns

    View full-size slide

  25. $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
    }

    View full-size slide

  26. Stampeding herd

    Easy to implement

    View full-size slide

  27. add_action( ‘save_post’, function($post_id) {
    $value = generate_related( $post_id );
    wp_cache_set( $post_id, $value, ‘related’ );
    } );

    View full-size slide

  28. $related = wp_cache_get( get_the_ID(),
    ‘related’ );
    if ( empty( $related ) ) {
    $related = get_default_related();
    }
    foreach ( $related as $related_post ) {
    // Display the related post
    }

    View full-size slide

  29. No stampeding herd

    Must be able to listen to
    event

    View full-size slide

  30. 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 );
    }

    View full-size slide

  31. $tweets = wp_cache_get( ‘tweets’ );
    if ( empty( $tweets ) || ‘none-
    available’ === $tweets) {
    $tweets = get_default_tweets();
    }
    foreach ( $tweets as $tweets ) {
    // Get your social on
    }

    View full-size slide

  32. No stampeding herd

    Doesn’t need an event

    Relies on WP Cron

    View full-size slide

  33. Stale while revalidate
    https://tools.ietf.org/html/rfc5861

    View full-size slide

  34. Use stale data while
    generating new data

    View full-size slide

  35. $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
    }

    View full-size slide

  36. 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’ );
    }

    View full-size slide

  37. TLC Transients
    https://github.com/markjaquith/WP-TLC-Transients

    View full-size slide

  38. $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 );
    }

    View full-size slide

  39. No stampeding herd

    Easy to implement with
    library

    View full-size slide

  40. Tips for the Cacher

    View full-size slide

  41. Avoid front-end
    caching

    View full-size slide

  42. Always set TTL

    View full-size slide

  43. Group level purges
    don’t exist
    https://www.tollmanz.com/invalidation-schemes/

    View full-size slide

  44. Invalidation first
    design

    View full-size slide

  45. #31245 sucks,

    a lot
    https://core.trac.wordpress.org/ticket/31245

    View full-size slide

  46. https://speakerdeck.com/tollmanz

    @tollmanz

    View full-size slide