cache.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package feed
  2. import (
  3. "context"
  4. "hash/crc32"
  5. "math/rand"
  6. "time"
  7. "go-common/app/interface/main/app-card/model/card/ai"
  8. "go-common/app/interface/main/app-intl/model"
  9. "go-common/library/log"
  10. )
  11. // indexCache is.
  12. func (s *Service) indexCache(c context.Context, mid int64, count int) (rs []*ai.Item, err error) {
  13. var (
  14. pos, nextPos int
  15. )
  16. cache := s.rcmdCache
  17. if len(cache) < count {
  18. return
  19. }
  20. if pos, err = s.rcmd.PositionCache(c, mid); err != nil {
  21. return
  22. }
  23. rs = make([]*ai.Item, 0, count)
  24. if pos < len(cache)-count-1 {
  25. nextPos = pos + count
  26. rs = append(rs, cache[pos:nextPos]...)
  27. } else if pos < len(cache)-1 {
  28. nextPos = count - (len(cache) - pos)
  29. rs = append(rs, cache[pos:]...)
  30. rs = append(rs, cache[:nextPos]...)
  31. } else {
  32. nextPos = count - 1
  33. rs = append(rs, cache[:nextPos]...)
  34. }
  35. s.addCache(func() {
  36. s.rcmd.AddPositionCache(context.Background(), mid, nextPos)
  37. })
  38. return
  39. }
  40. // recommendCache is.
  41. func (s *Service) recommendCache(count int) (rs []*ai.Item) {
  42. cache := s.rcmdCache
  43. index := len(cache)
  44. if count > 0 && count < index {
  45. index = count
  46. }
  47. rs = make([]*ai.Item, 0, index)
  48. for _, idx := range rand.Perm(len(cache))[:index] {
  49. rs = append(rs, cache[idx])
  50. }
  51. return
  52. }
  53. // group is.
  54. func (s *Service) group(mid int64, buvid string) (group int) {
  55. if mid == 0 && buvid == "" {
  56. group = -1
  57. return
  58. }
  59. if mid != 0 {
  60. if v, ok := s.groupCache[mid]; ok {
  61. group = v
  62. return
  63. }
  64. group = int(mid % 20)
  65. return
  66. }
  67. group = int(crc32.ChecksumIEEE([]byte(buvid)) % 20)
  68. return
  69. }
  70. // loadRcmdCache is.
  71. func (s *Service) loadRcmdCache() {
  72. is, err := s.rcmd.RcmdCache(context.Background())
  73. if err != nil {
  74. log.Error("%+v", err)
  75. }
  76. if len(is) >= 50 {
  77. for _, i := range is {
  78. i.Goto = model.GotoAv
  79. }
  80. s.rcmdCache = is
  81. return
  82. }
  83. aids, err := s.rcmd.Hots(context.Background())
  84. if err != nil {
  85. log.Error("%+v", err)
  86. }
  87. if len(aids) == 0 {
  88. if aids, err = s.rcmd.RcmdAidsCache(context.Background()); err != nil {
  89. return
  90. }
  91. }
  92. if len(aids) < 50 && len(s.rcmdCache) != 0 {
  93. return
  94. }
  95. s.rcmdCache = s.fromAids(aids)
  96. s.addCache(func() {
  97. s.rcmd.AddRcmdAidsCache(context.Background(), aids)
  98. })
  99. }
  100. // fromAids is.
  101. func (s *Service) fromAids(aids []int64) (is []*ai.Item) {
  102. is = make([]*ai.Item, 0, len(aids))
  103. for _, aid := range aids {
  104. i := &ai.Item{
  105. ID: aid,
  106. Goto: model.GotoAv,
  107. }
  108. is = append(is, i)
  109. }
  110. return
  111. }
  112. // rcmdproc is.
  113. func (s *Service) rcmdproc() {
  114. for {
  115. time.Sleep(s.tick)
  116. s.loadRcmdCache()
  117. }
  118. }
  119. // loadRankCache is.
  120. func (s *Service) loadRankCache() {
  121. rank, err := s.rank.AllRank(context.Background())
  122. if err != nil {
  123. log.Error("%+v", err)
  124. return
  125. }
  126. s.rankCache = rank
  127. }
  128. // rankproc is.
  129. func (s *Service) rankproc() {
  130. for {
  131. time.Sleep(s.tick)
  132. s.loadRankCache()
  133. }
  134. }
  135. // loadUpCardCache is.
  136. func (s *Service) loadUpCardCache() {
  137. follow, err := s.card.Follow(context.Background())
  138. if err != nil {
  139. log.Error("%+v", err)
  140. return
  141. }
  142. s.followCache = follow
  143. }
  144. // upCardproc is.
  145. func (s *Service) upCardproc() {
  146. for {
  147. time.Sleep(s.tick)
  148. s.loadUpCardCache()
  149. }
  150. }
  151. // loadGroupCache is.
  152. func (s *Service) loadGroupCache() {
  153. group, err := s.rcmd.Group(context.Background())
  154. if err != nil {
  155. log.Error("%+v", err)
  156. return
  157. }
  158. s.groupCache = group
  159. }
  160. // groupproc is.
  161. func (s *Service) groupproc() {
  162. for {
  163. time.Sleep(s.tick)
  164. s.loadGroupCache()
  165. }
  166. }