WP-Cache + gzip without excessive CPU overhead, yay!

In my last post, I talked about the problems of getting WP-Cache to work with gzip compression. In short, most of the solutions suggested on the web require gzip compression of cached pages each time they are requested. It turns out it is not so difficult to hack a gzip feature into WP-Cache after all. Read on to see how you can make the latest WP-Cache gzip compress cached pages only once per page no matter how many times they are served. A modified version of the latest WP-Cache v2.1.2 that supports this efficient gzip feature is also available for download. :)

The information and source code in this post is only relevant if you have some specific reason or desire to continue using WP-Cache instead of the new WP Super Cache. Read more about Super Cache here. WP Super Cache now incorporates all the features described here.

Recap of problems with common solutions

The most common suggestions (at least you see these tips on Digg all the time :( ) say to use the transparent and on-the-fly gzip compression feature of Apache or PHP. The problem is that pages are compressed each time they are requested, increasing your server’s CPU load and defeating the purpose of WP-Cache, which is to serve pages repeatedly with minimal CPU usage. The hack of inserting

ob_start(ob_gzhandler)

at the top of wp-cache-phase1.php is equally problematic: again, cached pages are compressed each time they are requested. Thus, these workarounds actually hurt your site’s performance; you are better off going without gzip entirely than using these hacks.

Solution: a WP-Cache gzip feature that is efficient

The solution, first created by Nick Georgakis, is to manually handle the gzip process. WP-Cache will create and store on disk a gzipped copy of a page if the page is requested by a browser that accepts gzip content. It will also create and store on disk a uncompressed copy of a page if the page is requested by a browser that cannot accept gzip content. This way, pages are only compressed once, with all subsequent visitors getting served the same cached and gzipped page stored on disk.

Practically all credit goes to Nick Georgakis, who first came up with the idea and code, and Reiner Saddey, who cleaned up and improved on some of the code. My only contribution is merging the changes with the latest WP-Cache v2.1.2 and creating a user-friendly interface in the WP-Cache’s admin panel to enable and disable this gzip feature. Here are the diff files showing the changes between this version and the vanilla version.

WP-Cache v2.1.2 modified with gzip feature

Download:
WP-Cache v2.1.2 gzip Enabled

Features:
- Optional efficient gzip feature that compresses cached pages only once per page (disabled by default)
- User-friendly admin interface to enable/disable the gzip feature
- Bug fix to prevent WP-Cache from changing feed content to text/html

Installation:
- Install the same way you normally install WP-Cache, or simply overwrite existing WP-Cache v2.1.2 files if already installed.
- Go to the WP-Cache admin panel in Options and click “Enable WP-Cache gzip”.

FAQ: (who am I kidding? I’ve had like 2 visitors :( )
Q: Does WP-Cache gzip work with WordPress v2.3.1?
A: As far as I can tell, yes. This blog is running v2.3.1.

Q: How can I tell if WP-Cache gzip is working?
A: Right-click your webpage and click “View Source.” Scroll to the bottom. If it’s the first time the page is requested, you should see something like this:

<!-- Dynamic Page Served (once) in 0.676 seconds -->

The second time, you should see something like this:

<!-- Dynamic Page Served (once) in 0.676 seconds -->
<!-- Cached page served by WP-Cache -->
<!-- Compression = gzip -->

If you see those messages, then WP-Cache gzip should be working properly. You can also verify with this web tool.

Known Issues / To-Do / Wishlist:
- The first time a page is requested and cached, it is served uncompressed to the visitor. Subsequent requests get the gzipped and cached page. It’d be nice if the very first visitor to a page gets a gzipped version. Will think about this. Suggestions and tips would be greatly appreciated.

Thanks again Nick and Reiner!

If you liked this post, please subscribe to my feed. Thanks for visiting!

Related posts

18 Responses to “WP-Cache + gzip without excessive CPU overhead, yay!”


  1. 1 Nick Georgakis

    I am glad to see someone tooking up the difficult & demanding task to “polish” and keep up to date the modifications of WP-Cache + gzip Me & Reiner have introduced.

    Yes serving the gzipped version to the first visitor would be nice , but it is resource (not bandwidth !) consuming … , by the time you have generated & compressed the page you have it’s source code ready in the output buffers - it’s a pity to ditch them & re-load the compressed ones , however someone else might manage to find another more elegant solution to the problem.

    Unfortunately I do not have the time to check your source code further since I am having exams :-) but will look it in a few weeks!

    The main problem WP-Cache + gzip still has is handling correctly pages with dynamic plugin code. Unfortunately I haven’t been able to find out a working , not AJAX - Javascript based solution.

    Did you managed to make any mods - improvements on this?

  2. 2 Tummblr

    Woo! My very first visitor comment, I’m so psyched! ^_^ I mean, hello Nick.

    Unfortunately, I’m a very noobish coder and haven’t done much more than simple fixes, improvements, and lots of copying and pasting. Don’t even know where to begin with the WP-Cache + dynamic plugins problem. :( Honestly, the only improvements I made to Nick’s and your work is the pretty enable/disable interface in the admin panel and some clearer (to me at least) comments marking the modifications.

    While you’re here, I had a question for you. WP-Cache’s author Ricardo Galli suggests setting “zlib.output_compression = On” in php.ini. That suffers the same constant recompression problem as Apache’s mod_gzip and the ob_start(’ob_gzhandler’) hack, right?

    Have you or Reiner been in touch with Ricardo about implementing these changes in WP-Cache? I’d really hate having to merge code again next time the plugin is updated. ;)

    Thanks for visiting my new site, and good luck with your exams!

  3. 3 tinkertim

    This looks good enough to munch on, I’ve been considering installing a caching system that was a bit friendlier with CPU overhead. I’ve been plugging away at getting WP to behave nicely compiled as a stand alone (dynamically linked) ELF (via Roadsend) that can be run as FastCGI, I was hoping to find caching that was a bit more sensible before I really got into it, since I do have to make some changes to make Roadsend’s libphp happy.

    Found this in the /. firehose, the last place that I’d expect to find hacks for WP :) I voted it up of course. Thanks for the effort, I’ll give it a whirl and let you know. Look for a pingback shortly.

    BTW, I use Mercurial to manage (most of) my plugins and themes, you might want to give it a whirl, it makes keeping these things up to date rather simple (and easy for users to pull frequent changes).

  4. 4 Tummblr

    Thanks for your comments, tinkertim. Your feedback would be greatly appreciated.

    Learned something new today. :) I only have limited experience with CVS and SVN and have never heard of Mercurial. It sounds like a very cool replacement to traditional version control systems. I’m on a shared host though so I can’t install my own server software. Thanks for the tip.

  5. 5 couchmouse

    Have been running WP-Cache v2.1.2 for some time.. will give your modification of it a try out. Thanks.

  6. 6 Daki

    Hi.
    I have installed the wp-cache gzip and enabled it in plugin section, but it wont work.
    I open source page and I dont see the lines you were talking about in your post.
    Could you give me some help, please?

  7. 7 Daki

    I fixed it, but I dont see.

    !– Dynamic Page Served (once) in 0.676 seconds –>

  8. 8 Daki

    I made a mistake in my previous post.

    I wanted to say that I dont see the lines below that you said, it have to be seen after second time.

    – Dynamic Page Served (once) in 0.676 seconds –
    – Cached page served by WP-Cache –
    – Compression = gzip –

  9. 9 Patrick

    Kick ass.

  10. 10 basho

    Installed today, easy to install. Site still up! Thanks for the good work!

  11. 11 Indigo

    Great stuff - installed and worked like a dream! Our t-shirt blog is all nice and fast again!

  12. 12 Blagovest Buyukliev

    Check out the recently released WP-Offload plugin. You will see a dramatical speedup especially if your posts have a lot of static content (images, documents, movies, etc.). It will completely redirect all the requests for static content to external cache servers, so the load on your web server will decrease significantly.

  13. 13 fedmich

    wow, didnt know there is super cache… I wonder will there be super-mega cache after this :p

    Thanks

  1. 1 More sensible caching for WordPress : Echoreply
  2. 2 Wordpress Tuning - WP-Cache und Gzip aktiviert
  3. 3 WP-Cache con gzip en Wordpress | adseok
  4. 4 GZIP Enabled WP-Cache wordpress plugin
  5. 5 Lo hice y lo entendí » Comprimir y cachear las páginas generadas por Wordpress

Leave a Reply