A Simple PHP Cache Tutorial

Most web servers are able to deal with “normal” traffic and there are plenty of websites which doesn’t have a lot traffic. So maybe you ask yourself: Why should you cache your PHP powered website? The Apache web server is able to serve many, many data files at the same time, but all these files need to be static.

A PHP script is parsed by the web server and then the produced HTML data is send to the client (web browser). While this happens the server need to use a lot more memory space than by sending a document to an internet client. The net has not only human guests! If your WordPress or PHP site has a few site visitors within an hour an online server should be able to serve ALL pages to these potential customers with no problems. So far so good, but imagine if your site get reached by some bot?

  1. Log in to system
  2. Use Emotional Titles
  3. Why are you calling us today
  4. Create Contrast

The worst scenario is these “unnatural” gain access to can decrease/take down your site and all other sites hosted on a single server! Even if your WordPress blog doesn’t have a great deal of post or feedback, you should set up the WP Super-Cache plugin. This plugin works on most servers and can rescue your blog’s life! WordPress requires a great deal of database questions showing a single web page to your visitors. Some memory space is needed by Each data source connection and will use some CPU.

Using this cache plugin a “normal” viewed page doesn’t use the database anymore and your server can handle much more traffic. There are plenty of ways to cache your PHP website, there are several cache modules available or it’s possible to make a cache version of every page using some PHP code. Which solution the best is perfect for your situation, depends on the application and the kind of hosting you’re using. If you’re in a position to configure your web server (you will need root to gain access to) you should try the eAccelerator project.

It works as a kind of PHP expansion and can build a cache version of your PHP scripts. I installed and updated eAccelerator on two web machines and I like it how it works now. Before you begin you should check certain requirements and maybe you want to read my notes about the eAccelerator installation. If you’re searching for a way to cache single pages from your website you should try this tutorial. The easy code snippet is able to “download” the Html page from the selected page and stores the Html page as a static page.

The following code will check, read/write, and output the cache version (check the remarks inside the code). The code above is simple however, not dynamic pretty. To understand this example working, you will need to create a list for the cache files and for the URL’s. Inside our case we use a part from the URL and pass this string as a variable using the query-string. The rewrite engine will complete anything after “page/” to the web page.php file as a query-string.