discuz_table.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_table.php 30321 2012-05-22 09:09:35Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_table extends discuz_base
  12. {
  13. public $data = array();
  14. public $methods = array();
  15. protected $_table;
  16. protected $_pk;
  17. protected $_pre_cache_key;
  18. protected $_cache_ttl;
  19. protected $_allowmem;
  20. public function __construct($para = array()) {
  21. if(!empty($para)) {
  22. $this->_table = $para['table'];
  23. $this->_pk = $para['pk'];
  24. }
  25. if(isset($this->_pre_cache_key) && (($ttl = getglobal('setting/memory/'.$this->_table)) !== null || ($ttl = $this->_cache_ttl) !== null) && memory('check')) {
  26. $this->_cache_ttl = $ttl;
  27. $this->_allowmem = true;
  28. }
  29. $this->_init_extend();
  30. parent::__construct();
  31. }
  32. public function getTable() {
  33. return $this->_table;
  34. }
  35. public function setTable($name) {
  36. return $this->_table = $name;
  37. }
  38. public function count() {
  39. $count = (int) DB::result_first("SELECT count(*) FROM ".DB::table($this->_table));
  40. return $count;
  41. }
  42. public function update($val, $data, $unbuffered = false, $low_priority = false) {
  43. if(isset($val) && !empty($data) && is_array($data)) {
  44. $this->checkpk();
  45. $ret = DB::update($this->_table, $data, DB::field($this->_pk, $val), $unbuffered, $low_priority);
  46. foreach((array)$val as $id) {
  47. $this->update_cache($id, $data);
  48. }
  49. return $ret;
  50. }
  51. return !$unbuffered ? 0 : false;
  52. }
  53. public function delete($val, $unbuffered = false) {
  54. $ret = false;
  55. if(isset($val)) {
  56. $this->checkpk();
  57. $ret = DB::delete($this->_table, DB::field($this->_pk, $val), null, $unbuffered);
  58. $this->clear_cache($val);
  59. }
  60. return $ret;
  61. }
  62. public function truncate() {
  63. DB::query("TRUNCATE ".DB::table($this->_table));
  64. }
  65. public function insert($data, $return_insert_id = false, $replace = false, $silent = false) {
  66. return DB::insert($this->_table, $data, $return_insert_id, $replace, $silent);
  67. }
  68. public function checkpk() {
  69. if(!$this->_pk) {
  70. throw new DbException('Table '.$this->_table.' has not PRIMARY KEY defined');
  71. }
  72. }
  73. public function fetch($id, $force_from_db = false){
  74. $data = array();
  75. if(!empty($id)) {
  76. if($force_from_db || ($data = $this->fetch_cache($id)) === false) {
  77. $data = DB::fetch_first('SELECT * FROM '.DB::table($this->_table).' WHERE '.DB::field($this->_pk, $id));
  78. if(!empty($data)) $this->store_cache($id, $data);
  79. }
  80. }
  81. return $data;
  82. }
  83. public function fetch_all($ids, $force_from_db = false) {
  84. $data = array();
  85. if(!empty($ids)) {
  86. if($force_from_db || ($data = $this->fetch_cache($ids)) === false || count($ids) != count($data)) {
  87. if(is_array($data) && !empty($data)) {
  88. $ids = array_diff($ids, array_keys($data));
  89. }
  90. if($data === false) $data =array();
  91. if(!empty($ids)) {
  92. $query = DB::query('SELECT * FROM '.DB::table($this->_table).' WHERE '.DB::field($this->_pk, $ids));
  93. while($value = DB::fetch($query)) {
  94. $data[$value[$this->_pk]] = $value;
  95. $this->store_cache($value[$this->_pk], $value);
  96. }
  97. }
  98. }
  99. }
  100. return $data;
  101. }
  102. public function fetch_all_field(){
  103. $data = false;
  104. $query = DB::query('SHOW FIELDS FROM '.DB::table($this->_table), '', 'SILENT');
  105. if($query) {
  106. $data = array();
  107. while($value = DB::fetch($query)) {
  108. $data[$value['Field']] = $value;
  109. }
  110. }
  111. return $data;
  112. }
  113. public function range($start = 0, $limit = 0, $sort = '') {
  114. if($sort) {
  115. $this->checkpk();
  116. }
  117. return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).($sort ? ' ORDER BY '.DB::order($this->_pk, $sort) : '').DB::limit($start, $limit), null, $this->_pk ? $this->_pk : '');
  118. }
  119. public function optimize() {
  120. DB::query('OPTIMIZE TABLE '.DB::table($this->_table), 'SILENT');
  121. }
  122. public function fetch_cache($ids, $pre_cache_key = null) {
  123. $data = false;
  124. if($this->_allowmem) {
  125. if($pre_cache_key === null) $pre_cache_key = $this->_pre_cache_key;
  126. $data = memory('get', $ids, $pre_cache_key);
  127. }
  128. return $data;
  129. }
  130. public function store_cache($id, $data, $cache_ttl = null, $pre_cache_key = null) {
  131. $ret = false;
  132. if($this->_allowmem) {
  133. if($pre_cache_key === null) $pre_cache_key = $this->_pre_cache_key;
  134. if($cache_ttl === null) $cache_ttl = $this->_cache_ttl;
  135. $ret = memory('set', $id, $data, $cache_ttl, $pre_cache_key);
  136. }
  137. return $ret;
  138. }
  139. public function clear_cache($ids, $pre_cache_key = null) {
  140. $ret = false;
  141. if($this->_allowmem) {
  142. if($pre_cache_key === null) $pre_cache_key = $this->_pre_cache_key;
  143. $ret = memory('rm', $ids, $pre_cache_key);
  144. }
  145. return $ret;
  146. }
  147. public function update_cache($id, $data, $cache_ttl = null, $pre_cache_key = null) {
  148. $ret = false;
  149. if($this->_allowmem) {
  150. if($pre_cache_key === null) $pre_cache_key = $this->_pre_cache_key;
  151. if($cache_ttl === null) $cache_ttl = $this->_cache_ttl;
  152. if(($_data = memory('get', $id, $pre_cache_key)) !== false) {
  153. $ret = $this->store_cache($id, array_merge($_data, $data), $cache_ttl, $pre_cache_key);
  154. }
  155. }
  156. return $ret;
  157. }
  158. public function update_batch_cache($ids, $data, $cache_ttl = null, $pre_cache_key = null) {
  159. $ret = false;
  160. if($this->_allowmem) {
  161. if($pre_cache_key === null) $pre_cache_key = $this->_pre_cache_key;
  162. if($cache_ttl === null) $cache_ttl = $this->_cache_ttl;
  163. if(($_data = memory('get', $ids, $pre_cache_key)) !== false) {
  164. foreach($_data as $id => $value) {
  165. $ret = $this->store_cache($id, array_merge($value, $data), $cache_ttl, $pre_cache_key);
  166. }
  167. }
  168. }
  169. return $ret;
  170. }
  171. public function reset_cache($ids, $pre_cache_key = null) {
  172. $ret = false;
  173. if($this->_allowmem) {
  174. $keys = array();
  175. if(($cache_data = $this->fetch_cache($ids, $pre_cache_key)) !== false) {
  176. $keys = array_intersect(array_keys($cache_data), $ids);
  177. unset($cache_data);
  178. }
  179. if(!empty($keys)) {
  180. $this->fetch_all($keys, true);
  181. $ret = true;
  182. }
  183. }
  184. return $ret;
  185. }
  186. public function increase_cache($ids, $data, $cache_ttl = null, $pre_cache_key = null) {
  187. if($this->_allowmem) {
  188. if(($cache_data = $this->fetch_cache($ids, $pre_cache_key)) !== false) {
  189. foreach($cache_data as $id => $one) {
  190. foreach($data as $key => $value) {
  191. if(is_array($value)) {
  192. $one[$key] = $value[0];
  193. } else {
  194. $one[$key] = $one[$key] + ($value);
  195. }
  196. }
  197. $this->store_cache($id, $one, $cache_ttl, $pre_cache_key);
  198. }
  199. }
  200. }
  201. }
  202. public function __toString() {
  203. return $this->_table;
  204. }
  205. protected function _init_extend() {
  206. }
  207. public function attach_before_method($name, $fn) {
  208. $this->methods[$name][0][] = $fn;
  209. }
  210. public function attach_after_method($name, $fn) {
  211. $this->methods[$name][1][] = $fn;
  212. }
  213. }
  214. ?>