Antiguo y abandonado blog de Ricardo Galli :-(

Wednesday 2/2/2005

WP-Cache 1.0 released

Filed under: Mis hacks, soft libre — gallir @ 21:44

Version 2.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

  1. Deactivate gzip compression (I’ll promise it for future versions) and also deactivate Staticize if you are running it.

  2. Copy the file you just downloaded to your WP include directory, for example wp-include/wp-cache.php.

  3. 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).

  4. Edit wp-settings.php, find the line require_once (ABSPATH . WPINC . '/wp-db.php'); and add the following line right before require_once (ABSPATH . WPINC . '/wp-cache.php'); So you will get:

    require_once (ABSPATH . WPINC . '/wp-cache.php');
    require_once (ABSPATH . WPINC . '/wp-db.php');
    
  5. Right before plugins areif ( get_settings(’active_plugins’) ) { called, insert the following line CachePhase2();. For WP 1.2 is right before if (!strstr($_SERVER['PHP_SELF'], 'wp-admin/plugins.php'.... For WP 1.3 and higher is a line before if ( get_settings('active_plugins') ) {. So you will get something like:

    CachePhase2();
    if ( get_settings('active_plugins') ) {
    
  6. 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.

26 Comments

  1. 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.php

    Comment by Ian — Wednesday 2/2/2005 @ 23:40

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

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

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

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

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

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

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

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

  10. 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.phps
    Gracias y saludos

    Comment by Alejandro — Thursday 10/2/2005 @ 3:17

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

  12. […] 法是,我放棄了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

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

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

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

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

  17. […] 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

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

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

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

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

  22. […] plugin März 23rd, 2005 […]

    Pingback by B:log.isch » noch’n plugin — Wednesday 23/3/2005 @ 17:03

  23. […] „啟示錄中看到的 使用後的結果真的有加快的感覺 不過還沒試用過 WP-cache 有機會 我再來使用看看 以下擷取部份安裝內容 該檔案可在 Joe Horn […]

    Pingback by ㄚ森的隨手記事 » 加快 WordPress 1.5 的頁面顯示 — Tuesday 29/3/2005 @ 18:01

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

  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

  26. […] 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

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress