Login   Register  
PHP Classes
elePHPant
Icontem

File: cache.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Shaun Thomas  >  cache  >  cache.inc  >  Download  
File: cache.inc
Role: ???
Content type: text/plain
Description: Cache Class
Class: cache
Author: By
Last change:
Date: 2001-06-07 21:51
Size: 2,681 bytes
 

Contents

Class file image Download
<?PHP
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
// Package : cache               Version : 1.2
// Date    : 04/20/2001          Author  : Shaun Thomas
// Req     : PHP 3.0               Type  : Class
//
// Description:
// ------------
// Caching should be a "behind the scenes" kind of thing.  So, this
// class was designed to handle all caching abilities, whether it be
// through file or database.  The user of the cacheing system need
// not know.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //

class CCache
{
  var $cachePath,    // Path to save or load the cache from.
      $fileName,     // Filename we'll generate for all save/loads.
      $expire;       // When cache pages should be recalculated. (in seconds)

  // -------------------------------------------------- //
  // A constructor sets up our cache environment so
  // we can start shuffling files around.  All we need
  // is a path to save/retrieve files at/from.
  // -------------------------------------------------- //
  function CCache($cPath)
  {
    global $QUERY_STRING, $SCRIPT_NAME;

    $this->cachePath = $cPath;

    // Generate a filename based on this script's name,
    // and query string.
    $this->fileName=base64_encode($SCRIPT_NAME.$QUERY_STRING);

  } // End function CCache.


  // -------------------------------------------------- //
  // Saves whatever is sent to this function, to the
  // generated filename for this script.
  // -------------------------------------------------- //
  function save(&$contents)
  {
    $fp = fopen($this->cachePath."/".$this->fileName, "w");
    if (!$fp) return;

    fwrite($fp, $contents);
    fclose($fp);

  } // End function save.


  // -------------------------------------------------- //
  // Of course, we need some way of getting a cached
  // file.  This returns false if there is no file.
  // -------------------------------------------------- //
  function retrieve()
  {
    $fName = $this->cachePath."/".$this->fileName;

    // If there is no cache page, or the page is expired, tell
    // the caller to recalculate it.
    if (!file_exists($fName)) return FALSE;
    if (filemtime($fName) < (time() - $this->expire)) return FALSE;

    $fp = fopen($fName, "r");
    if (!$fp) return FALSE;

    $retVal = fread($fp, filesize($fName));
    fclose($fp);

    return $retVal;

  } // End function retrieve.
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
// End cache class
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
?>