Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Caching Web Sites With PEAR Cache
Improve site performance with a PHP-driven cache

| Mixing It Up |

Of course, in the real world, it's unlikely that you'll want to cache a single line of output. It's more likely that you'll have a complete page to cache, usually with PHP code mixed in with the HTML formatting instructions. It's fairly easy to adapt the steps outlined on the previous page to fit this situation, by adding an output buffer to the mix. Take a look:


<?php
// include the package
include("Cache.php");

// initialize cache
$cache = new Cache("file", array("cache_dir" => "cache"));

// generate an ID for this cache
$id = $cache->generateID("index");

// look in the cache for this page
// if it exists, print it and exit
if ($data = $cache->get($id)) {
    echo $data;
    echo "<br> [cached data]";
// if not, generate the page from scratch
// and save it to the output buffer
} else {
    ob_start();
?>

    <!-- page begins -->
    <html>
    <head></head>
    <body>
    My favourite fruits:
    <ul>
    <?php
    // mix in some PHP code
    $fruits = array("apples", "oranges", "bananas");
    foreach ($fruits as $f) {
        echo "<li>$f";
    }
    ?>
    </ul>
    </body>
    </html>
    <!-- page ends -->

<?php
    // save the output buffer to the cache
    // for the next run
    // then dump it to the client
    $output = ob_get_contents();
    $cache->save($id, $output, 120);
    ob_end_flush();
}
?>


Here, if the call to get() returns false, the script initializes PHP's output buffer with ob_start() and then generates a complete page (including the dynamic PHP bits). The contents of the output buffer are then retrieved with a call to ob_get_contents(), and saved to the cache in readiness for the next request with the save() method. The ob_end_flush() method then flushes the output buffer and sends the page to the browser.

An alternative way to do this involves using the Cache_Output class, a subclass of the Cache 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. The following listing, which is equivalent to the previous one, demonstrates usage:


<?php
// include the package
include("Cache.php");
include("Cache/Output.php");

// initialize cache
$cache = new Cache_Output("file", array("cache_dir" => "cache"));

// generate an ID for this cache
$id = $cache->generateID("index");

// look in the cache for this page
// if it exists, print it and exit
if ($data = $cache->start($id)) {
    echo $data;
    echo "<br> [cached data]";
// if not, generate the page from scratch
// and save it to the output buffer
} else {
?>

    <!-- page begins -->
    <html>
    <head></head>
    <body>
    My favourite fruits:
    <ul>
    <?php
    // mix in some PHP code
    $fruits = array("apples", "oranges", "bananas");
    foreach ($fruits as $f) {
        echo "<li>$f";
    }
    ?>
    </ul>
    </body>
    </html>
    <!-- page ends -->

<?php
// save the output buffer to the cache
// for the next run
// then dump it to the client
    echo $cache->end(120);
}
?>


Here, the start() method checks the cache to see if a page already exists for the named ID; if it doesn't, the page is generated in the output buffer, saved to the cache and then flushed to the browser once the end() method is called. The same as the previous script, really, except this version is a little more compact.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback