Login   Register  
PHP Classes
elePHPant
Icontem

File: test_file_cache.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Manuel Lemos  >  File cache class  >  test_file_cache.php  >  Download  
File: test_file_cache.php
Role: Example script
Content type: text/plain
Description: Example script that demonstrates how to use the class.
Class: File cache class
Cache arbitrary data in files.
Author: By
Last change:
Date: 11 years ago
Size: 2,428 bytes
 

Contents

Class file image Download
<?php

/*
 * test_file_cache.php
 *
 * @(#) $Id: test_file_cache.php,v 1.1 2003/11/21 07:54:41 mlemos Exp $
 *
 */

    
require('filecacheclass.php');

?>
<html>
<head>
<title>Test Manuel Lemos File Cache class</title>
</head>
<body>
<center><h1>Test Manuel Lemos File Cache class</h1></center>
<hr />
<?php

    
/*
     *  First create an object of the class.
     */
    
$cache_object=&new file_cache_class;

    
/*
     *  Then set the variable of the path of the cache file.
     *  In the end, take a look at the contents of this file.
     */
    
$cache_object->path='mycachefile.cache';

    
/*
     *  If we want to take different action if the cache is being updated,
     *  we can ask the class to tell if an update is in progress.
     */
    
$success=$cache_object->updating($updating);

    
/*
     *  Check the success value to make sure there was no error.
     */
    
if($success)
    {
        if(
$updating)
        {
            echo 
'Sorry, the cache file is being updated by another process running simultaneously. Please come back later.';
        }
        else
        {

            
/*
             *  Now verify if the cache file exists is upto date.
             */
            
$success=$cache_object->verifycache($updated);

            
/*
             *  Check the success value to make sure there was no error.
             */
            
if($success)
            {

                
/*
                 *  Is the cache file upto date?
                 */
                
if($updated)
                {

                    
/*
                     *  Yes. Now, let's read from the cache file and output the stored data.
                     */
                    
$endofcache=0;
                    for(;!
$endofcache;)
                    {
                        
$success=$cache_object->retrievefromcache($data,$endofcache);
                        if(!(
$success))
                            break;
                        echo 
$data;
                    }
                }
                else
                {

                    
/*
                     *  No.  The cache file needs to be regenerated from fresh data.
                     *  Let's say that this is all the data I need to store in the cache.
                     */
                    
$data="Hello world! :-)\n";

                    
/*
                     *  If necessary, set the cache timeout period in seconds.
                     */
                    
$cachetimeout=60;
                    
$cache_object->setexpirytime($cachetimeout);

                    
/*
                     *  Store the cache data, all at once.
                     */
                    
$success=$cache_object->storedata($data,1);

                    
/*
                     *  If it was all ok, let's display the data.
                     */
                    
if($success)
                    {
                        echo 
$data;
                    }
                }
            }
        }
    }

    
/*
     *  Finally, let's just do some error handling.
     */
    
if(!($success))
    {
        echo (
'Error: '.$cache_object->error."\n");
    }
?>
<hr />
<address>Manuel Lemos (<a href="mailto:mlemos@acm.org">mlemos@acm.org</a>)</address>
</body>
</html>