Introduction to the Webcache

go back to this Tutorial

Why do I need this?

Right now you might be asking yourself why you even bother reading this. You may not even have trouble with the speed of your website, but it is still a good Idea to think about this cache, especially if you are with a shared hosting provider.
The webcache will help you to significantly reduce the load on the MySQL server AND the webserver, by avoiding costly PHP code execution and MySQL queries. The principle is quite simple: For every node of your CMS a html file will be generated, based on some rules you can set up (I will get into this in the implementation section). If the webserver finds a html file, it will serve this file instead of interpreting php and sending requests to the MySQL servers. This has a few advantages:
  • Your website remains active, even if the MySQL server will go down for some reason.
  • Your website will speed up (especially when the MySQL queries or PHP execution are slow for some reason, as it can easily happen on a shared host)
  • You can serve much more traffic, without generating a large CPU load on the webserver (PHP) or load on the Database Server. Shared Hosts sometimes ban sites, if they generate too much load. This can easily happen if you have a popular website, or get linked on a popular website. Even if you run your own servers, at a certain amount of visitors, every sever will have trouble keeping up and gets slow.
  • You will learn something (hopefully).

Credits

This cache is based on the implementation by Ben Slade that can be found here. While Bens ingenious cache works very well on news-like websites, where the latest news are always the most requested items, it does not work very well for me. Bens cache only keeps a copy of the last 20 nodes that have been created. For me, I have almost 2000 nodes. Each of them sees about the same amount of visitors on average. While I can not create 2000 nodes every 20 minutes, or I would stress my MySQL server too much (exactly what we want to avoid), I have change the code quite a bit. Here is the highlights of my changes:
  • I have arranged the comments on my website so, that each individual picture does not allow comments, but galleries do. With this arrangement, I have greatly reduced the amount of nodes that allow commenting.
  • I have split the cache in two parts. One part runs every 20 minutes and updates every page with comments enabled + the front page. The second part updates all nodes that do not have comments enabled. This part has to be run only after major updates to this site (when the navigation menus change). This second part waits longer between mysql queries and takes about 5 hours to complete.
  • I have added support for url_alias and clean urls, to acommodate my URL structure (I don't like query strings in the url).
  • I have added a sanity check. If the MySQL connection is lost during the script run, html files will have no content. The script now checks the file size before updating the cache.
  • The .htaccess code has been changed for clean url support and sub-directory support
I would also like to thank the "Extras" (forum nickname) for his support with .htaccess and php code.

Disclaimer and Other things

Even though the cache works well for me (so far), I do not guarantee that it will work for you. Make backups of your site and your database before messing around with it. Disable all functionality and use the built in log function to look at the unix commands and mysql queries generated before actually enabling the code. I provide this information at no benefit to me and I do not assume any liability for lost data, ruined customer reputation or whatever. You are on your own. That does not mean, that I will completely ignore questions.
If you improve the cache, please send me a copy of the improved cache. I will credit you for it on this website.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options