dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file cache.php

Documentation is available at cache.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @package dbscript
  10.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11.    */
  12.  
  13.   /**
  14.    * Cache
  15.    * 
  16.    * Incomplete/Experimental
  17.    * needs adapting for dbscript
  18.    * 
  19.    * More info...
  20.    * {@link http://dbscript.net/cache}
  21.    * 
  22.    * @package dbscript
  23.    * @author Alejandro Gervasio
  24.    * @access public
  25.    * @version 0.1.2
  26.    * @todo implement
  27.    */
  28.  
  29. class Cache {
  30.  
  31.   var $mysql;      // instance of MySQL object
  32.     var $result;     // instance of Result object
  33.     var $expiry;     // cache expire time in seconds
  34.     var $cacheFile;  // cache file
  35.     var $data;       // result set array
  36.  
  37.   // constructor
  38.     function Cache&$mysql$expiry 86400$cacheFile 'default_cache.txt' {
  39.     $this->mysql =$mysql;
  40.     is_int$expiry && $expiry>0$this->expiry = $expiry$this->mysql->isError'Expire time must be a positive integer' );
  41.     $this->cacheFile = $cacheFile;
  42.     $this->data = array();
  43.   }
  44.  
  45.   // if cache is valid, perform query and return a result set. Otherwise, get results from cache file
  46.     function query$query {
  47.     // check if query starts with SELECT
  48.     if !preg_match"/^SELECT/"$query ) ) {
  49.       $this->mysql->isError'Invalid query. Must start with SELECT' );
  50.     }
  51.     if !$this->isValid() ) {
  52.       // read from MySQL
  53.       $this->result = $this->mysql->query$query );
  54.       $this->data = $this->write();
  55.     else {
  56.       // read from cache file
  57.       $this->data = $this->read();
  58.     }
  59.   }
  60.  
  61.   // write cache file
  62.     function write({
  63.     if !$fp fopen$this->cacheFile'w' ) ) {
  64.       $this->mysql->isError'Error opening cache file' );
  65.     }
  66.     if !flock$fpLOCK_EX ) ) {
  67.       $this->mysql->isError'Unable to lock cache file' );
  68.     }
  69.     while$row $this->result->fetchRow() ) {
  70.       $content[$row;
  71.     }
  72.     if!fwrite$fpserialize$content ) ) ) {
  73.       $this->mysql->isError'Error writing to cache file' );
  74.     }
  75.     flock$fpLOCK_UN );
  76.     fclose$fp );
  77.     unset$fp$row );
  78.     return $content;
  79.   }
  80.  
  81.   // read cache file
  82.     function read({
  83.     if !$content unserializefile_get_contents$this->cacheFile ) ) ) {
  84.       $this->mysql->isError'Error reading from cache file' );
  85.     }
  86.     return $content;
  87.   }
  88.  
  89.   // determine cache validity based on a time expiry trigger
  90.     function isValid({
  91.     if file_exists$this->cacheFile && filemtime$this->cacheFile time($this->expiry ) ) {
  92.       return true;
  93.     }
  94.     return false;
  95.   }
  96.  
  97.   // fetch cache row
  98.     function fetchRow(){
  99.     if !$row current$this->data ) ) {
  100.       return false;
  101.     }
  102.     next$this->data );
  103.     return $row;
  104.   }
  105.  
  106.   // fetch all cache rows
  107.     function fetchAll(){
  108.     if count$this->data {
  109.       $this->mysql->isError'Error accessing cache data' );
  110.     }
  111.     return $this->data;
  112.   }
  113.  
  114.   // count cache rows
  115.     function countRows({
  116.     if !$rows count$this->data ) ) {
  117.       $this->mysql->isError'Error counting cache rows' );
  118.     }
  119.     return $rows;
  120.   }
  121.  
  122. }
  123.  
  124. ?>

Documentation generated on Mon, 19 Feb 2007 10:24:41 -0800 by phpDocumentor 1.3.1