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 Slide

  2. I know what you are
    thinking

    View Slide

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

    View Slide

  4. Cache

    Rules

    Everything

    Around

    Me

    View Slide

  5. WordPress can

    get slow

    View Slide

  6. Database queries

    View Slide

  7. API requests

    View Slide

  8. Long running routines

    View Slide

  9. Once is usually
    enough

    View Slide

  10. Object caching can
    improve performance

    View Slide

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

    View Slide

  12. Making data easier to
    access on subsequent

    requests

    View Slide

  13. 1. Caching primer

    2. Caching patterns

    View Slide

  14. wp-content/object-cache.php

    View Slide

  15. wp_cache_init();

    View Slide

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

    View Slide

  17. Persistence

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. wp_cache_get($key,
    $prefix)

    View Slide

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

    View Slide

  25. View Slide

  26. Caching patterns

    View Slide

  27. On demand

    View Slide

  28. View Slide

  29. $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 Slide

  30. Stampeding herd

    Easy to implement

    View Slide

  31. On event

    View Slide

  32. View Slide

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

    View Slide

  34. $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 Slide

  35. No stampeding herd

    Must be able to listen to
    event

    View Slide

  36. On interval

    View Slide

  37. View Slide

  38. 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 Slide

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

    View Slide

  40. No stampeding herd

    Doesn’t need an event

    Relies on WP Cron

    View Slide

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

    View Slide

  42. Use stale data while
    generating new data

    View Slide

  43. View Slide

  44. $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 Slide

  45. 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 Slide

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

    View Slide

  47. $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 Slide

  48. No stampeding herd

    Easy to implement with
    library

    View Slide

  49. Tips for the Cacher

    View Slide

  50. Avoid front-end
    caching

    View Slide

  51. Always set TTL

    View Slide

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

    View Slide

  53. Invalidation first
    design

    View Slide

  54. #31245 sucks,

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

    View Slide

  55. View Slide

  56. https://speakerdeck.com/tollmanz

    @tollmanz

    View Slide