reccache.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. daoAnchorV1 "go-common/app/service/live/dao-anchor/api/grpc/v1"
  6. "go-common/library/log"
  7. "go-common/library/sync/errgroup"
  8. )
  9. const (
  10. _video = 33
  11. _music = 34
  12. )
  13. var _fields = []string{
  14. "room_id",
  15. "uid",
  16. "title",
  17. "popularity_count",
  18. "keyframe",
  19. "cover",
  20. "parent_area_id",
  21. "parent_area_name",
  22. "area_id",
  23. "area_name",
  24. }
  25. func (s *Service) setRecInfoCache(ctx context.Context, roomIds []int64) (filterIds []int64) {
  26. filterIds = make([]int64, 0)
  27. chunkSize := 100
  28. // 批次
  29. chunkNum := int(math.Ceil(float64(len(roomIds)) / float64(chunkSize)))
  30. chunkIds := make([][]int64, chunkNum)
  31. wg := errgroup.Group{}
  32. for i := 1; i <= chunkNum; i++ {
  33. x := i
  34. wg.Go(func() error {
  35. chunkRoomIds := make([]int64, 10)
  36. if x == chunkNum {
  37. chunkRoomIds = roomIds[(x-1)*chunkSize:]
  38. } else {
  39. chunkRoomIds = roomIds[(x-1)*chunkSize : x*chunkSize]
  40. }
  41. resp, err := s.daoAnchor.FetchRoomByIDs(ctx, &daoAnchorV1.RoomByIDsReq{
  42. RoomIds: chunkRoomIds,
  43. Fields: _fields,
  44. })
  45. if err != nil {
  46. log.Error("[setRecInfoCache]FetchRoomByIDs_error:%+v", err)
  47. return nil
  48. }
  49. if resp == nil || len(resp.RoomDataSet) == 0 {
  50. log.Info("[setRecInfoCache]FetchRoomByIDs_empty")
  51. return nil
  52. }
  53. filterRoomData := make(map[int64]*daoAnchorV1.RoomData)
  54. for roomId, data := range resp.RoomDataSet {
  55. if data == nil {
  56. continue
  57. }
  58. if data.Cover == "" && data.Keyframe == "" {
  59. log.Info("[setRecInfoCache]emptyCoverOrKeyFrame, roomId:%d ,cover:%s, keyframe:%s", data.RoomId, data.Cover, data.Keyframe)
  60. continue
  61. }
  62. if data.AreaName == "" || data.ParentAreaName == "" {
  63. log.Info("[setRecInfoCache]emptyAreaName, roomId:%d ,areaName:%s, parentAreaName:%s", data.RoomId, data.AreaName, data.ParentAreaName)
  64. continue
  65. }
  66. if data.AreaId == _video || data.AreaId == _music {
  67. log.Info("[setRecInfoCache]musicOrVideoArea, roomId:%d ,area:%d", data.RoomId, data.AreaId)
  68. continue
  69. }
  70. if s.isBlackRoomID(data.RoomId) {
  71. log.Info("[setRecInfoCache]is IndexBlackRoomID, roomId:%d ", data.RoomId)
  72. continue
  73. }
  74. chunkIds[x-1] = append(chunkIds[x-1], roomId)
  75. filterRoomData[roomId] = data
  76. }
  77. s.dao.SetRecInfoCache(ctx, filterRoomData)
  78. return nil
  79. })
  80. }
  81. err := wg.Wait()
  82. if err != nil {
  83. log.Error("[setRecInfoCache]waitError:%+v", err)
  84. }
  85. for _, ids := range chunkIds {
  86. for _, id := range ids {
  87. filterIds = append(filterIds, id)
  88. }
  89. }
  90. return
  91. }