• Like
Caching in WordPress
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Caching in WordPress


2nd Wordpress Conference in Hluboká nad Vltavou, CZ

2nd Wordpress Conference in Hluboká nad Vltavou, CZ

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide
  • http://dev.mysql.com/doc/refman/5.1/en/query-cache.html
  • http://memcached.org/
  • http://nginx.org/en/docs/http/ngx_http_memcached_module.html
  • This is just a pseudo non working code showing principles of using PHP’s output buffer with a callback and advanced-cache.php dropin.
  • Example of mod_rewrites generated by WP Super Cache plugin for serving previously cached (saved) page
  • http://codex.wordpress.org/Transients_API
  • http://codex.wordpress.org/Transients_API
  • http://codex.wordpress.org/Class_Reference/WP_Object_Cache
  • This is a stripped code from the WordPress core


  • 1. Caching in WordPress David Biňovec david_binovec@parade.com
  • 2. Presentation outline ● What cache is, what to cache ● Caching options in PHP and MySQL o Memcached, Alternative PHP Cache (APC) o MySQL Query Cache ● Page level a Object Cache ● Is WordPress cache-ready? o Transients API o WP_Object_Cache a wp_cache funkce o oEmbed cache
  • 3. What is a cache and what to cache? ● transparently stored data so that future requests for that data can be served faster ● You can cache almost everything o whole pages, objects o external API requests, resource demanding operations ... ● … and even more o MySQL Query Cache o OpCode
  • 4. MySQL Query Cache ● only identical and deterministic SELECT requests ● inter session persistent ● cache flushing after the change of relevant data ● non-distributed
  • 5. Cache engines ● Memcached o distributed (server accessible via telnet) o key-value pairs saved in a memory o PECL extension for PHP (memcached / memcache) ● Alternative PHP Cache (APC) o Opcode and user cache (PHP <= 5.4), APCu (PHP 5.5) o non-distributed ● Flat file o eg. mod_rewrite (WP Super Cache), CDN
  • 6. Page Level vs. Object Cache ● Page level cache (e.g. WP Super Cache) o saves only whole pages o every single page has to be generated on it’s own with all database requests, API call etc.. o usually is not available for non logged in users ● Object cache (APC, Memcached, W3TC) o reusable for a generation of multiple pages o works also for logged in users o has affect even on WordPress administration
  • 7. Page Level Cache ● HTML files saved on hard drive o loading via PHP or mod_rewrite (WP Super Cache) ● memcached (Batcache) o can be used even without PHP (NginX, Apache ...) ● CDN o cloudflare (W3TC) o anything else (WP Super Cache)
  • 8. How to cache a page in WordPress
  • 9. WP Super Cache - mod_rewrite
  • 10. Object Cache ● reusable objects o either in terms of a single requests o or, and with better performance, in multiple requests ● object is created via one or multiple SELECTs o is an output from multiple functions, methods ... ● For example: o WP_User (capabilities, user_meta) o WP_Post ...
  • 11. Is WordPress cache-ready? It is! ● Transients API o set_transient, get_transient, delete_transient o set_site_transient, get_site_transient … ● WP_Object_Cache and wp_cache functions o wp-includes/cache.php  wp_cache_add, wp_cache_set, wp_cache_replace  wp_cache_get  wp_cache_delete, wp_cache_flush
  • 12. Transients API ● temporary saving of cached data ● uses wp_options table by default o add_option with param autoload = “no”  if no expiration is set, then autoload = “yes” o adds two entries to the database  transient value  maximal expiration time (vs. guaranted time) ● it’s guaranted by default, but saving into database can be overriden by memcache server, for instance
  • 13. ● core implementation of this class uses global variable for saving data o thus, from nature, is cached only per requests ● has it’s meaning for repeated database requests o you can call get_post_meta multiple time for one request, but you reach database once only WP_Object_Cache
  • 14. Where is WP_Object_Cache used ● get_metadata (post, user, comment ...) ● get_option (autoload options) ● get_post, get_user, get_comment, get_term ● transients ● ...
  • 15. wp_cache functions ● wp_cache_set($key, $data, $group, $expire) o no expiration by default ($expire = 0) ● wp_cache_get( $key, $group ) o group allows to use same keys across multiple groups
  • 16. WP_Object_Cache dropins ● dropins o wp-content/advanced-cache.php o wp-content/object-cache.php ● object-cache.php o prevents loading of wp-includes/wp_cache.php o redefines WP_Object_Cache and wp_cache functions o uses any of external caching engines  memcache, APC, filesystem ...
  • 17. oEmbed cache using post_meta
  • 18. WordPress Cache Plugins ● WP Super Cache http://wordpress.org/plugins/wp-super- cache/ o page level cache using mod_rewrite ● W3 Total Cache http://wordpress.org/plugins/w3-total-cache/ o multiple object-cache engines ● Batcache http://wordpress.org/plugins/batcache/ ● object-cache.php dropins http://wordpress.org/plugins/memcached/ http://wordpress.org/plugins/apc/
  • 19. Understanding Cache in WP - Summary ● Memcached, APC, MySQL Query Cache ● Page level vs. Object Cache o go for a mix of page level and object cache ● Transients API ● WP_Object_Cache a wp_cache funkce o dropins: object-cache.php, advanced-cache.php ● oEmbed cache using post_meta table