newlist.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/conf"
  5. "go-common/app/interface/main/web/model"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. const _allRank = 0
  13. // NewList get new list by region id.
  14. func (s *Service) NewList(c context.Context, rid int32, tp int8, pn, ps int) (arcs []*api.Arc, count int, err error) {
  15. var (
  16. addCache = true
  17. first bool
  18. allArcs, moreArcs []*api.Arc
  19. start, end, max int
  20. ip = metadata.String(c, metadata.RemoteIP)
  21. )
  22. // check first or second region id.
  23. if _, ok := s.rids[rid]; ok {
  24. first = true
  25. }
  26. if first {
  27. max = s.c.Rule.MaxFirstCacheSize
  28. end = max
  29. } else {
  30. max = s.c.Rule.MaxSecondCacheSize
  31. start = (pn - 1) * ps
  32. end = start + ps - 1
  33. if start < 0 || end < 0 {
  34. err = ecode.RequestErr
  35. return
  36. }
  37. }
  38. // get from local cache.
  39. if arcs, count, err = s.dao.NewListCache(c, rid, tp, start, end); err != nil {
  40. err = nil
  41. addCache = false
  42. } else if len(arcs) > 0 {
  43. return
  44. }
  45. // get from archive-service
  46. if first {
  47. if allArcs, count, err = s.rankTopArcs(c, rid, tp, 1, max, ip); err != nil {
  48. err = nil
  49. } else if tid, ok := model.NewListRid[rid]; ok && len(allArcs) < conf.Conf.Rule.MinNewListCnt {
  50. if moreArcs, _, err = s.rankArcs(c, tid, tp, _samplePn, conf.Conf.Rule.MinNewListCnt, ip); err != nil {
  51. err = nil
  52. } else {
  53. allAidMap := make(map[int64]int64, len(allArcs))
  54. for _, arc := range allArcs {
  55. allAidMap[arc.Aid] = arc.Aid
  56. }
  57. for _, v := range moreArcs {
  58. if _, ok := allAidMap[v.Aid]; ok {
  59. continue
  60. }
  61. allArcs = append(allArcs, v)
  62. }
  63. log.Info("NewList more arcs rid(%d) tid(%d) len allArcs(%d) len moreArcs(%d)", tid, rid, len(allArcs), len(moreArcs))
  64. }
  65. }
  66. } else {
  67. if start >= max {
  68. if arcs, count, err = s.rankArcs(c, rid, tp, pn, ps, ip); err != nil {
  69. return
  70. }
  71. fmtArcs3(arcs)
  72. return
  73. }
  74. if allArcs, count, err = s.rankArcs(c, rid, tp, 1, max, ip); err != nil {
  75. err = nil
  76. }
  77. }
  78. if len(allArcs) > 0 {
  79. fmtArcs3(allArcs)
  80. length := len(allArcs)
  81. if length < start {
  82. arcs = []*api.Arc{}
  83. return
  84. }
  85. if length > end {
  86. arcs = allArcs[start : end+1]
  87. } else {
  88. arcs = allArcs[start:]
  89. }
  90. if addCache {
  91. s.cache.Do(c, func(c context.Context) {
  92. s.dao.SetNewListCache(c, rid, tp, allArcs, count)
  93. })
  94. }
  95. return
  96. }
  97. // get from remote cache.
  98. arcs, count, err = s.dao.NewListBakCache(c, rid, tp, start, end)
  99. if len(arcs) == 0 {
  100. arcs = []*api.Arc{}
  101. }
  102. return
  103. }
  104. func (s *Service) rankArcs(c context.Context, rid int32, tp int8, pn, ps int, ip string) (arcs []*api.Arc, count int, err error) {
  105. var res = &archive.RankArchives3{}
  106. if rid == _allRank {
  107. if res, err = s.arc.RankAllArcs3(c, &archive.ArgRankAll2{Pn: pn, Ps: ps}); err != nil {
  108. log.Error("arcrpc.RankAllArcs2(%d,%d) error(%v)", pn, ps, err)
  109. return
  110. }
  111. } else {
  112. arg := &archive.ArgRank2{Rid: int16(rid), Type: tp, Pn: pn, Ps: ps, RealIP: ip}
  113. if res, err = s.arc.RankArcs3(c, arg); err != nil {
  114. log.Error("arcrpc.RankArcs2(%d,%d,%d,%s) error(%v)", rid, pn, ps, ip, err)
  115. return
  116. }
  117. }
  118. arcs = res.Archives
  119. count = res.Count
  120. return
  121. }
  122. func (s *Service) rankTopArcs(c context.Context, rid int32, tp int8, pn, ps int, ip string) (arcs []*api.Arc, count int, err error) {
  123. arg := &archive.ArgRankTop2{ReID: int16(rid), Pn: pn, Ps: ps}
  124. if arcs, err = s.arc.RankTopArcs3(c, arg); err != nil {
  125. log.Error("arcrpc.RankTopArcs3(%d,%d,%d,%s) error(%v)", rid, pn, ps, ip, err)
  126. }
  127. return
  128. }