Dynamic Web pages take longer to load than static ones, which forces visitors to your site to wait -- and we all know they won't wait for very long. Output caching is a powerful technique you can use to shorten that wait and keep them from leaving.
If we take a nostalgic look at those distant days when developing Websites consisted of writing a few, static HTML pages, made up mostly of text and scarce images, we notice that the process of sending a Web page from the Web server to a Web browser was painless and straightforward. The server had to fetch some files from the file system, and send them happily to the user’s browser, which received the parsed page almost immediately.
Once the page was displayed, the browser would trigger its caching routines to store the finished page on the user's computer, until a new request was made for that page. With a new request for that page, the browser performed a fast check with the Web server to make sure the page hadn’t been updated, and then could display the cached, locally stored version. Ah, memories!
As you know, the Web became more and more complex, and new techniques had to be developed for delivering dynamic content. Most websites migrated from static to dynamic schemas, according to current demands for satisfying content-hungry visitors. This new context rapidly broke the well-established process described above.
Now, nearly every page requested puts into action some kind of intermediate process (or many of them) that introduces a noticeable delay when delivering Web pages to the end user. If you are within the field of PHP processing, you know that the Web server will call the PHP engine for parsing scripts, and maybe those scripts will connect to databases, and so on, all of this resulting in a lag between the user’s request for a page and the final display in the browser.
If keeping our applications simple is not mandatory, they will become more complex, and easily cause us to run into difficulties that could increase the lag problem. There are many possible approaches to solve this situation. In this article, we will see how server side caching can help us to implement simple but powerful PHP mechanisms to avoid, or reduce, the delay between a Web server sending a dynamic page and the user seeing the finished result displayed on the machine.