/modules/shop/includes/classes/cache.php

https://github.com/severnaya99/Sg-2010 · PHP · 206 lines · 176 code · 8 blank · 22 comment · 20 complexity · 2176a13c8f41b2b2c2f75ae01723b3a2 MD5 · raw file

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // |zen-cart Open Source E-commerce |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2003 The zen-cart developers |
  7. // | |
  8. // | http://www.zen-cart.com/index.php |
  9. // | |
  10. // | Portions Copyright (c) 2003 osCommerce |
  11. // +----------------------------------------------------------------------+
  12. // | This source file is subject to version 2.0 of the GPL license, |
  13. // | that is bundled with this package in the file LICENSE, and is |
  14. // | available through the world-wide-web at the following url: |
  15. // | http://www.zen-cart.com/license/2_0.txt. |
  16. // | If you did not receive a copy of the zen-cart license and are unable |
  17. // | to obtain it through the world-wide-web, please send a note to |
  18. // | license@zen-cart.com so we can mail you a copy immediately. |
  19. // +----------------------------------------------------------------------+
  20. // $Id: cache.php 801 2004-12-13 23:50:33Z wilt $
  21. //
  22. class cache {
  23. function sql_cache_exists($zf_query) {
  24. global $db;
  25. $zp_cache_name = $this->cache_generate_cache_name($zf_query);
  26. switch (SQL_CACHE_METHOD) {
  27. case 'file':
  28. // where using a single directory at the moment. Need to look at splitting into subdirectories
  29. // like adodb
  30. if (file_exists(DIR_FS_SQL_CACHE . '/' . $zp_cache_name . '.sql')) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. break;
  36. case 'database':
  37. $sql = "select * from " . TABLE_DB_CACHE . " where cache_entry_name = '" . $zp_cache_name . "'";
  38. $zp_cache_exists = $db->Execute($sql);
  39. if ($zp_cache_exists->RecordCount() > 0) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. break;
  45. case 'memory':
  46. return false;
  47. break;
  48. case 'none':
  49. return false;
  50. break;
  51. }
  52. }
  53. function sql_cache_is_expired($zf_query, $zf_cachetime) {
  54. global $db;
  55. $zp_cache_name = $this->cache_generate_cache_name($zf_query);
  56. switch (SQL_CACHE_METHOD) {
  57. case 'file':
  58. if (filemtime(DIR_FS_SQL_CACHE . '/' . $zp_cache_name . '.sql') > (time() - $zf_cachetime)) {
  59. return false;
  60. } else {
  61. return true;
  62. }
  63. break;
  64. case 'database':
  65. $sql = "select * from " . TABLE_DB_CACHE . " where cache_entry_name = '" . $zp_cache_name ."'";
  66. $cache_result = $db->Execute($sql);
  67. if ($cache_result->RecordCount() > 0) {
  68. $start_time = $cache_result->fields['cache_entry_created'];
  69. if (time() - $start_time > $zf_cachetime) return true;
  70. return false;
  71. } else {
  72. return true;
  73. }
  74. break;
  75. case 'memory':
  76. return true;
  77. break;
  78. case 'none':
  79. return true;
  80. break;
  81. }
  82. }
  83. function sql_cache_expire_now($zf_query) {
  84. global $db;
  85. $zp_cache_name = $this->cache_generate_cache_name($zf_query);
  86. switch (SQL_CACHE_METHOD) {
  87. case 'file':
  88. @unlink(DIR_FS_SQL_CACHE . '/' . $zp_cache_name . '.sql');
  89. return true;
  90. break;
  91. case 'database':
  92. $sql = "delete from " . TABLE_DB_CACHE . " where cache_entry_name = '" . $zp_cache_name . "'";
  93. $db->Execute($sql);
  94. return true;
  95. break;
  96. case 'memory':
  97. unset($this->cache_array[$zp_cache_name]);
  98. return true;
  99. break;
  100. case 'none':
  101. return true;
  102. break;
  103. }
  104. }
  105. function sql_cache_store($zf_query, $zf_result_array) {
  106. global $db;
  107. $zp_cache_name = $this->cache_generate_cache_name($zf_query);
  108. switch (SQL_CACHE_METHOD) {
  109. case 'file':
  110. $OUTPUT = serialize($zf_result_array);
  111. $fp = fopen(DIR_FS_SQL_CACHE . '/' . $zp_cache_name . '.sql',"w");
  112. fputs($fp, $OUTPUT);
  113. fclose($fp);
  114. return true;
  115. break;
  116. case 'database':
  117. $result_serialize = $db->prepare_input(serialize($zf_result_array));
  118. $sql = "insert into " . TABLE_DB_CACHE . " set cache_entry_name = '" . $zp_cache_name . "',
  119. cache_data = '" . $result_serialize . "',
  120. cache_entry_created = '" . time() . "'";
  121. $db->Execute($sql);
  122. return true;
  123. break;
  124. case 'memory':
  125. return true;
  126. break;
  127. case 'none':
  128. return true;
  129. break;
  130. }
  131. }
  132. function sql_cache_read($zf_query) {
  133. global $db;
  134. $zp_cache_name = $this->cache_generate_cache_name($zf_query);
  135. switch (SQL_CACHE_METHOD) {
  136. case 'file':
  137. $zp_fa = file(DIR_FS_SQL_CACHE . '/' . $zp_cache_name . '.sql');
  138. $zp_result_array = unserialize(implode('', $zp_fa));
  139. return $zp_result_array;
  140. break;
  141. case 'database':
  142. $sql = "select * from " . TABLE_DB_CACHE . " where cache_entry_name = '" . $zp_cache_name . "'";
  143. $zp_cache_result = $db->Execute($sql);
  144. $zp_result_array = unserialize($zp_cache_result->fields['cache_data']);
  145. return $zp_result_array;
  146. break;
  147. case 'memory':
  148. return true;
  149. break;
  150. case 'none':
  151. return true;
  152. break;
  153. }
  154. }
  155. function sql_cache_flush_cache() {
  156. global $db;
  157. switch (SQL_CACHE_METHOD) {
  158. case 'file':
  159. if ($za_dir = @dir(DIR_FS_SQL_CACHE)) {
  160. while ($zv_file = $za_dir->read()) {
  161. if (strstr($zv_file, '.sql') && strstr($zv_file, 'zc_')) {
  162. @unlink(DIR_FS_SQL_CACHE . '/' . $zv_file);
  163. }
  164. }
  165. }
  166. return true;
  167. break;
  168. case 'database':
  169. $sql = "delete from " . TABLE_DB_CACHE;
  170. $db->Execute($sql);
  171. return true;
  172. break;
  173. case 'memory':
  174. return true;
  175. break;
  176. case 'none':
  177. return true;
  178. break;
  179. }
  180. }
  181. function cache_generate_cache_name($zf_query) {
  182. switch (SQL_CACHE_METHOD) {
  183. case 'file':
  184. return 'zc_' . md5($zf_query);
  185. break;
  186. case 'database':
  187. return 'zc_' . md5($zf_query);
  188. break;
  189. case 'memory':
  190. return 'zc_' . md5($zf_query);
  191. break;
  192. case 'none':
  193. return true;
  194. break;
  195. }
  196. }
  197. }
  198. ?>