Speed up your Website with this Simple Page Cache Script for PHP
For dynamic websites, database queries take up a significant amount of the CPU resources of your servers. For medium to high traffic websites in shared or virtual hosting services, this could mean significant loss of traffic due to too much database queries or over use of shared CPU time. As such, it is quite useful to have some sort of cache system for your dynamic pages, particularly those with content that don’t change too much or too often anyway. Below is a very simple and generic cache script for PHP web pages which you can copy-paste and use almost instantly in your website. What it does is store an instance of the dynamically generated web page as an HTML file and it then counter checks with a predefined time period that you set whether to display the cached instance or to dynamically generate the page again. The longer the time period between dynamic generation, the greater the savings in terms of CPU usage and database queries. 1. First create a folder in your hosting account where you’ll be housing the cached data. In this example the cache folder is located at http://www.yourdomain.com/cache 2. Next, copy-paste the script below at the start of the PHP page you want cached. We are assuming here that the dynamically generated page is utilizing the GET variable “id”, such as: http://www.yourdomain.com/index.php?id=XXXX: 3. At the bottom of your PHP page, copy-paste the code below: 4. You’re done! Upload and update your PHP file.