Caching in WordPress 
Tareq Hasan (@tareq_cse) 
http://tareq.weDevs.com
What is Caching? 
• Serve static content to the visitor without querying 
the database. 
• Not generated upon **every** re...
Type of Caching? 
• Page Cache - Whole page as static content 
• Query Cache - Cache SQL query results 
• Fragment Cache -...
WordPress Caching APIs 
Transients! 
- Persistent out of the box 
- Stored in wp_options 
get_transient() 
set_transient()...
Using Transients 
set_transient( $key, $value, $expiration ); 
<?php 
// Get any existing copy of our transient data 
$spe...
Object Cache 
• Not persistent. Means, valid for only one pageload 
• Requires a persistent backend. Such as APC, 
Memcach...
Object Cache Functions 
// set the cache with value 
wp_cache_add( $key, $data, $group, $expire ) 
wp_cache_set( $key, $da...
Using Object Cache 
$result = wp_cache_get( 'my_result' ); 
if ( false === $result ) { 
$result = $wpdb->get_results( $que...
Persistent Cache Plugins 
• W3 Total Cache 
• WP File Cache 
• Memcached Object Cache 
• APC Object Cache 
• WordPress Red...
Fragment Caching 
• Cache a fragment of a page 
• This could be your menubar, widgets and other 
query related parts. 
htt...
Browser Caching 
• HTTP Cache-Control 
• Expiry Headers 
• Entity Tags 
(W3-Total-Cache, WP Super Cache) 
http://www.mobif...
Opcode Cache 
php-apc + W3 Total Cache
Questions?
Upcoming SlideShare
Loading in...5
×

Caching in WordPress

670

Published on

Various caching techniques and methodology in WordPress

Published in: Software
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
670
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Caching in WordPress

  1. 1. Caching in WordPress Tareq Hasan (@tareq_cse) http://tareq.weDevs.com
  2. 2. What is Caching? • Serve static content to the visitor without querying the database. • Not generated upon **every** request • Reduce to API calls
  3. 3. Type of Caching? • Page Cache - Whole page as static content • Query Cache - Cache SQL query results • Fragment Cache - Cache a piece of page • Browser Cache - Cache page assets • Opcode Cache - PHP executes faster
  4. 4. WordPress Caching APIs Transients! - Persistent out of the box - Stored in wp_options get_transient() set_transient() delete_transient() Object Cache! - Not persistent without a persistent backend - Storage depends on the backend wp_cache_add() wp_cache_set() wp_cache_get() wp_cache_delete()
  5. 5. Using Transients set_transient( $key, $value, $expiration ); <?php // Get any existing copy of our transient data $special_query = get_transient( 'special_query' ); if ( false === $special_query_results ) { $special_query = new WP_Query( 'cat=5&order=random' ); set_transient( 'special_query', $special_query, 12 * HOUR_IN_SECONDS ); } // Use the data like you would have normally... Queries only after each 12 hours
  6. 6. Object Cache • Not persistent. Means, valid for only one pageload • Requires a persistent backend. Such as APC, Memcached. • Plugins like W3 Total Cache, Memcached Object Cache helps. • Used Extensively in WordPress • Cache objects can be grouped
  7. 7. Object Cache Functions // set the cache with value wp_cache_add( $key, $data, $group, $expire ) wp_cache_set( $key, $data, $group, $expire ) // retrieve the value from cache wp_cache_get( $key, $group ) // clears data from the cache for the given key. wp_cache_replace( $key, $data, $group, $expire ) // clears all cached data. wp_cache_flush()
  8. 8. Using Object Cache $result = wp_cache_get( 'my_result' ); if ( false === $result ) { $result = $wpdb->get_results( $query ); wp_cache_set( 'my_result', $result ); } // Do something with $result; http://codex.wordpress.org/Class_Reference/WP_Object_Cache
  9. 9. Persistent Cache Plugins • W3 Total Cache • WP File Cache • Memcached Object Cache • APC Object Cache • WordPress Redis Backend
  10. 10. Fragment Caching • Cache a fragment of a page • This could be your menubar, widgets and other query related parts. https://gist.github.com/markjaquith/2653957
  11. 11. Browser Caching • HTTP Cache-Control • Expiry Headers • Entity Tags (W3-Total-Cache, WP Super Cache) http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/
  12. 12. Opcode Cache php-apc + W3 Total Cache
  13. 13. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×