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

  • View
    3.652

  • Download
    0

Embed Size (px)

DESCRIPTION

Using what I've learned working on the WordPress.com VIP platform, this presentation discusses caching techniques applicable to WordPress installations of almost any size. Presented November 28, 2011 at the Boston WordPress Meetup.

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

  • 1. Caching, Scaling, andWordPress.comVIPErick HitterBoston WordPress Meetup, November 28

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. Caching Serving static content to a visitor rather thansomething 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. Fragment Caching BenefitsAllow dynamic and static content to coexist 5. Fragment Caching BenefitsCommon elements can be reused throughout a site 6. Fragment Caching BenefitsReduce calls to APIs 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 pluginscertain internal functions set_, get_, and capabilities Used extensively withindelete_transient() WordPress Cache objects can be grouped wp_cache_add(), _set, _g et, _delete 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. 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. Unpredictable Keys Object caching doesnt provide a way to clearall caches in a given group. Therefore, if the cache key isunpredictable, how can we clear it? For example, a list of recent posts to bedisplayed on an individual post, but thatexcludes the current post. 11. Unpredictable Keys: Recent Posts 12. Unpredictable Keys: Cached Array 13. Unpredictable Keys: Cached ArrayProsCons Cache can easily be Object cachingcleared because a singleconfiguration may limit sizeobject with a predictable of individual cache objects.key is set. Array corruption could Cache is only rebuilt ifinvalidate an entire cachespecific post ID/quantity object.key is absent from array. Array can become bloated Better for cache elements if different quantities arethat are reliably small.used simultaneously. Allows for checkingexistence of variouskeys, such as in a loop. 14. Unpredictable Keys: Incrementor 15. Where We Use Fragment Caching Custom loops Anytime a lot of data must be retrieved from WordPressand parsed. Most situations where WP_Query generates a subqueryoutside of the main query. Almost anything that is reused across multiple pages. 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. category__not_invspost__not_in 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. Menu Caching: No Active States 20. Menu Caching: Active States 21. Menu Caching: Keys & Clearing get_queried_object_id() returns an integer representingthe post ID or term ID.Front page and custom post type archives return 0. Menu caches must be cleared when four differentactions fire to ensure consistency:o wp_update_nav_menuo wp_update_nav_menu_itemo wp_delete_nav_menuo wp_setup_nav_menu_item 22. query_posts() vspre_get_postsquery_posts() pre_get_posts Function provided to Action used to modifymodify main query.any query. Runs after main query Runs before every queryhas already executed. executes. 23. How Does This Factor Into Our WorkOn WordPress.com VIP? Page caches only last for five minutes. No page caching for logged-in users. Sites that publish with great frequency trigger regularinvalidations of homepage and category pages. Web servers outnumber database servers. Want to know more about WordPress.cominfrastructure? Check out http://goo.gl/lYpJH. 24. Questions?erick@thinkoomph.com@ethitterhttp://www.slideshare.net/ethitter