discuz_table.php 6.5 KB

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