News Archive
PhpRiot Newsletter
Your Email Address:

More information

Why should you cache your PHP website?

Note: This article was originally published at Planet PHP on 15 February 2011.
Planet PHP

Most web servers are able to handle aonormala traffic and there are plenty of websites which doesn't have so much traffic. So maybe you ask yourself: Why should you cache your PHP powered website? The apache web server is able to serve many, many files at the same time, but all these files need to be static. A PHP script is parsed by the web server and next the generated html data is send to the client (web browser). While this happens the server need to use much more memory than by sending a file to a web client. Imagine what happens if you run/parse a page which is build with WordPressa

The web has not only human visitors!

If your WordPress or PHP site has a few visitors within an hour a web server should be able to serve ALL pages to your visitors without any problems. So far so good, but what if your site get accessed by some bot? The most bad scenario is that these aounnaturala access can slow/take down your site and also all other sites hosted on the same server!

WordPress Super Cache, a required WP plugin

Even if your blog doesn't have a lot of blog post or comments, you should install the WordPress Super Cache plugin. This plugin works on most servers and can rescue your blog's life! WordPress needs a lot of database queries to show a single page to your visitors. Each database connection needs some memory and will use some CPU. Using this cache plugin a aonormala viewed page doesn't use the database anymore and your server can handle much more traffic.

Cache functions for custom PHP websites

There are many ways to cache your website, there are several cache modules available or it's possible to create a cache version of each page using some PHP code. Which the best is for your situation depends on the application and the type of hosting you're using.

The eAccelerator project

If you're able to configure your web server (you need root access) you should try the eAccelerator project. It works as kind of PHP extension and is able to create a cache version of your PHP scripts. I installed and updated eAccelerator on two web servers now and I like it how it works. Before you start you should check the requirements and maybe you like to read my notes about the eAccelerator installation.

Custom PHP file caching (tutorial)

If you're looking for a way to cache single pages from your website you should try this tutorial. The simple code snippet is able to aodownloada the html code from the selected page and stores the html code as a static page. The following code will check, read/write and output the cache version (check the comments inside the code).

// a function to receive an write some data into a file function get_and_write($url, $cache_file) { $string = file_get_contents($url); $f = fopen($cache_file, 'w'); fwrite ($f, $string, strlen($string)); fclose($f);

Truncated by Planet PHP, read more at the original (another 10920 bytes)