@mlteal | #wceu | 2018
The Balancing Act of
Caching in WordPress
@mlteal | #wceu | 2018
Maura Teal (@mlteal)
Software Engineer at Pagely
Previously Sr Dev at Time Inc.
Emoji and meme enthusiast:
github.com/mlteal/custom-emoji
@mlteal | #wceu | 2018
Caching?
@mlteal | #wceu | 2018
Caching?
• Data stored “closer” to the end user*
• *or closer to where it will be utilized often
@mlteal | #wceu | 2018
Cache Layer
Caching
Request 1
💻
📜
Fetch data
📜
Still fetching
📜
More data…
Request 2, 3, etc
💻
@mlteal | #wceu | 2018
Caching?
• Object Caching (storage in memory):
• Memcached
• Redis
• …
@mlteal | #wceu | 2018
Caching what?
• Object Caching (storage in memory):
• Memcached
• Redis
• Alternative storage mechanisms:
• Database storage (Transients)
• Custom tables/meta
• Local (browser level) storage
@mlteal | #wceu | 2018
Data is not forever
@mlteal | #wceu | 2018
Quick Win
*
@mlteal | #wceu | 2018
*Except logged-in content
@mlteal | #wceu | 2018
*Except frequently-changing content
@mlteal | #wceu | 2018
@mlteal | #wceu | 2018
What to Cache
@mlteal | #wceu | 2018
Transients API
• get_transient( $key );
• set_transient( $key, $value, $expires );
• delete_transient( $key );
@mlteal | #wceu | 2018
WP’s Caching API’s
get_my_data() {

$data = get_transient( ‘my_transient_name’ );
if ( false === $data ) { // Transient was expired/hasn’t been set.
$data = fetch_my_data(); // Pull data from original source.
set_transient( ‘my_transient_name’, $data,
2 * HOUR_IN_SECONDS );
}
return $data; 

}
@mlteal | #wceu | 2018
WP’s Caching API’s
• wp_cache_set( ‘my_key’, $data, $group, $expire );
• wp_cache_get( ‘my_key’, $group, $force, $found );
@mlteal | #wceu | 2018
set_transient( ‘my_item’, $data, 2 * HOUR_IN_SECONDS );
==
wp_cache_set( ‘my_item’, $data, ‘transient’, 2 * HOUR_IN_SECONDS);
WP’s Caching API’s
@mlteal | #wceu | 2018
Do I need to cache more?
@mlteal | #wceu | 2018
Do I need to cache more?
• Performance indicators 

(Speed, CPU & memory use)
• Traffic indicators

(Any expected upticks?)
• Use of external API’s
• Content and access goals
@mlteal | #wceu | 2018
Do I need to cache more?
• Performance indicators 

(Speed, CPU & memory use)
@mlteal | #wceu | 2018
Do I need to cache more?
• Traffic indicators

(Any expected upticks?)
@mlteal | #wceu | 2018
Do I need to cache more?
• Use of external API’s
@mlteal | #wceu | 2018
Browser Cache API
@mlteal | #wceu | 2018
Browser Cache API
• Fully frontend-focused solution.
• Store data directly in browser's cache.
• Use sparingly. With great power…
• Depending on the API, not fully supported across
browsers.
@mlteal | #wceu | 2018
Browser Cache API
caches.open('my-cache').then((cache) => {
  // do something with cache...
});
@mlteal | #wceu | 2018
Browser Cache API: 

Further Reading
• https://developer.mozilla.org/en-US/docs/Web/API/
Cache
• https://developers.google.com/web/fundamentals/
instant-and-offline/web-storage/cache-api
• https://davidwalsh.name/cache
@mlteal | #wceu | 2018
Lessons Learned at Scale
@mlteal | #wceu | 2018
Lessons Learned at Scale
• Cache warming is real
• There is such a thing as caching too much
• Significantly reducing load is great, but still need
to handle clearing or refreshing data without
bringing the site down!
@mlteal | #wceu | 2018
Lessons Learned at Scale
• Load test before big events
• Knowledge is power—anyone working at scale
should load test
@mlteal | #wceu | 2018
Lessons Learned at Scale
• Expired != deleted
• Unless there’s a specific garbage collection
process in place, cached data, especially transients
in the DB, have a way of hanging around.
@mlteal | #wceu | 2018
Lessons Learned at Scale
• Cache all you want, but you still need to write
performant code
@mlteal | #wceu | 2018
Maura Teal
@mlteal

Balancing Act of Caching, WordCamp Europe 2018

  • 1.
    @mlteal | #wceu| 2018 The Balancing Act of Caching in WordPress
  • 2.
    @mlteal | #wceu| 2018 Maura Teal (@mlteal) Software Engineer at Pagely Previously Sr Dev at Time Inc. Emoji and meme enthusiast: github.com/mlteal/custom-emoji
  • 3.
    @mlteal | #wceu| 2018 Caching?
  • 4.
    @mlteal | #wceu| 2018 Caching? • Data stored “closer” to the end user* • *or closer to where it will be utilized often
  • 5.
    @mlteal | #wceu| 2018 Cache Layer Caching Request 1 💻 📜 Fetch data 📜 Still fetching 📜 More data… Request 2, 3, etc 💻
  • 6.
    @mlteal | #wceu| 2018 Caching? • Object Caching (storage in memory): • Memcached • Redis • …
  • 7.
    @mlteal | #wceu| 2018 Caching what? • Object Caching (storage in memory): • Memcached • Redis • Alternative storage mechanisms: • Database storage (Transients) • Custom tables/meta • Local (browser level) storage
  • 8.
    @mlteal | #wceu| 2018 Data is not forever
  • 9.
    @mlteal | #wceu| 2018 Quick Win *
  • 10.
    @mlteal | #wceu| 2018 *Except logged-in content
  • 11.
    @mlteal | #wceu| 2018 *Except frequently-changing content
  • 12.
  • 13.
    @mlteal | #wceu| 2018 What to Cache
  • 14.
    @mlteal | #wceu| 2018 Transients API • get_transient( $key ); • set_transient( $key, $value, $expires ); • delete_transient( $key );
  • 15.
    @mlteal | #wceu| 2018 WP’s Caching API’s get_my_data() {
 $data = get_transient( ‘my_transient_name’ ); if ( false === $data ) { // Transient was expired/hasn’t been set. $data = fetch_my_data(); // Pull data from original source. set_transient( ‘my_transient_name’, $data, 2 * HOUR_IN_SECONDS ); } return $data; 
 }
  • 16.
    @mlteal | #wceu| 2018 WP’s Caching API’s • wp_cache_set( ‘my_key’, $data, $group, $expire ); • wp_cache_get( ‘my_key’, $group, $force, $found );
  • 17.
    @mlteal | #wceu| 2018 set_transient( ‘my_item’, $data, 2 * HOUR_IN_SECONDS ); == wp_cache_set( ‘my_item’, $data, ‘transient’, 2 * HOUR_IN_SECONDS); WP’s Caching API’s
  • 18.
    @mlteal | #wceu| 2018 Do I need to cache more?
  • 19.
    @mlteal | #wceu| 2018 Do I need to cache more? • Performance indicators 
 (Speed, CPU & memory use) • Traffic indicators
 (Any expected upticks?) • Use of external API’s • Content and access goals
  • 20.
    @mlteal | #wceu| 2018 Do I need to cache more? • Performance indicators 
 (Speed, CPU & memory use)
  • 21.
    @mlteal | #wceu| 2018 Do I need to cache more? • Traffic indicators
 (Any expected upticks?)
  • 22.
    @mlteal | #wceu| 2018 Do I need to cache more? • Use of external API’s
  • 23.
    @mlteal | #wceu| 2018 Browser Cache API
  • 24.
    @mlteal | #wceu| 2018 Browser Cache API • Fully frontend-focused solution. • Store data directly in browser's cache. • Use sparingly. With great power… • Depending on the API, not fully supported across browsers.
  • 25.
    @mlteal | #wceu| 2018 Browser Cache API caches.open('my-cache').then((cache) => {   // do something with cache... });
  • 26.
    @mlteal | #wceu| 2018 Browser Cache API: 
 Further Reading • https://developer.mozilla.org/en-US/docs/Web/API/ Cache • https://developers.google.com/web/fundamentals/ instant-and-offline/web-storage/cache-api • https://davidwalsh.name/cache
  • 27.
    @mlteal | #wceu| 2018 Lessons Learned at Scale
  • 28.
    @mlteal | #wceu| 2018 Lessons Learned at Scale • Cache warming is real • There is such a thing as caching too much • Significantly reducing load is great, but still need to handle clearing or refreshing data without bringing the site down!
  • 29.
    @mlteal | #wceu| 2018 Lessons Learned at Scale • Load test before big events • Knowledge is power—anyone working at scale should load test
  • 30.
    @mlteal | #wceu| 2018 Lessons Learned at Scale • Expired != deleted • Unless there’s a specific garbage collection process in place, cached data, especially transients in the DB, have a way of hanging around.
  • 31.
    @mlteal | #wceu| 2018 Lessons Learned at Scale • Cache all you want, but you still need to write performant code
  • 32.
    @mlteal | #wceu| 2018 Maura Teal @mlteal