How to use w3 total cache for wordpress

W3 total cache by Frederick Townes is probably the best wordpress cache plugin. Setting it up for best outcome can take sometime. But once you get a hang of it, you will find it awesome.

Here are some things to note about the plugin:

  1. The plugin install object-cache.php, db.php and advanced-cache.php in wp-content directory. It expects you to make wp-content directory writable temporarily for installation. wp-content/cache is used to store the cached data.
  2. The plugin also has support for memcached. I think its best to install memcached server for best outcome of the plugin.
  3. The plugin need the following entry in wp-config.php file
    define('WP_CACHE', true);
  4. To install w3 total cache plugin no other similar caching plugin should be present.

The plugin has lots of features and these all can be used for site performance improvements.  The impact of some of these is very high and immediately notable. I think the page cache feature is probably the most powerful feature. Here are some of the thing I did using this plugin:

Page cache


If you just enable page cache, you can probably get 80% of the plugin benefits. This is one feature alone if implemented correctly can give you huge improvements. Here is how this feature can be used:

  1. The plugin lets you use disk, opcode or memcache for caching purpose. If number of blogs on your site is not too high (probably < 1000), its best to use memory based cache like memcache. I got awesome results with memcache. I’m using memcached server with 64MB memory for cache on AWS Ubuntu micro instance. I’m seeing server side page latency close to 20-50 milliseconds. Earlier it used to be ~300 milliseconds. Note that this is server side latency and does not include network latency time, rendering time, etc.
  2. Page gets cached on first hit. This is good enough if your site has lots of traffic. Only the first user faces high latency within cache valid period. But its best to use cache preload feature of the plugin.
    w3-total-cache-page-cache-preload-settingsThe plugin lets you specify an update interval and pages per interval. It takes all the urls in sitemap sorted by priority and processes them in batch. Its best to keep a small batch spread out throughout the day instead of loading in burst.
  3. By default wordpress will piggy back on user requests to run crons. For better results disable wordpress cron and enable it on your server using crontab if possible. To disable wordpress cron edit wp-config.php to have this code:
    define('DISABLE_WP_CRON', true);

    And create a cron entry on your machine. For Linux here is how it will look:

    * * * * * wget -q -O -

    I’m running cron every minute. You can reduce the frequency of the cron as per your need. I kept it high as I wanted to load a small set of entries every minute (5 entries per minute in above configuration) instead of loading a large number of entries after larger interval.

  4. In case you need some portion of the page dynamic, the plugin has support for it. You can look at this thread for more info. Here is how mclude can be used:
    <!--mclude file_to_include.php-->

    This can be pretty handy if you need to execute some dynamic code e.g. displaying current time or something based client IP adress, etc. Also note that file_to_include.php should be independent code and should not depend on some plugin code etc. Otherwise page loaded from cache may appear blank.

  5. In case you need to purge cache for a specific post (or page), edit and manage posts screens have a link to purge cache for a specific post. Here is how it looks on edit post screen:
  6. To clear all the cache at once you can do it on cache page screen (“Performance” => “Page Cache”)

Notes on other features of the plugin

  1. Minify. This reduces the html, css and javascript size and optionally combines them also. Usually the benefit of this is not as high as it appears.
    1. The compression may look high but the data anyway is transferred on wire in compressed form. So the saving in compressed data on wire is not so much.
    2. Combining css and javascript also does not let browsers cache specific js and css files. But for new visitor’s “first hit” combining has huge benefit as it reduces round trips. I had some jQuery issue after combining. So you may want to double check if your site works fine after combining js, css, etc.
  2. Database and Object cache. I believe these can also be implemented. But after page cache, the benefit of these won’t be too much.
  3. Browser cache. This can also be done using apache settings. This does not take much time to implement and benefits are huge. Here is an example configuration which can be used as a starting point.
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/gif "access plus 24 hours"
    ExpiresByType image/png "access plus 7 days"
    ExpiresByType text/css "access plus 24 hours"
    ExpiresByType application/x-javascript "access plus 24 hours"
    ExpiresByType application/javascript "access plus 24 hours"
  4. CDN. It can be used if you have lot of static content like images etc. These will load much faster at user’s end if CDN is used.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments