19
Caching in WordPress David Biňovec [email protected] m

Caching in WordPress

Embed Size (px)

DESCRIPTION

2nd Wordpress Conference in Hluboká nad Vltavou, CZ

Citation preview

Page 1: Caching in WordPress

Caching in WordPress

David Biň[email protected]

Page 2: Caching in WordPress

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 APIo WP_Object_Cache a wp_cache funkceo oEmbed cache

Page 3: Caching in WordPress

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 everythingo whole pages, objectso external API requests, resource demanding

operations ...● … and even more

o MySQL Query Cacheo OpCode

Page 4: Caching in WordPress

MySQL Query Cache

● only identical and deterministic SELECT requests

● inter session persistent● cache flushing after the change of relevant

data● non-distributed

Page 5: Caching in WordPress

Cache engines

● Memcachedo distributed (server accessible via telnet)o key-value pairs saved in a memoryo 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 fileo eg. mod_rewrite (WP Super Cache), CDN

Page 6: Caching in WordPress

Page Level vs. Object Cache

● Page level cache (e.g. WP Super Cache)o saves only whole pageso 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 pageso works also for logged in userso has affect even on WordPress administration

Page 7: Caching in WordPress

Page Level Cache

● HTML files saved on hard driveo loading via PHP or mod_rewrite (WP Super Cache)

● memcached (Batcache)o can be used even without PHP (NginX, Apache ...)

● CDNo cloudflare (W3TC)o anything else (WP Super Cache)

Page 8: Caching in WordPress

How to cache a page in WordPress

Page 9: Caching in WordPress

WP Super Cache - mod_rewrite

Page 10: Caching in WordPress

Object Cache

● reusable objectso either in terms of a single requestso or, and with better performance, in multiple requests

● object is created via one or multiple SELECTso is an output from multiple functions, methods ...

● For example:o WP_User (capabilities, user_meta)o WP_Post ...

Page 11: Caching in WordPress

Is WordPress cache-ready? It is!

● Transients APIo set_transient, get_transient, delete_transiento set_site_transient, get_site_transient …

● WP_Object_Cache and wp_cache functionso wp-includes/cache.php

wp_cache_add, wp_cache_set, wp_cache_replace

wp_cache_get wp_cache_delete, wp_cache_flush

Page 12: Caching in WordPress

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

Page 13: Caching in WordPress

● core implementation of this class uses global variable for saving datao thus, from nature, is cached only per requests

● has it’s meaning for repeated database requestso you can call get_post_meta multiple time for one

request, but you reach database once only

WP_Object_Cache

Page 14: Caching in WordPress

Where is WP_Object_Cache used

● get_metadata (post, user, comment ...)● get_option (autoload options)● get_post, get_user, get_comment, get_term● transients● ...

Page 15: Caching in WordPress

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

Page 16: Caching in WordPress

WP_Object_Cache dropins

● dropinso wp-content/advanced-cache.phpo wp-content/object-cache.php

● object-cache.phpo prevents loading of wp-includes/wp_cache.phpo redefines WP_Object_Cache and wp_cache

functionso uses any of external caching engines

memcache, APC, filesystem ...

Page 17: Caching in WordPress

oEmbed cache using post_meta

Page 18: Caching in WordPress

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 dropinshttp://wordpress.org/plugins/memcached/http://wordpress.org/plugins/apc/

Page 19: Caching in WordPress

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