rich_list.auto_cache.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2016/10/26
  6. * Time: 17:45
  7. */
  8. class rich_list_auto_cache extends auto_cache
  9. {
  10. private $key = "rich_list:";
  11. public function load($param)
  12. {
  13. $this->key .= md5(serialize($param));
  14. $key_bf = $this->key . '_bf';
  15. $list = $GLOBALS['cache']->get($this->key, true);
  16. if ($list === false) {
  17. $is_ok = $GLOBALS['cache']->set_lock($this->key);
  18. if (!$is_ok) {
  19. $list = $GLOBALS['cache']->get($key_bf, true);
  20. } else {
  21. $m_config = load_auto_cache("m_config");//初始化手机端配置
  22. //缓存更新时间
  23. $rank_cache_time = intval($m_config['rank_cache_time']) > 0 ? intval($m_config['rank_cache_time']) : 300;
  24. //数据处理
  25. $live_list = $this->get_live();
  26. $data = $this->rich_ceil(isset($param['page_size']) ? $param['page_size'] : 10);
  27. $list = array(
  28. 'day' => $this->is_live($data['day'], $live_list),
  29. 'pre_day' => $this->is_live($data['pre_day'], $live_list),
  30. 'weeks' => $this->is_live($data['weeks'], $live_list),
  31. 'pre_weeks' => $this->is_live($data['pre_weeks'], $live_list),
  32. 'month' => $this->is_live($data['month'], $live_list),
  33. 'all' => $this->is_live($data['all'], $live_list),
  34. );
  35. //数据处理结束
  36. $GLOBALS['cache']->set($this->key, $list, $rank_cache_time, true);
  37. $GLOBALS['cache']->set($key_bf, $list, 86400, true);//备份
  38. }
  39. }
  40. if ($list == false) {
  41. $list = array();
  42. }
  43. return $list;
  44. }
  45. //给榜单中正在直播的用户添加直播链接
  46. public function is_live($data, $live_list)
  47. {
  48. foreach ($data as $k => $v) {
  49. foreach ($live_list as $kk => $vv) {
  50. if ($vv['user_id'] == $v['user_id']) {
  51. $data[$k]['live_in'] = $vv['live_in'];
  52. $data[$k]['room_id'] = $vv['room_id'];
  53. $data[$k]['watch_number'] = $vv['watch_number'];
  54. $data[$k]['title'] = $vv['title'];
  55. if($vv['live_in'] == 3){
  56. $data[$k]['video_url'] = get_video_url($vv['room_id'], $vv['live_in']);
  57. }else{
  58. $data[$k]['video_url'] = "/".intval($vv['user_id']);
  59. }
  60. $data[$k]['group_id'] = $vv['group_id'];
  61. $data[$k]['live_image'] = get_spec_image(empty($vv['live_image']) ? $vv['head_image'] : $vv['live_image']);
  62. $data[$k]['create_type'] = $vv['create_type'];
  63. $data[$k]['video_type'] = $vv['video_type'];
  64. }
  65. }
  66. if (empty($data[$k]['video_url'])) {
  67. $data[$k]['video_url'] = "/".intval($v['user_id']);
  68. }
  69. $data[$k]['user_level_ico'] = get_spec_image("./public/images/rank/rank_" . $v['user_level'] . ".png");
  70. }
  71. return $data;
  72. }
  73. //获取当前直播中的用户列表
  74. public function get_live()
  75. {
  76. $sql = "SELECT v.id AS room_id, v.sort_num, v.group_id, v.user_id, v.city, v.title, v.cate_id, v.live_in, v.video_type, v.room_type, v.create_type,
  77. (v.robot_num + v.virtual_watch_number + v.watch_number) as watch_number, v.live_image, v.head_image,v.thumb_head_image, v.xpoint,v.ypoint,
  78. u.v_type, u.v_icon, u.nick_name,u.user_level FROM " . DB_PREFIX . "video v
  79. LEFT JOIN " . DB_PREFIX . "user u ON u.id = v.user_id where v.live_in in (1,3) and v.room_type = 3 order by v.create_time,v.sort_num desc,v.sort desc";
  80. $live_list = $GLOBALS['db']->getAll($sql, true, true);
  81. return $live_list;
  82. }
  83. //财富榜数据
  84. public function rich_ceil($page_size = 10)
  85. {
  86. $pre_time = NOW_TIME - 60 * 10;
  87. $limit = "0,{$page_size}";
  88. $root['month'] = $this->month($limit);
  89. $root['day'] = $this->day($limit);
  90. $root['pre_day'] = $this->day($limit, $pre_time);
  91. $root['weeks'] = $this->weeks($limit);
  92. $root['pre_weeks'] = $this->weeks($limit, $pre_time);
  93. $sql = "select u.id as user_id ,u.nick_name,u.v_type,u.v_icon,u.head_image,u.sex,u.user_level,u.use_diamonds as ticket ,u.is_authentication
  94. from " . DB_PREFIX . "user as u where u.is_effect=1 and u.use_diamonds>0
  95. order BY u.use_diamonds desc limit " . $limit;
  96. $root['all'] = $GLOBALS['db']->getAll($sql);
  97. return $root;
  98. }
  99. private function day($limit, $pre_time = NOW_TIME)
  100. {
  101. return $this->get_data("create_d = day(curdate()) and is_red_envelope = 0", $limit, $pre_time);
  102. }
  103. private function weeks($limit, $pre_time = NOW_TIME)
  104. {
  105. return $this->get_data('create_w = WEEK(curdate())', $limit, $pre_time);
  106. }
  107. private function month($limit)
  108. {
  109. return $this->get_data("create_ym = " . to_date(NOW_TIME, 'Ym'), $limit, NOW_TIME);
  110. }
  111. private function get_data($where, $limit, $pre_time)
  112. {
  113. $table = createPropTable();
  114. $last_month_table = createPropTable(to_timespan("first day of last month"));
  115. $sql = "SELECT
  116. u.id AS user_id ,
  117. u.nick_name ,
  118. u.v_type ,
  119. u.v_icon ,
  120. u.head_image ,
  121. u.sex ,
  122. u.user_level ,
  123. u.is_authentication ,
  124. sum(v.total_diamonds) AS use_ticket
  125. FROM
  126. " . DB_PREFIX . "user AS u
  127. INNER JOIN(
  128. SELECT
  129. from_user_id ,
  130. total_diamonds
  131. FROM
  132. {$table}
  133. WHERE
  134. {$where}
  135. AND create_time < {$pre_time}
  136. UNION ALL
  137. SELECT
  138. from_user_id ,
  139. total_diamonds
  140. FROM
  141. {$last_month_table}
  142. WHERE
  143. {$where}
  144. AND create_time < {$pre_time}
  145. ) AS v ON u.id = v.from_user_id
  146. WHERE
  147. u.is_effect = 1
  148. GROUP BY
  149. v.from_user_id
  150. ORDER BY
  151. sum(v.total_diamonds) DESC
  152. LIMIT {$limit}";
  153. return $GLOBALS['db']->getAll($sql);
  154. }
  155. public function rm()
  156. {
  157. $GLOBALS['cache']->clear_by_name($this->key);
  158. }
  159. public function clear_all()
  160. {
  161. $GLOBALS['cache']->clear_by_name($this->key);
  162. }
  163. }
  164. ?>