WP-Cache 1.0 released
Important: vesion 1.5.3 and 1.5.4 fixed a couple of showstoppers. Upgrade it please, you will get also flock() if semaphores does not work in your server.
Version 1.5 was released. Easier installation and setup for Wordpress 1.5 and higher.
Versión en castellano y otra. Thread in Wordpress forum.
WP-Cache is extremely efficient WordPress hack –not a simple plugin– for page caching. It allows to serve hundred of times more pages per second, and to reduce the response time from several tenths of seconds to less than a millisecond.
The rant
Every WordPress user or administrator know that WP sucks badly in the efficiency department. The response times for generating a simple page can take up to a second in a Pentium 4 server. Staticize and Staticize-reloaded plugins tried to solve the problem by caching the output of the page and avoiding to access the database.
But that approach didn’t work well. Staticize bypasses the request to the database, but this was not the real problem. MySQL requests are fast enough and they add very little time. The real problem is that WP is already a beast that sucks up to the last bogomip compiling thousands lines of PHP code. And, you know dear WP expert, plugins’ code are the last to be included and compiled for every single access. So you can cache whatever you want, but the time it takes to PHP to reach you caching code is already too long and makes a big part of the freaking WP response time.
The obvious solution is to cache the content of a page (which is identified by an URL), if the cache is available, send it at the beginning of WP code, as soon is possible to avoid compiling the whole beast. That was my original idea, so I started with Staticize-reloaded and split it in two parts. The last forbade me from developing it as a simple plugin because plugins are called at the end of WP processing.
The final result is this efficient caching mechanism that avoid both, database requests, and –more important– compiling thousands lines of useless code.
The features
Given enough bandwidth, forget worrying about the Slashdot effect.
It deliver typically up to a 1000 times response time gain, from several tenths of second to ¡less than a millisecond!.
It serves more than 80 times pages per second.
Support more than 100 times concurrent clients.
It caches all headers generated by WP and sends them again for every connection.
As Staticize-reloaded, it allows to specify part of codes that must be executed every time. This features does not impact negatively to pages that have no “cacheable” code.
Requirements
The only “special” requirement besides those already required by Wordpress is that PHP must be compiled and/or loads the sysvsem module. This modules provide semaphores that are needed to ensure consistency of the cached data.
Since version 1.5.2 it uses semaphores if they are available, otherwise it uses flock().
Downloading
The current working WP-Cache source code is just this.
Installing
Deactivate gzip compression (I’ll promise it for future versions) and also deactivate Staticize if you are running it.
Copy the file you just downloaded to your WP include directory, for example
wp-include/wp-cache.php.Create the cache directory under
wp-contents, i.e.wp-content/cache. Make sure the directory is writeable by the Apache server (the same as in Staticize).Edit wp-settings.php, find the line
require_once (ABSPATH . WPINC . '/wp-db.php');and add the following line right beforerequire_once (ABSPATH . WPINC . '/wp-cache.php');So you will get:require_once (ABSPATH . WPINC . '/wp-cache.php'); require_once (ABSPATH . WPINC . '/wp-db.php');Right before plugins areif ( get_settings(’active_plugins’) ) { called, insert the following line
CachePhase2();. For WP 1.2 is right beforeif (!strstr($_SERVER['PHP_SELF'], 'wp-admin/plugins.php'.... For WP 1.3 and higher is a line beforeif ( get_settings('active_plugins') ) {. So you will get something like:CachePhase2(); if ( get_settings('active_plugins') ) {Et voilà. Now access to your blog and check in the cache directory, you must see files as:
wp-cache-6f6ffbe015d377ebb12bbf6f870d71b5.html wp-cache-6f6ffbe015d377ebb12bbf6f870d71b5.meta wp-cache-fddbd3ebfe8e93fe58c07021e885ad88.html wp-cache-fddbd3ebfe8e93fe58c07021e885ad88.meta
Tuning
You can change the value of few variables to change the behaviour of WP-Cache.
- $cache_enabled: enable or disables wp-cache.
- $cache_max_time: cache max time in seconds.
- $cache_path: the path for cache files.
- $file_prefix: name prefix of cache files.
- $known_headers: which headers are cached.
- $acceptableFiles: which files are allowed to be cached.
Performance
The following tables are the result of Apache Benchmark from another computer in the same LAN. The server is a Compaq Server with a P3 1.4 Ghz, 512 MB (and running lots of services).
The command for both cases was:
/usr/sbin/ab -t 5 -c 5 http://mnm.uib.es/gallir/posts/2005/02/02/110/
Cache disabled
| Document Length: | 15482 bytes | ||
|---|---|---|---|
| Concurrency Level: | 5 | ||
| Time taken for tests: | 5.126 seconds | ||
| Complete requests: | 7 | ||
| Failed requests: | 0 | ||
| Total transferred: | 114490 bytes | ||
| HTML transferred: | 110810 bytes | ||
| Requests per second: | 1.37 | ||
| Transfer rate: | 22.34 kb/s received | ||
| Connnection Times (ms) | |||
| min | avg | max | |
| Connect: | 0 | 428 | 3000 |
| Processing: | 520 | 1553 | 1141 |
| Total: | 520 | 1981 | 4141 |
Cache enabled
| Document Length: | 15581 bytes | ||
|---|---|---|---|
| Concurrency Level: | 5 | ||
| Time taken for tests: | 5.006 seconds | ||
| Complete requests: | 424 | ||
| Failed requests: | 0 | ||
| Total transferred: | 6805616 bytes | ||
| HTML transferred: | 6622862 bytes | ||
| Requests per second: | 84.70 | ||
| Transfer rate: | 1359.49 kb/s received | ||
| Connnection Times (ms) | |||
| min | avg | max | |
| Connect: | 0 | 0 | 0 |
| Processing: | 11 | 46 | 109 |
| Total: | 11 | 46 | 109 |
Final words
Now write something interesting, geeky and weird in you blog. Send the link to Slashdot. Enjoy the storm.
I just tried this out on my local (OS X) install WP1.5 -31/01/05 and get the message
call to undefined function sem_get() line 106 wp-cache.phpComment by Ian — Wednesday 2/2/2005 @ 23:40
Probably you compalied PHP and didn’t include semaphore libraries (which is normally enabled). The option is: “–enable-sysvsem”
Or perhaps you have the module but is not loaded. Check it out with phpinfo(). The name of the module es sysvsem.
Comment by ricardo galli — Wednesday 2/2/2005 @ 23:50
Thanks, I don’t have that module. I think adding it is beyond my ability so will try it out on my webhost’s server sometime.
Comment by Ian — Thursday 3/2/2005 @ 0:10
I installed it and it seems to be working, apart for one thing: I have dynamic content on my pages, which I put between html comments like for staticize (I had staticize before) — but they seem static. See my home page, below the search box: there is a ‘random post’ link.
Reload. It doesn’t change.
Any ideas?
Comment by Steph aka bunnywabbit_ — Sunday 6/2/2005 @ 14:35
It seems to me that you are calling a function instead of including an external php file.
Comment by ricardo galli — Sunday 6/2/2005 @ 14:38
yes, that’s how it worked in staticize. You put the tags around the function that you wanted to remain dynamic. (Sometimes the function in question was include(), but not necessarily).
So for it to work, I need to put all my dynamic content in a bunch of separate php includes?
Comment by Steph aka bunnywabbit_ — Sunday 6/2/2005 @ 14:53
Acording to the documentation and the relevant code:
$store = preg_replace(’|(.*?)|s’, ‘< ?php include("' . ABSPATH . '$1"); ?>‘, $buffer);
wp-cache and staticize reloaded do exactly the same thing: to include a file.
Are you sure you din’t patch your plugin? If so, the same modification should work in wp-cache.
Comment by ricardo galli — Sunday 6/2/2005 @ 15:07
Sure I didn’t. Weird. So maybe it wasn’t working and I didn’t realize. I’m off to create a whole bunch of little files, then
Thanks!
Comment by Steph aka bunnywabbit_ — Sunday 6/2/2005 @ 15:18
oh! figured it out.
$buffer = preg_replace(’|(.*?)|is’, ‘< ?php $1 ;?>‘, $buffer);
I’ve been using mfunc, not mclude
so your cache doesn’t use mfunc, right?
Comment by Steph aka bunnywabbit_ — Sunday 6/2/2005 @ 15:24
Hola! estuve probando tu hack/plugin… y bueno, usando el ab de apache me muestra resultados increibles, muy buenos. Pero cuando vuelvo a una página ya visitada, se carga un código totalmente ilegible (miralo en http://ale.servepics.com/displayimage.php?pos=-4744 ). No importa la página, siempre se carga así. He visto los permisos de los archivos y los permisos son 644 (u=rw-, g=r–, o=—), le cambié los permisos a 755 e inclusive a 777, pero parece ser que no funciona.
Quizá no estoy siguiendo el paso 5 muy bien ya que no lo entiendo totalmente (mi inglés es bueno, pero cuando es cuestión de códigos, ahi está el problema). Mira el archivo aca:
http://weblog.homelinux.org/wp-settings.phpsGracias y saludos
Comment by Alejandro — Thursday 10/2/2005 @ 3:17
Ostras, soy un idiota, lo siento, tengo ya la respuesta. Me faltaba lo de el gzip y ordenar bien el CachePhase2();
Lo siento
Alejandro
Comment by Alejandro — Thursday 10/2/2005 @ 3:33
[…] æ³æ¯ï¼ææ¾æ£äºStaticize Reloadedï¼æ¹ç¨éåæåRicardo Galli鿰坫åºä¾çWP-Cache 1.0 ã WP-Cache æ¯è¼æ²æ Staticize Reloaded å¨ include é é¢æè·¯å¾æåºçæ³ […]
Pingback by :::zonbleâs promptbook » åç´ WordPress 1.5::: — Sunday 20/2/2005 @ 15:05
Hey, WordPress 1.5 includes the hooks you need to make this plugin happen without any core modification of any files except wp-config.php.
Comment by Matt — Monday 21/2/2005 @ 9:51
Last time I checked (few weeks ago) and according to some comments in WP forum, it seems not possible. Nevertheless, I’ll check it again.
Thanks.
Comment by gallir — Monday 21/2/2005 @ 10:10
Oooh, please let me know if you do find a solution. This would be a killer plugin if no modifs to core files need to be made.
Comment by Steph aka bunnywabbit_ — Sunday 27/2/2005 @ 19:40
I already checked it out. There is no hook to allow wp-cache to run so early (and due to this, very fast if the url is cached).
Comment by gallir — Sunday 27/2/2005 @ 19:54
[…] I’ve used at work for years (but I’m using mod_perl/Embperl instead of PHP): Ricardo Galli, de software libre : WP-Cache 1.0 released When I have a free moment, I’ll pa […]
Pingback by www.eKris.org » Blog Archive » WP-Cache — Monday 7/3/2005 @ 22:29
Hello,
With very last SVN, meta files doesn’t seem to be created and as I result I get errors like :
Warning: file_get_contents(/home/account/www/foo/wp-content/cache/wp-cache-0cb848d15df4ed348ad0d787ca598ec5.meta): failed to open stream: No such file or directory in /home/account/www/foo/wp-includes/wp-cache.php on line 85
Warning: Invalid argument supplied for foreach() in /home/account/www/foo/wp-includes/wp-cache.php on line 86
Warning: Cannot modify header information - headers already sent by (output started at /home/account/www/foo/wp-includes/wp-cache.php:85) in /home/account/www/foo/wp-includes/wp-cache.php on line 96
Comment by Luc — Friday 11/3/2005 @ 23:58
The hook Matt suggest (comment #13) is in wp-settings line 34
// For an advanced caching plugin to use, static because you would only want one
if ( defined(’WP_CACHE’) )
require (ABSPATH . ‘wp-content/advanced-cache.php’);
Comment by Luc — Saturday 12/3/2005 @ 0:26
Nice! Didn’t see, sorry. I’ll release another version tomorrow to make it work with this hook
Comment by gallir — Saturday 12/3/2005 @ 0:36
WP-Cache 1.5 Released
This new version is optimized for WordPress 1.5 and higher….
Trackback by Ricardo Galli, de software libre — Saturday 12/3/2005 @ 17:29
[…] plugin März 23rd, 2005 […]
Pingback by B:log.isch » noch’n plugin — Wednesday 23/3/2005 @ 17:03
[…] å示éä¸çå°ç 使ç¨å¾ççµæççæå å¿«çæè¦º ä¸ééæ²è©¦ç¨é WP-cache ææ©æ æåä¾ä½¿ç¨çç 以䏿·åé¨ä»½å®è£å §å®¹ è©²æªæ¡å¯å¨ Joe Horn […]
Pingback by ãæ£®ç鍿è¨äº » å å¿« WordPress 1.5 çé é¢é¡¯ç¤º — Tuesday 29/3/2005 @ 18:01
Forgive my basic question. I’m using WP1.5 I get error: “call to undefined function sem_get() line 106 wp-cache.php”. My host’s PHP is compiled without “–enable-sysvsem”. Is there a way to make your plugin work on such a system?
Comment by stever — Sunday 3/4/2005 @ 4:25
Sorry. I see one must read and follow instructions. This is addressed in 1.5.x
Comment by stever — Sunday 3/4/2005 @ 5:36
[…] Editor
Wordpress Plugins (looking into…)
WP-Cache Ricardo Galli, de software libre » WP-Cache 1.0 released Every WordPress user or administrator kn […]
Pingback by Expoblog » Blog Archive » Wordpress Plugins (looking into…) — Sunday 24/4/2005 @ 12:09