Slide 1

Slide 1 text

The Many Challenges of Object Caching in WordPress Zack Tollman @tollmanz

Slide 2

Slide 2 text

I know what you are thinking

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Cache Rules Everything Around Me

Slide 5

Slide 5 text

WordPress can get slow

Slide 6

Slide 6 text

Database queries

Slide 7

Slide 7 text

API requests

Slide 8

Slide 8 text

Long running routines

Slide 9

Slide 9 text

Once is usually enough

Slide 10

Slide 10 text

Object caching can improve performance

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Making data easier to access on subsequent requests

Slide 13

Slide 13 text

1. Caching primer 2. Caching patterns

Slide 14

Slide 14 text

wp-content/object-cache.php

Slide 15

Slide 15 text

wp_cache_init();

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Persistence

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

wp_cache_get($key, $prefix)

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Caching patterns

Slide 27

Slide 27 text

On demand

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Stampeding herd Easy to implement

Slide 31

Slide 31 text

On event

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No stampeding herd Must be able to listen to event

Slide 36

Slide 36 text

On interval

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

No stampeding herd Doesn’t need an event Relies on WP Cron

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Use stale data while generating new data

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

No stampeding herd Easy to implement with library

Slide 49

Slide 49 text

Tips for the Cacher

Slide 50

Slide 50 text

Avoid front-end caching

Slide 51

Slide 51 text

Always set TTL

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Invalidation first design

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

https://speakerdeck.com/tollmanz @tollmanz