Upload
david-binovec
View
200
Download
1
Embed Size (px)
DESCRIPTION
2nd Wordpress Conference in Hluboká nad Vltavou, CZ
Citation preview
Caching in WordPress
David Biň[email protected]
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
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
MySQL Query Cache
● only identical and deterministic SELECT requests
● inter session persistent● cache flushing after the change of relevant
data● non-distributed
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 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 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)
How to cache a page in WordPress
WP Super Cache - mod_rewrite
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 ...
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
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
● 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
Where is WP_Object_Cache used
● get_metadata (post, user, comment ...)● get_option (autoload options)● get_post, get_user, get_comment, get_term● transients● ...
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
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 ...
oEmbed cache using post_meta
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/
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