php缓存数据功能的mysqli类

  1. <?php
  2. /**
  3. * Mysqli类
  4. *
  5. * @author 废墟
  6. * @version v1.0 2009-08-18
  7. */
  8. class db_mysqli {
  9. protected $mysqli;
  10. protected $sql;
  11. protected $rs;
  12. protected $query_num = 0;
  13. protected $fetch_mode = MYSQLI_ASSOC;
  14. protected $cache_dir = './cache/';
  15. protected $cache_time = 1800;
  16. public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
  17. $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  18. if(mysqli_connect_errno()) {
  19. $this->mysqli = false;
  20. echo '<h2>'.mysqli_connect_error().'</h2>';
  21. die();
  22. } else {
  23. $this->mysqli->set_charset("utf8");
  24. }
  25. }
  26. public function __destruct() {
  27. $this->free();
  28. $this->close();
  29. }
  30. protected function free() {
  31. @$this->rs->free();
  32. }
  33. protected function close() {
  34. $this->mysqli->close();
  35. }
  36. protected function fetch() {
  37. return $this->rs->fetch_array($this->fetch_mode);
  38. }
  39. protected function getQuerySql($sql, $limit = null) {
  40. if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
  41. $sql .= " LIMIT " . $limit;
  42. }
  43. return $sql;
  44. }
  45. protected function get_cache($sql,$method) {
  46. include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件
  47. $cache = new cache($this->cache_dir,$this->cache_time);
  48. $cache_file = md5($sql.$method);
  49. $res = $cache->get_cache($cache_file);
  50. if(!$res) {
  51. $res = $this->$method($sql);
  52. $cache->set_cache($cache_file, $res);
  53. }
  54. return $res;
  55. }
  56. public function query_num() {
  57. return $this->query_num;
  58. }
  59. public function set_cache_dir($cache_dir) {
  60. $this->cache_dir = $cache_dir;
  61. }
  62. public function set_cache_time($cache_time) {
  63. $this->cache_time = $cache_time;
  64. }
  65. public function query($sql, $limit = null) {
  66. $sql = $this->getQuerySql($sql, $limit);
  67. $this->sql = $sql;
  68. $this->rs = $this->mysqli->query($sql);
  69. if (!$this->rs) {
  70. echo "<h2>".$this->mysqli->error."</h2>";
  71. die();
  72. } else {
  73. $this->query_num++;
  74. return $this->rs;
  75. }
  76. }
  77. public function getOne($sql) {
  78. $this->query($sql, 1);
  79. $this->fetch_mode = MYSQLI_NUM;
  80. $row = $this->fetch();
  81. $this->free();
  82. return $row[0];
  83. }
  84. public function get_one($sql) {
  85. return $this->getOne($sql);
  86. }
  87. public function cache_one($sql) {
  88. $sql = $this->getQuerySql($sql, 1);
  89. return $this->get_cache($sql, 'getOne');
  90. }
  91. public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
  92. $this->query($sql, 1);
  93. $this->fetch_mode = $fetch_mode;
  94. $row = $this->fetch();
  95. $this->free();
  96. return $row;
  97. }
  98. public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
  99. return $this->getRow($sql);
  100. }
  101. public function cache_row($sql) {
  102. $sql = $this->getQuerySql($sql, 1);
  103. return $this->get_cache($sql, 'getRow');
  104. }
  105. public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
  106. $this->query($sql, $limit);
  107. $all_rows = array();
  108. $this->fetch_mode = $fetch_mode;
  109. while($rows = $this->fetch()) {
  110. $all_rows[] = $rows;
  111. }
  112. $this->free();
  113. return $all_rows;
  114. }
  115. public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
  116. return $this->getAll($sql);
  117. }
  118. public function cache_all($sql, $limit = null) {
  119. $sql = $this->getQuerySql($sql, $limit);
  120. return $this->get_cache($sql, 'getAll');
  121. }
  122. public function insert_id() {
  123. return $this->mysqli->insert_id();
  124. }
  125. public function escape($str) {
  126. if(is_array($str)) {
  127. foreach($str as $key=>$val) {
  128. $str[$key] = $this->escape($val);
  129. }
  130. } else {
  131. $str = addslashes(trim($str));
  132. } //开源代码phpfensi.com
  133. return $str;
  134. }
  135. }
  136. //用法
  137. $db = new db_mysqli('localhost', 'root', 111222, 'dict');
  138. $db->set_cache_time(10);
  139. $db->set_cache_dir('./cache/sql/');
  140. $sql = "select * from words order by word_id limit 10,10";
  141. $res1 = $db->get_all($sql);
  142. $res2 = $db->cache_all($sql);
  143. echo $db->query_num(),'<br>';
  144. ?>