Caching With PHP Cache_Lite - In And Out (
Page 5 of 9 )
The example you just saw cached the output of PHP statements. It's also
possible to cache static HTML content, through creative use of PHP's output
buffering functions. Consider the following example, which revises the code on
the previous page to cache HTML markup instead of PHP command output:
<?php
// include the package
require_once("Lite.php");
// set some variables
$options = array(
"cacheDir" => "cache/",
"lifeTime" => 5
);
// create a Cache_Lite object
$objCache = new Cache_Lite($options);
// test if there exists a valid cache
if ($page = $objCache->get("starwars"))
{
// if so, display it
echo $page;
// add a message indicating this is cached output
echo " [cached]";
}
else
{
// no cache
// so display the HTML output
// and save it to a buffer
ob_start();
?>
<html>
<head></head>
<body>
Do, or do not. There is no try.
<br>
-- Yoda,
<i>Star Wars</i> </body> </html>
<?php
// page generation complete
// retrieve the page from the buffer
$page = ob_get_contents();
// and save it in the cache for future use
$objCache->save($page, "starwars");
// also display the buffer and then flush it
ob_end_flush();
}
?>
In this case, PHP's output buffering functions have been used to capture all
the output generated subsequent to the call to ob_start(), and store this output
in a buffer. Once the entire output has been captured, the
ob_get_contents()
function is used to retrieve the buffer contents to a variable. This variable
(which now contains the entire HTML page) is then stored in the cache for future
use. Finally, the ob_end_flush() function is used to end output buffering and
send the contents of the buffer to the browser.
{mospagebreak title=Different
Strokes}
An alternative method to accomplish the task on the previous page lies with
the Cache_Lite_Output() class, a subclass of the Cache_Lite class. This subclass
(which internally uses output buffering) inherits all of the attributes of the
parent class, and adds two methods to the parent class' method collection: a
start() method, which begins caching data, and an
end() method, which marks
the end of data caching.
Consider the following variant of the example on the previosu page, which
illustrates how this may be used:
<?php
// include the package
require_once("Output.php");
// set some variables
$options = array(
"cacheDir" => "cache/",
"lifeTime" => 5
);
// create a Cache_Lite_Output object
$objCache = new Cache_Lite_Output($options);
// test if there exists a valid cache
// if so, display it, else regenerate the page
if (!$objCache->start("starwars"))
{
?>
<html>
<head></head>
<body>
Do or do not. There is no try.
<br>
-- Yoda,
<i>Star Wars</i> </body> </html>
<?php
// end caching
$objCache->end();
}
?>
Over here, if the call to start() - which is provided with a cache identifier
- returns true, it implies that the requested data has already been cached, and
the Cache_Lite_Output object takes care of printing the contents of the cache.
If, however, the call to start() returns false, it implies that the data is not
present in the cache, and so all subsequent output generated by the script will
be cached, until the object's end() method is invoked.