PHP Anthology, Volume 2: Applications. Chapter 5: Caching | 3
PHP Anthology, Volume 2: Applications. Chapter 5: Caching
How do I capture server side output for caching?
It’s time to look at how we can reduce server side delay by caching output. The general approach begins by rendering the page as normal, performing database queries and so on with PHP. However, before sending it to the browser, we capture and store the finished page somewhere, for instance, in a file. The next time the page is requested, the PHP script first checks to see whether a cached version of the page exists. If it does, the script sends the cached version straight to the browser, avoiding the delay involved in rebuilding the page.What about Template Caching?
Template engines such as Smarty often talk about template caching. Usually, these engines offer an in-built mechanism for storing a compiled version of a template (i.e. the native PHP generated from the template), which prevents us having to recompile the template every time a page is requested. This should not be confused with output caching, which refers to the caching of the rendered HTML (or other output) that PHP sends to the browser. You can successfully use both types of caching together on the same site.
Here, we’ll look at PHP’s in-built caching mechanism, the output buffer, which can be used with whatever page rendering system you prefer (templates or no templates). Consider a situation in which your script displays results using, for example, echo or print, rather than sending the data directly to the browser. In these cases, you can use PHP’s output control functions to store the data in an in-memory buffer, which your PHP script has both access to and control over.
Here’s a simple example:
<?php // Start buffering the output ob_start(); // Echo some text (which is stored in the buffer); echo '1. Place this in the buffer<br />'; // Get the contents of $buffer = ob_get_contents(); // Stop buffering and clean out the buffer ob_end_clean(); // Echo some text normally echo '2. A normal echo<br />'; // Echo the contents from the buffer echo $buffer; ?>
The buffer itself stores the output as a string. So, in the above script, we commence buffering with ob_start and use echo to display something. We then use ob_get_contents to fetch the data the echo statement placed in the buffer, and store it in a string. The ob_end_clean function stops the output buffer and trashes the contents; the alternative is ob_end_flush, which displays the contents of the buffer.
The above script displays:
2. A normal echo 1. Place this in the buffer
In other words, we captured the output of the first echo, then sent it to the browser after the second echo. As this simple example suggests, output buffering can be a very powerful tool when it comes to building your site; it provides a solution for caching, as we’ll see in a moment, and is an excellent way to hide errors from your site’s visitors (see Chapter 10, Error Handling ). It even provides a possible alternative to browser redirection in situations such as user authentication.
HTTP Headers and Output Buffering
Output buffering can help solve the most common problem associated with the header function, not to mention session_start and set_cookie . Normally, if you call any of these functions after page output has begun, you’ll get a nasty error message. With output buffering turned on, the only output types that can escape the buffer are HTTP headers. Using ob_start at the very beginning of your application’s execution, you can send headers at whichever point you like, without encountering the usual errors. You can then write out the buffered page content all at once, when you’re sure there are no more HTTP headers required.
Now you’ve seen a basic example of output buffering, here’s the next step, in which the buffer is stored as a file:
<?php
// If a cached version exists use it...
if (file_exists('./cache/2.cache')) {
// Read and display the file
readfile('./cache/2.cache');
exit();
}
// Start buffering the output
ob_start();
// Display some HTML
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Cached Page </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
This page was cached with PHP's
<a href="http://www.php.net/outcontrol">Output Control
Functions</a>
</body>
</html>
<?php
// Get the contents of the buffer
$buffer = ob_get_contents();
// Stop buffering and display the buffer
ob_end_flush();
// Write a cache file from the contents
$fp = fopen('./cache/2.cache', 'w');
fwrite($fp, $buffer);
fclose($fp);
?>
First, the above script checks to see if a cached version of the page exists and, if it does, the script reads and displays it. Otherwise, it uses output buffering to create a cached version of the page. It stores this as a file, while using ob_end_flush to display the page to the visitor.
The file 2.cache looks exactly like the HTML that was rendered by the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> Cached Page </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> This page was cached with PHP's <a href="http://www.php.net/outcontrol">Output Control Functions</a> </body> </html>
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth3

Find a programming school near you