[ PHPXref.com ] [ Generated: Sun Jul 20 19:13:40 2008 ] [ osCommRes 1.2.0 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

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

   1  <?php
   2  /*

   3  

   4    osCommerce, Open Source E-Commerce Solutions

   5    http://www.oscommerce.com

   6  

   7    Copyright (c) 2003 osCommerce

   8    

   9    osCommRes, Services Online

  10    http://www.oscommres.com

  11  

  12    Copyright (c) 2005 osCommRes

  13  

  14    Released under the GNU General Public License

  15  */
  16  ////

  17  //! Write out serialized data.

  18  //  write_cache uses serialize() to store $var in $filename.

  19  //  $var      -  The variable to be written out.

  20  //  $filename -  The name of the file to write to.

  21    function write_cache(&$var, $filename) {
  22      $filename = DIR_FS_CACHE . $filename;
  23      $success = false;
  24  
  25  // try to open the file

  26      if ($fp = @fopen($filename, 'w')) {
  27  // obtain a file lock to stop corruptions occuring

  28        flock($fp, 2); // LOCK_EX

  29  // write serialized data

  30        fputs($fp, serialize($var));
  31  // release the file lock

  32        flock($fp, 3); // LOCK_UN

  33        fclose($fp);
  34        $success = true;
  35      }
  36  
  37      return $success;
  38    }
  39  
  40  ////

  41  //! Read in seralized data.

  42  //  read_cache reads the serialized data in $filename and

  43  //  fills $var using unserialize().

  44  //  $var      -  The variable to be filled.

  45  //  $filename -  The name of the file to read.

  46    function read_cache(&$var, $filename, $auto_expire = false){
  47      $filename = DIR_FS_CACHE . $filename;
  48      $success = false;
  49  
  50      if (($auto_expire == true) && file_exists($filename)) {
  51        $now = time();
  52        $filetime = filemtime($filename);
  53        $difference = $now - $filetime;
  54  
  55        if ($difference >= $auto_expire) {
  56          return false;
  57        }
  58      }
  59  
  60  // try to open file

  61      if ($fp = @fopen($filename, 'r')) {
  62  // read in serialized data

  63        $szdata = fread($fp, filesize($filename));
  64        fclose($fp);
  65  // unserialze the data

  66        $var = unserialize($szdata);
  67  
  68        $success = true;
  69      }
  70  
  71      return $success;
  72    }
  73  
  74  ////

  75  //! Get data from the cache or the database.

  76  //  get_db_cache checks the cache for cached SQL data in $filename

  77  //  or retreives it from the database is the cache is not present.

  78  //  $SQL      -  The SQL query to exectue if needed.

  79  //  $filename -  The name of the cache file.

  80  //  $var      -  The variable to be filled.

  81  //  $refresh  -  Optional.  If true, do not read from the cache.

  82    function get_db_cache($sql, &$var, $filename, $refresh = false){
  83      $var = array();
  84  
  85  // check for the refresh flag and try to the data

  86      if (($refresh == true)|| !read_cache($var, $filename)) {
  87  // Didn' get cache so go to the database.

  88  //      $conn = mysql_connect("localhost", "apachecon", "apachecon");

  89        $res = tep_db_query($sql);
  90  //      if ($err = mysql_error()) trigger_error($err, E_USER_ERROR);

  91  // loop through the results and add them to an array

  92        while ($rec = tep_db_fetch_array($res)) {
  93          $var[] = $rec;
  94        }
  95  // write the data to the file

  96        write_cache($var, $filename);
  97      }
  98    }
  99  
 100  ////

 101  //! Cache the categories box

 102  // Cache the categories box

 103    function tep_cache_categories_box($auto_expire = false, $refresh = false) {
 104      global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string;
 105  
 106      if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . $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-' . $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      $manufacturers_id = '';
 124      if (isset($HTTP_GET_VARS['manufactuers_id']) && tep_not_null($HTTP_GET_VARS['manufacturers_id'])) {
 125        $manufacturers_id = $HTTP_GET_VARS['manufacturers_id'];
 126      }
 127  
 128      if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id, $auto_expire)) {
 129        ob_start();
 130        include(DIR_WS_BOXES . 'manufacturers.php');
 131        $cache_output = ob_get_contents();
 132        ob_end_clean();
 133        write_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id);
 134      }
 135  
 136      return $cache_output;
 137    }
 138  
 139  ////

 140  //! Cache the also purchased module

 141  // Cache the also purchased module

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


[ Powered by PHPXref - Served by Debian GNU/Linux ]