cache.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-card/model/card/live"
  9. "go-common/app/interface/main/app-feed/model"
  10. "go-common/app/service/main/archive/model/archive"
  11. "go-common/library/log"
  12. )
  13. func (s *Service) indexCache(c context.Context, mid int64, count int) (rs []*ai.Item, err error) {
  14. var (
  15. pos, nextPos int
  16. )
  17. cache := s.rcmdCache
  18. if len(cache) < count {
  19. return
  20. }
  21. if pos, err = s.rcmd.PositionCache(c, mid); err != nil {
  22. return
  23. }
  24. rs = make([]*ai.Item, 0, count)
  25. if pos < len(cache)-count-1 {
  26. nextPos = pos + count
  27. rs = append(rs, cache[pos:nextPos]...)
  28. } else if pos < len(cache)-1 {
  29. nextPos = count - (len(cache) - pos)
  30. rs = append(rs, cache[pos:]...)
  31. rs = append(rs, cache[:nextPos]...)
  32. } else {
  33. nextPos = count - 1
  34. rs = append(rs, cache[:nextPos]...)
  35. }
  36. s.addCache(func() {
  37. s.rcmd.AddPositionCache(context.Background(), mid, nextPos)
  38. })
  39. return
  40. }
  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. func (s *Service) group(mid int64, buvid string) (group int) {
  54. if mid == 0 && buvid == "" {
  55. group = -1
  56. return
  57. }
  58. if mid != 0 {
  59. if v, ok := s.groupCache[mid]; ok {
  60. group = v
  61. return
  62. }
  63. group = int(mid % 20)
  64. return
  65. }
  66. group = int(crc32.ChecksumIEEE([]byte(buvid)) % 20)
  67. return
  68. }
  69. func (s *Service) loadRcmdCache() {
  70. is, err := s.rcmd.RcmdCache(context.Background())
  71. if err != nil {
  72. log.Error("%+v", err)
  73. }
  74. if len(is) >= 50 {
  75. for _, i := range is {
  76. i.Goto = model.GotoAv
  77. }
  78. s.rcmdCache = is
  79. return
  80. }
  81. aids, err := s.rcmd.Hots(context.Background())
  82. if err != nil {
  83. log.Error("%+v", err)
  84. }
  85. if len(aids) == 0 {
  86. if aids, err = s.rcmd.RcmdAidsCache(context.Background()); err != nil {
  87. log.Error("%+v", err)
  88. return
  89. }
  90. }
  91. if len(aids) < 50 && len(s.rcmdCache) != 0 {
  92. return
  93. }
  94. s.addCache(func() {
  95. s.rcmd.AddRcmdAidsCache(context.Background(), aids)
  96. })
  97. if is, err = s.fromArchvies(aids); err != nil {
  98. log.Error("%+v", err)
  99. return
  100. }
  101. s.rcmdCache = is
  102. }
  103. func (s *Service) UpRcmdCache(c context.Context, is []*ai.Item) (err error) {
  104. if err = s.rcmd.AddRcmdCache(c, is); err != nil {
  105. log.Error("%+v", err)
  106. }
  107. return
  108. }
  109. func (s *Service) fromArchvies(aids []int64) (is []*ai.Item, err error) {
  110. var as map[int64]*archive.ArchiveWithPlayer
  111. if as, err = s.arc.ArchivesWithPlayer(context.Background(), aids, 0, "", 0, 0, 0, 0); err != nil {
  112. return
  113. }
  114. is = make([]*ai.Item, 0, len(aids))
  115. for _, aid := range aids {
  116. a, ok := as[aid]
  117. if !ok || a.Archive3 == nil || !a.IsNormal() {
  118. continue
  119. }
  120. is = append(is, &ai.Item{ID: aid, Goto: model.GotoAv, Archive: a.Archive3})
  121. }
  122. return
  123. }
  124. func (s *Service) rcmdproc() {
  125. for {
  126. time.Sleep(s.tick)
  127. s.loadRcmdCache()
  128. }
  129. }
  130. func (s *Service) loadRankCache() {
  131. rank, err := s.rank.AllRank(context.Background())
  132. if err != nil {
  133. log.Error("%+v", err)
  134. return
  135. }
  136. s.rankCache = rank
  137. }
  138. func (s *Service) rankproc() {
  139. for {
  140. time.Sleep(s.tick)
  141. s.loadRankCache()
  142. }
  143. }
  144. func (s *Service) loadConvergeCache() {
  145. converge, err := s.cvg.Cards(context.Background())
  146. if err != nil {
  147. log.Error("%+v", err)
  148. return
  149. }
  150. s.convergeCache = converge
  151. }
  152. func (s *Service) convergeproc() {
  153. for {
  154. time.Sleep(s.tick)
  155. s.loadConvergeCache()
  156. }
  157. }
  158. func (s *Service) loadDownloadCache() {
  159. download, err := s.gm.DownLoad(context.Background())
  160. if err != nil {
  161. log.Error("%+v", err)
  162. return
  163. }
  164. s.downloadCache = download
  165. }
  166. func (s *Service) downloadproc() {
  167. for {
  168. time.Sleep(s.tick)
  169. s.loadDownloadCache()
  170. }
  171. }
  172. func (s *Service) loadSpecialCache() {
  173. special, err := s.sp.Card(context.Background(), time.Now())
  174. if err != nil {
  175. log.Error("%+v", err)
  176. return
  177. }
  178. var roomIDs []int64
  179. idm := map[int64]int64{}
  180. for _, sp := range special {
  181. if sp.Goto == model.GotoLive && sp.Pid != 0 {
  182. roomIDs = append(roomIDs, sp.Pid)
  183. idm[sp.Pid] = sp.ID
  184. }
  185. }
  186. room, err := s.lv.Rooms(context.Background(), roomIDs, "")
  187. if err != nil {
  188. log.Error("%+v", err)
  189. }
  190. if len(room) != 0 {
  191. for rid, id := range idm {
  192. if r, ok := room[rid]; !ok || r.LiveStatus != 1 {
  193. delete(special, id)
  194. }
  195. }
  196. }
  197. s.specialCache = special
  198. }
  199. func (s *Service) specialproc() {
  200. for {
  201. time.Sleep(s.tick)
  202. s.loadSpecialCache()
  203. }
  204. }
  205. func (s *Service) loadGroupCache() {
  206. group, err := s.rcmd.Group(context.Background())
  207. if err != nil {
  208. log.Error("%+v", err)
  209. return
  210. }
  211. s.groupCache = group
  212. }
  213. func (s *Service) groupproc() {
  214. for {
  215. time.Sleep(s.tick)
  216. s.loadGroupCache()
  217. }
  218. }
  219. func (s *Service) loadFollowModeList() {
  220. list, err := s.rcmd.FollowModeList(context.Background())
  221. if err != nil {
  222. log.Error("%+v", err)
  223. if list, err = s.rcmd.FollowModeListCache(context.Background()); err != nil {
  224. log.Error("%+v", err)
  225. return
  226. }
  227. } else {
  228. s.addCache(func() {
  229. s.rcmd.AddFollowModeListCache(context.Background(), list)
  230. })
  231. }
  232. log.Warn("loadFollowModeList list len(%d)", len(list))
  233. s.followModeList = list
  234. }
  235. func (s *Service) followModeListproc() {
  236. for {
  237. time.Sleep(s.tick)
  238. s.loadFollowModeList()
  239. }
  240. }
  241. func (s *Service) loadUpCardCache() {
  242. follow, err := s.card.Follow(context.Background())
  243. if err != nil {
  244. log.Error("%+v", err)
  245. return
  246. }
  247. s.followCache = follow
  248. }
  249. func (s *Service) upCardproc() {
  250. for {
  251. time.Sleep(s.tick)
  252. s.loadUpCardCache()
  253. }
  254. }
  255. func (s *Service) loadLiveCardCache() {
  256. liveCard, err := s.lv.Card(context.Background())
  257. if err != nil {
  258. log.Error("%+v", err)
  259. return
  260. }
  261. s.liveCardCache = liveCard
  262. }
  263. func (s *Service) liveUpRcmdCard(c context.Context, ids ...int64) (cardm map[int64][]*live.Card, upIDs []int64) {
  264. if len(ids) == 0 {
  265. return
  266. }
  267. cardm = make(map[int64][]*live.Card, len(ids))
  268. for _, id := range ids {
  269. if card, ok := s.liveCardCache[id]; ok {
  270. cardm[id] = card
  271. for _, c := range card {
  272. if c.UID != 0 {
  273. upIDs = append(upIDs, c.UID)
  274. }
  275. }
  276. }
  277. }
  278. return
  279. }
  280. func (s *Service) liveCardproc() {
  281. for {
  282. time.Sleep(1 * time.Second)
  283. s.loadLiveCardCache()
  284. }
  285. }
  286. func (s *Service) loadABTestCache() {
  287. res, err := s.rsc.AbTest(context.Background(), _feedgroups)
  288. if err != nil {
  289. log.Error("resource s.rsc.AbTest error(%v)", err)
  290. return
  291. }
  292. s.abtestCache = res
  293. log.Info("loadAbTestCache cache success")
  294. }
  295. func (s *Service) loadABTestCacheProc() {
  296. for {
  297. time.Sleep(s.tick)
  298. s.loadABTestCache()
  299. }
  300. }
  301. func (s *Service) loadAutoPlayMid() {
  302. tmp := map[int64]struct{}{}
  303. for _, mid := range s.c.AutoPlayMids {
  304. tmp[mid] = struct{}{}
  305. }
  306. s.autoplayMidsCache = tmp
  307. }