recdata.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/service/live/xroom-feed/internal/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. var (
  12. recConfKey = "rec_conf"
  13. recPoolKey = "rec_pool_%d"
  14. recRoomInfo = "rec_info_%d"
  15. )
  16. //GetCacheData 获取缓存数据
  17. func (d *Dao) GetCacheData() (data []byte) {
  18. conn := d.redis.Get(context.Background())
  19. defer conn.Close()
  20. var err error
  21. if data, err = redis.Bytes(conn.Do("GET", recConfKey)); err != nil {
  22. log.Error("[recdata cache] GetCacheData err:%+v", err)
  23. return make([]byte, 0)
  24. }
  25. return data
  26. }
  27. //GetRecInfoByRoomid 批量通过Roomid获取rec_info
  28. func (d *Dao) GetRecInfoByRoomid(ctx context.Context, roomids []int64) map[int64]*model.RecRoomInfo {
  29. conn := d.redis.Get(ctx)
  30. defer conn.Close()
  31. redisSuccess := make([]int64, 0, len(roomids))
  32. for _, roomid := range roomids {
  33. key := fmt.Sprintf(recRoomInfo, roomid)
  34. if err := conn.Send("HGETALL", key); err != nil {
  35. log.Error("[recdata] GetRecInfoByRoomid redis send roomid:%d err:%v", roomid, err)
  36. continue
  37. }
  38. redisSuccess = append(redisSuccess, roomid)
  39. }
  40. conn.Flush()
  41. result := make(map[int64]*model.RecRoomInfo)
  42. for _, roomid := range redisSuccess {
  43. var roominfo map[string]string
  44. var err error
  45. if roominfo, err = redis.StringMap(conn.Receive()); err != nil {
  46. log.Error("[recdata] GetRecInfoByRoomid redis receive err:%d err:%+v", roomid, err)
  47. continue
  48. }
  49. if len(roominfo) <= 0 {
  50. log.Error("[recdata] GetRecInfoByRoomid redis receive empty:%d err:+%v", roomid, roominfo)
  51. continue
  52. }
  53. result[roomid] = model.NewRecRoomInfo()
  54. result[roomid].Title = roominfo["title"]
  55. result[roomid].Uid, _ = strconv.ParseInt(roominfo["uid"], 10, 64)
  56. result[roomid].PopularityCount, _ = strconv.ParseInt(roominfo["popularity_count"], 10, 64)
  57. result[roomid].KeyFrame = roominfo["Keyframe"]
  58. result[roomid].Cover = roominfo["cover"]
  59. result[roomid].ParentAreaID, _ = strconv.ParseInt(roominfo["parent_area_id"], 10, 64)
  60. result[roomid].ParentAreaName = roominfo["parent_area_name"]
  61. result[roomid].AreaID, _ = strconv.ParseInt(roominfo["area_id"], 10, 64)
  62. result[roomid].AreaName = roominfo["area_name"]
  63. }
  64. return result
  65. }
  66. //GetRecPoolByID 通过id获取推荐池
  67. func (d *Dao) GetRecPoolByID(ctx context.Context, rids []int64) map[int64][]int64 {
  68. conn := d.redis.Get(ctx)
  69. defer conn.Close()
  70. var (
  71. room string
  72. err error
  73. )
  74. redisSuccess := make([]int64, 0, len(rids))
  75. for _, rid := range rids {
  76. key := fmt.Sprintf(recPoolKey, rid)
  77. if err := conn.Send("GET", key); err != nil {
  78. log.Error("[recdata] GetRecPoolByID send id %d err:%+v", rid, err)
  79. continue
  80. }
  81. redisSuccess = append(redisSuccess, rid)
  82. }
  83. conn.Flush()
  84. result := make(map[int64][]int64)
  85. for _, rid := range redisSuccess {
  86. if room, err = redis.String(conn.Receive()); err != nil {
  87. log.Error("[recdata] GetRecPoolByID receive id %d err:%+v", rid, err)
  88. continue
  89. }
  90. if room == "" {
  91. log.Info("[recdata] GetRecPoolByID receive empty room list rid: %d", rid)
  92. continue
  93. }
  94. rooms := strings.Split(room, ",")
  95. roomIDs := make([]int64, len(rooms), len(rooms))
  96. for i, roomid := range rooms {
  97. roomIDs[i], _ = strconv.ParseInt(roomid, 10, 64)
  98. }
  99. result[rid] = roomIDs
  100. }
  101. return result
  102. }