[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

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

   1  <?php
   2  /*

   3  $Id: cache.php 3 2006-05-27 04:59:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2008 osCMax

   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    function tep_cache_categories_box($auto_expire = false, $refresh = false) {
 100  // LINE CHANGED

 101  //    global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string;

 102      global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string, $cat_name, $boxContent, $box_base_name;
 103  
 104      $cache_output = '';
 105  
 106      if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . str_replace ( '/', '-', bts_select('boxes', $box_base_name)) . '-' . $language . '.cache' . $cPath, $auto_expire)) {
 107        ob_start();
 108        include (DIR_WS_BOXES . 'categories.php');
 109        $cache_output = ob_get_contents();
 110        ob_end_clean();
 111        write_cache($cache_output, 'categories_box-' . str_replace ( '/', '-', bts_select('boxes', $box_base_name)) . '-' . $language . '.cache' . $cPath);
 112      }
 113  
 114      return $cache_output;
 115    }
 116  
 117  ////

 118  //! Cache the manufacturers box

 119  // Cache the manufacturers box

 120    function tep_cache_manufacturers_box($auto_expire = false, $refresh = false) {
 121      global $HTTP_GET_VARS, $language;
 122  
 123      $cache_output = '';
 124  
 125      $manufacturers_id = '';
 126      if (isset($HTTP_GET_VARS['manufacturers_id']) && is_numeric($HTTP_GET_VARS['manufacturers_id'])) {
 127        $manufacturers_id = $HTTP_GET_VARS['manufacturers_id'];
 128      }
 129  
 130      if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . str_replace ( '/', '-', bts_select('boxes', $box_base_name)) . '-' . $language . '.cache' . $manufacturers_id, $auto_expire)) {
 131        ob_start();
 132        include (DIR_WS_BOXES . 'manufacturers.php');
 133        $cache_output = ob_get_contents();
 134        ob_end_clean();
 135        write_cache($cache_output, 'manufacturers_box-' . str_replace ( '/', '-', bts_select('boxes', $box_base_name)) . '-' . $language . '.cache' . $manufacturers_id);
 136      }
 137  
 138      return $cache_output;
 139    }
 140  
 141  ////

 142  //! Cache the also purchased module

 143  // Cache the also purchased module

 144    function tep_cache_also_purchased($auto_expire = false, $refresh = false) {
 145      global $HTTP_GET_VARS, $language, $languages_id;
 146  
 147      $cache_output = '';
 148  
 149      if (isset($HTTP_GET_VARS['products_id']) && is_numeric($HTTP_GET_VARS['products_id'])) {
 150        if (($refresh == true) || !read_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id'], $auto_expire)) {
 151          ob_start();
 152          include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);
 153          $cache_output = ob_get_contents();
 154          ob_end_clean();
 155          write_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id']);
 156        }
 157      }
 158  
 159      return $cache_output;
 160    }
 161  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7