UserViewHistoryRedisService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class UserViewHistoryRedisService extends BaseRedisService
  3. {
  4. private $user_view_history_db;
  5. private $id;
  6. /**
  7. * +----------------------------------------------------------
  8. * 架构函数
  9. * +----------------------------------------------------------
  10. * @access public
  11. * +----------------------------------------------------------
  12. */
  13. public function __construct($user_id)
  14. {
  15. parent::__construct();
  16. $this->user_view_history_db = $this->prefix . 'user_view_history:';
  17. $this->id = $user_id;
  18. }
  19. public function view($room_id)
  20. {
  21. $this->redis->zAdd($this->user_view_history_db . $this->id, time(), $room_id);
  22. }
  23. public function remove($room_id)
  24. {
  25. $this->redis->zRem($this->user_view_history_db . $this->id, $room_id);
  26. }
  27. public function get_history($page = 1, $page_size = 20)
  28. {
  29. if($page < 1){
  30. $page = 1;
  31. }
  32. $start = ($page - 1) * $page_size;
  33. // $this->redis->delete($this->user_view_history_db . $this->id);
  34. $list = $this->redis->zRevRange($this->user_view_history_db . $this->id, $start, $start + $page_size - 1);
  35. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoRedisService.php');
  36. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  37. $video_redis = new VideoRedisService();
  38. $user_redis = new UserRedisService();
  39. $video_list = array();
  40. foreach ($list as $room_id) {
  41. $fields = array('room_id', 'user_id', 'live_in', 'live_image', 'head_image', 'title');
  42. $video = $video_redis->getRow_db($room_id,$fields);
  43. if(empty($video['live_image'])) {
  44. $video['live_image'] =get_spec_image($video['head_image'],320,180,1);
  45. }else{
  46. $video['live_image'] =get_spec_image($video['live_image'],320,180,1);
  47. }
  48. if($video['room_id'] === false){
  49. $video['room_id'] = $room_id;
  50. }
  51. $video['watch_number'] = $video_redis->get_video_watch_num($room_id);
  52. $video['video_url'] = get_video_url($room_id, $video['live_in']);
  53. $user = $user_redis->getRow_db($video['user_id'], array('nick_name', 'head_image'));
  54. $video['nick_name'] = $user['nick_name'];
  55. $video['thumb_head_image'] = $user['head_image'];
  56. if($video['user_id']===false){
  57. $this->remove($room_id);
  58. }
  59. $video_list[] = $video;
  60. }
  61. return $video_list;
  62. }
  63. public function count()
  64. {
  65. return intval($this->redis->zCard($this->user_view_history_db . $this->id));
  66. }
  67. public function zfollowing()
  68. {
  69. return $this->redis->zRange($this->user_view_history_db . $this->id, 0, -1);
  70. }
  71. }