Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Caching, Scaling, and What I've Learned from VIP

Using what I've learned working on the VIP platform, this presentation discusses caching techniques applicable to WordPress installations of almost any size.

Presented November 28, 2011 at the Boston WordPress Meetup.

  • Login to see the comments

Caching, Scaling, and What I've Learned from VIP

  1. 1. Caching, Scaling, andWordPress.comVIPErick HitterBoston WordPress Meetup, November 28
  2. 2. Why Should You Care AboutAnything I Have to Say?Erick HitterLead WordPress developer with Oomph!@ethitterhttp://www.ethitter.comhttp://www.thinkoomph.comDisclaimer: I started this in Google Docs, then had to switch toPowerPoint, which explains some of the wonky formatting.
  3. 3. Caching• Serving static content to a visitor rather than something generated upon request.• Two major types in WordPress context: o Page  Whole page is static and, therefore, could be outdated. o Fragment  Cache pieces of a page rather than the entire page.
  4. 4. Fragment Caching BenefitsAllow dynamic and static content to coexist
  5. 5. Fragment Caching BenefitsCommon elements can be reused throughout a site
  6. 6. Fragment Caching BenefitsReduce calls to APIs
  7. 7. WordPress Native Caching APIsTransients Object Cache• Persistent out of the box • Not persistent without a• Stored in wp_options: plugin, such as W3 Total Cache _transient_{key}• WordPress uses for • Storage depends on servers and plugins certain internal functions• set_, get_, and • capabilities Used extensively within delete_transient() WordPress • Cache objects can be grouped • wp_cache_add(), _set, _g et, _delete
  8. 8. Fragment Caching Basics: Creating • wp_cache_get() returns a boolean false if the requested cache isnt set or has expired. • Everything that is to be cached must be accessible via a function that returns its results rather than echoing them, otherwise output buffering is needed. • wp_cache_add() will not overwrite an existing, unexpired cache, whereas wp_cache_set() does.
  9. 9. Fragment Caching Basics: ClearingThisabove example clears a cache when anything is published or somethingthat is published is modified. The "something" could be a post, page, orcustom post type object.If, instead, the cache should be rebuilt when the same eventoccurs, wp_cache_delete() could be replaced with generate_cached_output().
  10. 10. Unpredictable Keys• Object caching doesn’t provide a way to clear all caches in a given group.• Therefore, if the cache key is unpredictable, how can we clear it?• For example, a list of recent posts to be displayed on an individual post, but that excludes the current post.
  11. 11. Unpredictable Keys: Recent Posts
  12. 12. Unpredictable Keys: Cached Array
  13. 13. Unpredictable Keys: Cached ArrayPros Cons• Cache can easily be • Object caching cleared because a single configuration may limit size object with a predictable of individual cache objects. key is set. • Array corruption could• Cache is only rebuilt if invalidate an entire cache specific post ID/quantity object. key is absent from array. • Array can become bloated• Better for cache elements if different quantities are that are reliably small. used simultaneously.• Allows for checking existence of various keys, such as in a loop.
  14. 14. Unpredictable Keys: Incrementor
  15. 15. Where We Use Fragment Caching• Custom loops• Anytime a lot of data must be retrieved from WordPress and parsed.• Most situations where WP_Query generates a subquery outside of the main query.• Almost anything that is reused across multiple pages.
  16. 16. category__not_invspost__not_inSELECT … WHERE 1=1 ANDwp_posts.ID NOT IN (SELECT tr.object_id FROM wp_term_relationshipsAS tr INNER JOIN wp_term_taxonomy AS tt ONtr.term_taxonomy_id = tt.term_taxonomy_id WHEREtt.taxonomy = category AND tt.term_id IN(167) ) …
  17. 17. category__not_invspost__not_in
  18. 18. category__not_invspost__not_inSELECT … WHERE 1=1 ANDwp_posts.ID NOT IN (SELECT tr.object_id FROM wp_term_relationshipsAS tr INNER JOIN wp_term_taxonomy AS tt ONtr.term_taxonomy_id = tt.term_taxonomy_id WHEREtt.taxonomy = category AND tt.term_id IN(167) ) …SELECT … WHERE 1=1 ANDwp_posts.ID NOT IN (1,2,3,4,5 ) …
  19. 19. Menu Caching: No Active States
  20. 20. Menu Caching: Active States
  21. 21. Menu Caching: Keys & Clearing• get_queried_object_id() returns an integer representing the post ID or term ID.• Front page and custom post type archives return 0.• Menu caches must be cleared when four different actions fire to ensure consistency: o wp_update_nav_menu o wp_update_nav_menu_item o wp_delete_nav_menu o wp_setup_nav_menu_item
  22. 22. query_posts() vspre_get_postsquery_posts() pre_get_posts• Function provided to • Action used to modify modify main query. any query.• Runs after main query • Runs before every query has already executed. executes.
  23. 23. How Does This Factor Into Our WorkOn VIP?• Page caches only last for five minutes.• No page caching for logged-in users.• Sites that publish with great frequency trigger regular invalidations of homepage and category pages.• Web servers outnumber database servers.• Want to know more about infrastructure? Check out
  24. 24. Questions?