Server-Side Caching for AJAX
Database-enabled AJAX opens the door to an interaction model like no other, providing users with dynamic database data without a browser refresh. In all of the excitement of learning about database connectivity with AJAX, it’s easy to overlook the fact that the server is probably becoming inundated with more requests than usual. This is true especially when implementing features that suggest terms based on each letter entered into an input, such as Google Suggest, because each and every key press is sending a request to the server and the database. This setup could be extremely overwhelming if the site has a lot of traffic—possibly even bringing your server to its knees.
One solution to the issue is caching requests for specific amounts of time. In this article, I’ll show you how to implement a PHP object that caches requests to the server for a specified amount of time. Each time a user comes to the site and makes a request, the server-side object will check for a cached version, decipher what time it was created, and either return it (if the specified time limit hasn’t elapsed) or update it and return fresh data from the database. The final sample project is an application that allows users to add, edit, and delete messages. You can download the source code for the project here. The source code is necessary to complete the project because I won’t cover every line of code in this article; rather, I’ll give you a high-level overview of the project and how caching relates to it. Let’s get started by taking a look at the client side.