PostgreSQL & PHP Tutorials - Using the Cache Object

PHP »  Creating a Cache
Site News »  Creating a Cache

Posted By Chris Smith Posted on 02 Apr 2006, 09:08 PM
Viewing page 2 of 2
« | Back | Next | »

There are 3 main areas to look at (in my cache anyway).

ReadCache
It will check for the cache file (based on the name you pass in).
If that isn't present, it will return false.
If the cache file is present, it will read it and return it.


WriteCache
It will write the content to the filename passed in.


ClearCache
If you pass in a filename, it will remove only that file from the cache. If you don't pass in a filename, it will remove everything from the cache directory.


It's quite easy to use it.


<?php
/**
* Include the cache class file.
*/
require('/path/to/cache.php');

/**
* Set up the cache object.
*/
$cache_object = &new Cache('/path/to/cache/directory');

/**
* If we can read the cache, do it and then exit.
*/
if ($cache_contents = $cache_object->ReadCache('Index')) {
    echo
$cache_contents;
    exit();
}

....

/**
* Write the cache out.
*/

$cache_object->WriteCache('Index', $page_contents);

?>


Nice and easy!

There are other pre-built cache systems, such as Pear's Cache Lite and Pear's Cache.

The only caveat in any cache system is making sure you clear it out at the right time.

This CMS is written using Object Oriented Programming, so it was easy for me to do this in the Create, Save and Delete methods.
Viewing page 2 of 2
« | Back | Next | »



Avg Rating: 3
Vote Count: 1


              


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 6 + 6 =
Comment