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

| A Galaxy Far, Far Away |

The Cache class is a PEAR package which provides a caching framework for PHP developers. It's currently maintained by Christian Stocker, Ulf Wendel and Helgi Pormar, and is freely available from http://pear.php.net/package/Cache.

To understand how it works, consider the following simple script:


<?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("starwars");

// if cached data available, print it
// add a message indicating this is cached output
if ($quote = $cache->get($id)) {
    echo $quote;
    echo " [cached]";
// else obtain fresh data and print it
// also save it to the cache for the next run
// auto-expire the cached data after 2 min
} else {
    $quote = "Do, or do not. There is no try. -- Yoda, Star Wars"; 
    echo $quote;
    $cache->save($id, $quote, 120);
}
?>


Now, the first time, you run this script, the page will be generated from scratch and you'll see something like this:


Do, or do not. There is no try. -- Yoda, Star Wars


Next, try refreshing the page. This time, the output will be retrieved from the cache:


Do, or do not. There is no try. -- Yoda, Star Wars [cached]


Let's take a quick look at the script above. I begin by importing the Cache class, and creating a new Cache() object. This object is initialized with two arguments: the "container" to use for the cached data (here, a file, although you can also use a database), and an array containing container-specific options (here, the location of the cache directory).

You can use multiple simultaneous caches with the Cache class, which is why every cache must be given a unique identifier. The object's generateID() method is used to create this from the identifying string "starwars".

Once an instance of the Cache object has been created, the business logic to use it becomes fairly simple. The first step is to check if the required data already exists in the cache. If it doesn't, it should be generated from the original data source, and a copy saved to the cache for future use. If it does, you can do something useful with it - write it to a file, pipe it to an external program or output it to the client.

Checking whether the data already exists in the cache is accomplished with the get() method, while writing a fresh snapshot to the cache is done with the save() method. Notice that both methods required the ID generated by generateID() to identify which cache to access; the save() method additionally lets you specify a duration (in seconds) for which the cache is valid.

The steps above make up a fairly standard process for using the Cache class, and you'll see them being repeated over and over again in subsequent examples as well.


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
2017 Melonfire. All rights reserved
Terms and Conditions | Feedback