| [ Index ] |
|
Code source de osCommerce 2.2ms2-060817 |
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) 2006 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 $cache_output = ''; 104 105 if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath, $auto_expire)) { 106 ob_start(); 107 include(DIR_WS_BOXES . 'categories.php'); 108 $cache_output = ob_get_contents(); 109 ob_end_clean(); 110 write_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath); 111 } 112 113 return $cache_output; 114 } 115 116 //// 117 //! Cache the manufacturers box 118 // Cache the manufacturers box 119 function tep_cache_manufacturers_box($auto_expire = false, $refresh = false) { 120 global $HTTP_GET_VARS, $language; 121 122 $cache_output = ''; 123 124 $manufacturers_id = ''; 125 if (isset($HTTP_GET_VARS['manufactuers_id']) && is_numeric($HTTP_GET_VARS['manufacturers_id'])) { 126 $manufacturers_id = $HTTP_GET_VARS['manufacturers_id']; 127 } 128 129 if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id, $auto_expire)) { 130 ob_start(); 131 include(DIR_WS_BOXES . 'manufacturers.php'); 132 $cache_output = ob_get_contents(); 133 ob_end_clean(); 134 write_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id); 135 } 136 137 return $cache_output; 138 } 139 140 //// 141 //! Cache the also purchased module 142 // Cache the also purchased module 143 function tep_cache_also_purchased($auto_expire = false, $refresh = false) { 144 global $HTTP_GET_VARS, $language, $languages_id; 145 146 $cache_output = ''; 147 148 if (isset($HTTP_GET_VARS['products_id']) && is_numeric($HTTP_GET_VARS['products_id'])) { 149 if (($refresh == true) || !read_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id'], $auto_expire)) { 150 ob_start(); 151 include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS); 152 $cache_output = ob_get_contents(); 153 ob_end_clean(); 154 write_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id']); 155 } 156 } 157 158 return $cache_output; 159 } 160 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 19:48:25 2007 | par Balluche grâce à PHPXref 0.7 |
|