Pages: [1]   Go Down
Send this topic | Print
Author Topic: [SCRIPT] Optimize WordPress Using a Predictive Database Object Cache  (Read 734 times)
inp o҉rtb
The Gangsta
Global Moderator
Official 110mb Guru
*****
Offline Offline

Posts: 15645


experimental theologian


WWW
« on: November 23, 2008, 12:56:13 PM »

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.

Indications

Caching'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.

Installation

The 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.phps

Before 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.

Configuration

There 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_r


Discussion

This 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 wink
Logged

Hi! I’m a signature virus! Add me to your signature to help me spread.
spam me: ispamspot@gmail.com

blog | my work @ deviantART | Imagine-ng image editor
iM1
Authority Member
****
Offline Offline

Posts: 890


Why drink and drive when you can smoke and fly...


WWW
« Reply #1 on: November 24, 2008, 07:55:45 AM »

I love it! Thanks  cheesy
Logged

My site is worth
My Sites:
iM1 MUSIC // DesiDrop - Music Blogs
Satize.com - MP3 Search Engine NEW V2!
inp o҉rtb
The Gangsta
Global Moderator
Official 110mb Guru
*****
Offline Offline

Posts: 15645


experimental theologian


WWW
« Reply #2 on: November 24, 2008, 08:21:47 AM »

You're welcome. If you notice anything weird or have something to suggest, just post back here.
To uninstall the script, just remove/rename the file (and remove the tables if necessary).
Logged

Hi! I’m a signature virus! Add me to your signature to help me spread.
spam me: ispamspot@gmail.com

blog | my work @ deviantART | Imagine-ng image editor
iM1
Authority Member
****
Offline Offline

Posts: 890


Why drink and drive when you can smoke and fly...


WWW
« Reply #3 on: November 24, 2008, 08:23:49 AM »

No doubt! If theres any info you need me to report back let me know.
Logged

My site is worth
My Sites:
iM1 MUSIC // DesiDrop - Music Blogs
Satize.com - MP3 Search Engine NEW V2!
Pages: [1]   Go Up
Send this topic | Print
Jump to: