Home arrow PHP arrow Page 5 - Caching With PHP Cache_Lite

In And Out - PHP

As Web applications become more and more complex, cleverdevelopers can use application-level caching to improve the performanceof their PHP scripts. This article shows you how, discussing the PEARCache_Lite class with examples that illustrate how it can be used in alive environment.

TABLE OF CONTENTS:
  1. Caching With PHP Cache_Lite
  2. The Food Chain
  3. Return Of The Jedi
  4. Digging Deeper
  5. In And Out
  6. Bits And Bytes
  7. No News Is Good News
  8. Cache Cow
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 22
June 06, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- 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>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- 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.



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: