W3TC Cache Preload for Version 0.9.2.5

Back in March 2012 I posted a set of feature enhancements and repairs to cache priming for the WordPress W3TC Total Cache plugin version 0.9.2.4. See my W3TC Cache Preload or Cache Prime posting.

A W3TC security fix was recently released. Unfortunately, the fix introduced a fault with basic mode disk caching. A cached page is now being prefixed with a nine byte ‘<?php /*’ text string. This text is inserted before the page expiry time and compressed page data is written. What this means is that the page expiry time is never picked up successfully when the cache page is referenced. The page is always thought to be expired and cached disk pages are never returned. There is also a problem with decoding of the returned data.

w3-total-cache/lib/W3/Cache/File.php

/**
  * Sets data
  *
  * @param string $key
  * @param mixed $var
  * @param integer $expire
  * @return boolean
  */
 function set($key, &$var, $expire = 0) {
.
.
.
    if ($fp) {
      if ($this->_locking) {
         @flock($fp, LOCK_EX);
      }
//    @fputs($fp, '<?php /* ');   // wrong place ...
      @fputs($fp, pack('L', $expire));
      @fputs($fp, '<?php /* ');   // it needs to go here ...
      @fputs($fp, @serialize($var));
      @fclose($fp);
.
.
.
 /**
  * Returns data
  *
  * @param string $key
  * @return mixed
  */
 function get($key) {
.
.
.
    if ($ftime > time() - $expire) {
      $data = '';
.
      while (!@feof($fp)) {
        $data .= @fread($fp, 4096);
      }
.                            
//    $var = substr($data, 9);   // and this is wrong ...
      $data = substr($data, 9);  // it needs to be this ...
      $var = @unserialize($data);
   }
.
.
.

 

A corrected version of W3TC 0.9.2.5 that also integrates my previous cache prime feature enhancements can be downloaded here.

Updated Elements

  • w3-total-cache/lib/W3/Config.php
  • w3-total-cache/lib/W3/PgCache.php
  • w3-total-cache/lib/W3/Plugin/PgCacheAdmin.php
  • w3-total-cache/lib/W3/Plugin/TotalCacheAdmin.php
  • w3-total-cache/lib/W3/Plugin/PgCache.php
  • w3-total-cache/lib/W3/Cache/Base.php
  • w3-total-cache/lib/W3/Cache/File.php
  • w3-total-cache/lib/W3/Cache/File/Generic.php
  • w3-total-cache/inc/functions/http.php
  • w3-total-cache/inc/options/pgcache.php

 

Update, January 6, 2013

Apparently, folks are actually using this feature?  This has prompted me to complete some outstanding items from my cache prime feature enhancement last March, 2012.

  1. The cache prime trace name is now configurable.  The trace resource is publicly accessible from your site URL.  You need a way to turn this on or off.
  2. Cache priming will now generate a new cached page before a previously cached page expires.  Previously, pages were not refreshed until after they had expired.  This change can reduce the probability that an uncached page reference occurs. Pages may now be generated within two update intervals before expected expiry.

The download file referenced above now contains these changes.  The recommended steps to install this feature enhancement on a working W3TC 0.9.2.4 or W3TC 0.9.2.5 implementation are as follows:

  1. Turn off cache priming.  In the Page Cache configuration page clear the checkbox to automatically prime the page cache.  Save all settings.
  2. Empty all cache.
  3. Deactivate the W3TC 0.9.2.4 plugin in WordPress.
  4. Update the plugin code.  Extract the zip file.  Replace the w3-total-cache plugin folder in WordPress with the w3-total-cache folder from the zip file.
  5. Activate the W3TC 0.9.2.5 plugin in WordPress.
  6. Empty all cache again.
  7. Review the Page Cache configuration.  Activate cache priming if desired.

w3tc_prime2

The default TTL (Time to Live) of page cache files is set via the “Expires header lifetime” field in the “HTML” section on Browser Cache Settings tab.  This is 3600 seconds, or one hour.

Specific page expiry times can be set in the Advanced section of the Page Cache configuration. W3TC limits maximum disk cache expiry time to 30 days, or 2592000 seconds.

The example below specifies that any page URL ending with “html” is to be cached for 30 days; any page URL that contain any two digit number between slashes is to be cached for 7 days; and any page URL that begins with “/v/” or “/tag/” or “/category/” is to be cached for 24 hours.  The first identified match in the listed order is used.  If no match is found then the default TTL is set.

w3tc_prime3

2 thoughts on “W3TC Cache Preload for Version 0.9.2.5

  1. your adjustments W3TC rocks :-) thanks, it saves more than 2 seconds to load and score now 95/100.
    Strange that Frederick himself has not changed in its own plugin

  2. Pingback: How to Speed up Suffusion and WordPress - Mbrsolution

Comments are closed.