[ Index ]
osCommerce Docs :: PHP Cross Reference For osCommerce 2.2 MS2
Provided By OSCdox.com

/includes/functions/ -> cache.php (source)

[Summary view]

   1  <?php
   2  /*
   3    $Id: cache.php,v 1.11 2003/07/01 14:34:54 hpdl Exp $
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Copyright (c) 2003 osCommerce
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13  ////
  14  //! Write out serialized data.
  15  //  write_cache uses serialize() to store $var in $filename.
  16  //  $var      -  The variable to be written out.
  17  //  $filename -  The name of the file to write to.
  18    function write_cache(&$var, $filename) {
  19      $filename = DIR_FS_CACHE . $filename;
  20      $success = false;
  21  
  22  // try to open the file
  23      if ($fp = @fopen($filename, 'w')) {
  24  // obtain a file lock to stop corruptions occuring
  25        flock($fp, 2); // LOCK_EX
  26  // write serialized data
  27        fputs($fp, serialize($var));
  28  // release the file lock
  29        flock($fp, 3); // LOCK_UN
  30        fclose($fp);
  31        $success = true;
  32      }
  33  
  34      return $success;
  35    }
  36  
  37  ////
  38  //! Read in seralized data.
  39  //  read_cache reads the serialized data in $filename and
  40  //  fills $var using unserialize().
  41  //  $var      -  The variable to be filled.
  42  //  $filename -  The name of the file to read.
  43    function read_cache(&$var, $filename, $auto_expire = false){
  44      $filename = DIR_FS_CACHE . $filename;
  45      $success = false;
  46  
  47      if (($auto_expire == true) && file_exists($filename)) {
  48        $now = time();
  49        $filetime = filemtime($filename);
  50        $difference = $now - $filetime;
  51  
  52        if ($difference >= $auto_expire) {
  53          return false;
  54        }
  55      }
  56  
  57  // try to open file
  58      if ($fp = @fopen($filename, 'r')) {
  59  // read in serialized data
  60        $szdata = fread($fp, filesize($filename));
  61        fclose($fp);
  62  // unserialze the data
  63        $var = unserialize($szdata);
  64  
  65        $success = true;
  66      }
  67  
  68      return $success;
  69    }
  70  
  71  ////
  72  //! Get data from the cache or the database.
  73  //  get_db_cache checks the cache for cached SQL data in $filename
  74  //  or retreives it from the database is the cache is not present.
  75  //  $SQL      -  The SQL query to exectue if needed.
  76  //  $filename -  The name of the cache file.
  77  //  $var      -  The variable to be filled.
  78  //  $refresh  -  Optional.  If true, do not read from the cache.
  79    function get_db_cache($sql, &$var, $filename, $refresh = false){
  80      $var = array();
  81  
  82  // check for the refresh flag and try to the data
  83      if (($refresh == true)|| !read_cache($var, $filename)) {
  84  // Didn' get cache so go to the database.
  85  //      $conn = mysql_connect("localhost", "apachecon", "apachecon");
  86        $res = tep_db_query($sql);
  87  //      if ($err = mysql_error()) trigger_error($err, E_USER_ERROR);
  88  // loop through the results and add them to an array
  89        while ($rec = tep_db_fetch_array($res)) {
  90          $var[] = $rec;
  91        }
  92  // write the data to the file
  93        write_cache($var, $filename);
  94      }
  95    }
  96  
  97  ////
  98  //! Cache the categories box
  99  // Cache the categories box
 100    function tep_cache_categories_box($auto_expire = false, $refresh = false) {
 101      global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string;
 102  
 103      if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath, $auto_expire)) {
 104        ob_start();
 105        include (DIR_WS_BOXES . 'categories.php');
 106        $cache_output = ob_get_contents();
 107        ob_end_clean();
 108        write_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath);
 109      }
 110  
 111      return $cache_output;
 112    }
 113  
 114  ////
 115  //! Cache the manufacturers box
 116  // Cache the manufacturers box
 117    function tep_cache_manufacturers_box($auto_expire = false, $refresh = false) {
 118      global $HTTP_GET_VARS, $language;
 119  
 120      $manufacturers_id = '';
 121      if (isset($HTTP_GET_VARS['manufactuers_id']) && tep_not_null($HTTP_GET_VARS['manufacturers_id'])) {
 122        $manufacturers_id = $HTTP_GET_VARS['manufacturers_id'];
 123      }
 124  
 125      if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id, $auto_expire)) {
 126        ob_start();
 127        include (DIR_WS_BOXES . 'manufacturers.php');
 128        $cache_output = ob_get_contents();
 129        ob_end_clean();
 130        write_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id);
 131      }
 132  
 133      return $cache_output;
 134    }
 135  
 136  ////
 137  //! Cache the also purchased module
 138  // Cache the also purchased module
 139    function tep_cache_also_purchased($auto_expire = false, $refresh = false) {
 140      global $HTTP_GET_VARS, $language, $languages_id;
 141  
 142      if (($refresh == true) || !read_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id'], $auto_expire)) {
 143        ob_start();
 144        include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);
 145        $cache_output = ob_get_contents();
 146        ob_end_clean();
 147        write_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id']);
 148      }
 149  
 150      return $cache_output;
 151    }
 152  ?>


Generated: Tue Nov 4 23:53:39 2003
Hosted By :: AABox.com
Cross-referenced by PHPXref 0.4