In the beginning, when the Internet was new, it was easy to create fast HTML web pages that could load easily on a dial-up modem. Once a page was requested, the browser would create a cached version of the site on the computer in a temporary directory and later, should that page be requested again, it would simply serve the cached version instead of making a request to the server. This made everything much faster!
Then dynamic pages came onto the scene and messed that idea up. Now we must make requests to our server every time the user wants a page because the page’s content has most likely changed. This means every time the user requests a page, even if it is a page they have been to before, a new request is sent to the server, queries are made and the whole process repeats itself. The downside of this process is the increased load on the server, your database and other resources such as bandwidth.
Here is a real-world example. Let’s say you like to visit a popular sports website that publishes information about various games. Since the data on this page changes frequently it makes sense to put the data in a database and use a PHP script to update the page. When a request is made the following things happen:
Whew! Not only does that take a large amount of time but there are a lot of resources in use as well. Everything may be great if you only get 50 people that come to your site a day, but what about 500?
Enter server-side caching. This idea is based around a simple concept: when the user makes a request to the server, the server generates the requested page and keeps a copy. If the page is requested again by any user, the server knows it has recently generated this page and returns the copy instead of generating it again. This cuts down on the amount of database queries that are running and reduces the overall workload of your server. Great right?
So here is how our request would look with caching on our server:
As you can see, in the new request we are using a lot less time and energy to get the same result. We do not have to make a query to our database and we do not have to call our template file to do any formatting, cutting down a large amount of time spent in server-side processing. This creates a smaller response time for the user’s request and frees up our resources to be used by more visitors. When the page does change, the PHP cache will make a fresh request, re-creating the cached page so the visitor will always get the latest data without the bottleneck of your resources.
Now that you understand how great caching can be, I would caution you about implementing it on every PHP application you have. Caching is not great for dynamic pages that change often like search results, forums or pages that change due to user input. Take caution that you are not serving the user stale data due to a bad caching setup. Otherwise, for appropriate solutions caching can mean the difference between a thriving site and a dead one.
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Jun 05-06||Web Development with PHP/MySQL|
|Aug 07-08||Web Development with PHP/MySQL|
|- Classroom - Online|