PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/includes/functions/cache.php

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