HomePHP Caching Result Sets in PHP: Cost-efficient PHP acceleration
Caching Result Sets in PHP: Cost-efficient PHP acceleration
With many websites using a database backend for storing and delivering content, certain common problems arise. One of these is a situation that causes the server to slow down or even bring the system to a complete halt. What can you do to reduce the load on your server? This tutorial discusses one method, a result set caching system, and demonstrates how to implement it with either a procedural or an object-oriented approach.
On the Web of today, most websites heavily use a database backend for storing and delivering content. This directly implies establishing many connections to the database server, performing queries and retrieving result sets for displaying data. Particularly, when one site is attracting many visitors, this process rapidly introduces an overhead on the server that may lead to slower performance or, in critical situations, to complete system halts.
Addressing the problem from a developer's point of view, there are many methods one can use to reduce the server overload. The first basic method to improve the performance of a website may be done naturally, by reducing the amount of data sent to the client. This benefit can be easily obtained using byte-shaving techniques, such as binary file optimization (images, Flash files and so forth), (X)HTML, CSS, and JavaScript optimization, and finally HTTP compression, since most modern browsers support data compression in a transparent way. Fortunately, PHP offers a set of useful functions to handle efficiently compressed data.
The second method consists of sending data to the client as infrequently as possible. This concept immediately bring us to a couple of widely known improvement mechanisms: server and client caching. While client-based caching is mostly based on the inherent browser's ability to cache different files, relying strongly on a well-defined caching strategy, server-side caching offers a wider range of possibilities.
As you probably know, one nice way to implement server-side caching is by caching the entire HTML output generated by a PHP script (known as HTML output caching). This technique is used by popular PHP packages such as Smarty and PEAR:Cache_Lite. It allows you to work with more complex caching methods, including chunked caching, that is, several sections of a page that are individually cached according to a given data "freshness" policy.
However, HTML output caching is not very flexible. When the entire output is cached in a file, post processing on data may become a serious difficulty, since the whole script output, including the data and the HTML markup, is stored conjunctly in the same physical place.
On the other side, there is another caching method, useful for reducing the overhead in resource-consuming processes: result set caching. Certainly, caching result sets in a separate location can introduce major benefits, since it reduces the need to execute queries each time the page is accessed, while maintaining enough flexibility to carry out further processing on the data.
The main objective of this article series is to demonstrate how to implement a result set caching system, either taking a procedural approach or an object-oriented solution, in this way reducing the overall server overload when working with high traffic database-driven websites.
Are you feeling motivated to find out more about the topic? All right, let's get started!