charm_podcast.auto_cache.php 7.0 KB

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