WordPress 2.5 introduced some pretty radical changes to its object caching model. Most notably, the caching API was greatly improved and the built-in file caching mechanism was removed. The reason file caching was removed is that offloading work to the disk results in poor performance. The obvious alternative is to cache to RAM using Memcached and XCache, but these are not available in most shared hosting environments. So here's a cheap compromise: cache to MySQL. This might sound stupid because a main point of caching is to reduce database load. But this method does reduce database load by consolidating queries (fewer and more efficient queries). At the same time, MySQL offers the benefit of partial RAM caching and a highly optimized C engine.
IndicationsCaching's a good idea if you have more than a handful of visitors at any moment. My blog used to make 15 queries per pageload, but the caching engine reduced that number to 5.
InstallationThe standard method of installing a WP object cache is to put it in
/wp-content/object-cache.php. WP would automatically recognize and load it. You can have only one object cache active for obvious reasons. Here's the code:
http://inportb.com/downloads/object-cache-predictive.phpsBefore you install the file, you should create two MySQL tables:
DROP TABLE IF EXISTS `wp_objc_s`;
CREATE TABLE `wp_objc_s` (
`group` varchar(255) NOT NULL,
`id` varchar(255) NOT NULL,
`data` text CHARACTER SET utf8 NOT NULL,
`expire` datetime NOT NULL,
KEY (`expire`),
UNIQUE (`group`,`id`)
) ENGINE=MyISAM;
DROP TABLE IF EXISTS `wp_objc_r`;
CREATE TABLE `wp_objc_r` (
`handle` text NOT NULL,
`recall` text NOT NULL,
UNIQUE (`handle`(1000))
) ENGINE=MyISAM;The red part is your WP
prefix. It's easier if you just use these names, but if you want you can configure the script to use other table names.
And after that... you're done! Watch as your resource usage takes a dive.
ConfigurationThere are a few
optional configuration switches that go within wp-config.php:
define('CACHE_EXPIRATION_TIME',900); // cache interval in seconds, default 15 minutes
define('CACHE_TABLE_STORE','nameoftable'); // cache data table, default prefixobjc_s
define('CACHE_TABLE_RECALL','nameoftable'); // cache prediction table, default prefixobjc_rDiscussionThis object cache has three portions: the memory, the database, and the prediction engine. Objects are cached in the database for persistent storage and loaded into memory when needed. The prediction engine knows which objects need to be loaded for each page, and is thus able to consolidate many small queries into a single efficient query. If the prediction engine misses a needed resource, the resource is loaded on-demand; if the prediction engine loads an unnecessary resource, it would not be used. While these two situations are non-optimal, the prediction engine learns to do it right the next time.
So the first pageload would show no performance improvement (but also no degradation). After that, the prediction engine kicks in and maximum performance is achieved by the third pageload or so.
Enjoy
