W3TC Caching and Multiple WordPress installations

Bernews uses multiple wordpress installations on the same server. For example, bernews.com/flights, bernews.com/weather and bernews.com are all separate installations.

Adding W3 Total Cache to the installations caused confusion because the W3TC cache keys are unique per host, but not per installation. That means that sometime installation A will get an entry cached by installation B! This has completely unpredictable results.

To solve the problem I prepended an MD5 hash of the path to the blog to the cache key that W3TC generates. To do this for the object cache I modified W3_ObjectCache::_get_cache_key() in w3-total-cache/lib/W3/ObjectCache.php by adding this line to the key calculation code, after the sprintf() call:

$key = md5(__FILE__) . '_' . $key;

For the database query cache I made the same modification to W3_Db::_get_cache_key() in w3-total-cache/lib/W3/Db.php.

Bernews uses the “disk (enhanced)” option for page caching, which works fine with multiple installations. If we were using disk, APC or memcached then I think it would be necessary to make a similar change to W3_PgCache::_get_page_key() in w3-total-cache/lib/W3/PgCache.php.


Leave a Reply

Your email address will not be published. Required fields are marked *