ugc_load.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package cms
  2. import (
  3. "context"
  4. "go-common/app/interface/main/tv/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. // LoadArcsMediaMap loads a batch of arc meta
  9. func (d *Dao) LoadArcsMediaMap(ctx context.Context, aids []int64) (resMetas map[int64]*model.ArcCMS, err error) {
  10. var (
  11. cachedMetas map[int64]*model.ArcCMS // cache hit seasons
  12. missedMetas map[int64]*model.ArcCMS // cache miss seasons, pick from DB
  13. missed []int64 // cache miss seasons
  14. addCache = true // whether we need to fill DB data in MC
  15. )
  16. resMetas = make(map[int64]*model.ArcCMS) // merge info from MC and from DB
  17. // pick up the information for these season ids
  18. if cachedMetas, missed, err = d.ArcsMetaCache(ctx, aids); err != nil {
  19. log.Error("LoadArcsMedia ArcsMetaCache Aids:%v, Error:%v", aids, err)
  20. err = nil
  21. addCache = false // mc error, we don't add
  22. }
  23. if len(missed) > 0 {
  24. if missedMetas, err = d.ArcMetas(ctx, missed); err != nil {
  25. log.Error("LoadArcsMedia ArcMetas Sids:%v, Error:%v", missed, err)
  26. return
  27. }
  28. }
  29. // merge info from DB and the info from MC
  30. for sid, v := range cachedMetas {
  31. resMetas[sid] = v
  32. }
  33. for sid, v := range missedMetas {
  34. resMetas[sid] = v
  35. }
  36. // async Reset the DB data in MC for next time
  37. log.Info("Set Sids [%d], MissedMetas [%d] Data in MC", len(aids), len(missedMetas))
  38. if addCache && len(missedMetas) > 0 {
  39. for _, art := range missedMetas {
  40. d.AddArcMetaCache(art)
  41. }
  42. }
  43. return
  44. }
  45. // LoadVideosMeta picks the videos meta info
  46. func (d *Dao) LoadVideosMeta(ctx context.Context, cids []int64) (resMetas map[int64]*model.VideoCMS, err error) {
  47. var (
  48. cachedMetas map[int64]*model.VideoCMS // cache hit seasons
  49. missedMetas map[int64]*model.VideoCMS // cache miss seasons, pick from DB
  50. missed []int64 // cache miss seasons
  51. addCache = true // whether we need to fill DB data in MC
  52. )
  53. resMetas = make(map[int64]*model.VideoCMS) // merge info from MC and from DB
  54. // pick up the information for these season ids
  55. if cachedMetas, missed, err = d.VideosMetaCache(ctx, cids); err != nil {
  56. log.Error("LoadVideosMeta VideosMetaCache Aids:%v, Error:%v", cids, err)
  57. err = nil
  58. addCache = false // mc error, we don't add
  59. }
  60. if len(missed) > 0 {
  61. if missedMetas, err = d.VideoMetas(ctx, missed); err != nil {
  62. log.Error("LoadVideosMeta VideoMetas Sids:%v, Error:%v", missed, err)
  63. return
  64. }
  65. }
  66. // merge info from DB and the info from MC
  67. for sid, v := range cachedMetas {
  68. resMetas[sid] = v
  69. }
  70. for sid, v := range missedMetas {
  71. resMetas[sid] = v
  72. }
  73. // async Reset the DB data in MC for next time
  74. log.Info("Set Sids [%d], MissedMetas [%d] Data in MC", len(cids), len(missedMetas))
  75. if addCache && len(missedMetas) > 0 {
  76. for _, art := range missedMetas {
  77. d.AddVideoMetaCache(art)
  78. }
  79. }
  80. return
  81. }
  82. // LoadArcsMedia loads the arc meta cms data from cache, for missed ones, pick them from the DB
  83. func (d *Dao) LoadArcsMedia(ctx context.Context, aids []int64) (arcs []*model.ArcCMS, err error) {
  84. var (
  85. resMetas map[int64]*model.ArcCMS // merge info from MC and from DB
  86. )
  87. if resMetas, err = d.LoadArcsMediaMap(ctx, aids); err != nil {
  88. log.Error("LoadArcsMedia LoadArcsMediaMap Aids: %v, Err: %v", aids, err)
  89. return
  90. }
  91. // re-arrange the info, according to the order got from Redis
  92. for _, v := range aids {
  93. if arcCMS, ok := resMetas[v]; !ok {
  94. log.Error("PickDBeiPage LoadArcsMedia Miss Info for Sid: %d", v)
  95. continue
  96. } else {
  97. arcs = append(arcs, arcCMS)
  98. }
  99. }
  100. return
  101. }
  102. // LoadArcMeta loads the arc meta cms data from cache, for missed ones, pick them from the DB
  103. func (d *Dao) LoadArcMeta(ctx context.Context, aid int64) (arcMeta *model.ArcCMS, err error) {
  104. if arcMeta, err = d.ArcMetaCache(ctx, aid); err != nil { // mc error
  105. log.Error("LoadArcMedia Get Aid [%d] from CMS Error (%v)", aid, err)
  106. return
  107. }
  108. if arcMeta != nil { // mc found
  109. return
  110. }
  111. if arcMeta, err = d.ArcMetaDB(ctx, aid); err != nil { // db error
  112. log.Error("LoadArcMedia ArcMetaDB Aid ERROR (%d) (%v)", aid, err)
  113. return
  114. }
  115. if arcMeta == nil { // db not found
  116. err = ecode.NothingFound
  117. return
  118. }
  119. d.AddArcMetaCache(arcMeta) // db found, re-fill the cache
  120. return
  121. }
  122. // LoadVideoMeta loads the video meta cms data from cache, for missed ones, pick them from the DB
  123. func (d *Dao) LoadVideoMeta(ctx context.Context, cid int64) (videoMeta *model.VideoCMS, err error) {
  124. if videoMeta, err = d.VideoMetaCache(ctx, cid); err != nil { // mc error
  125. log.Error("LoadVideoMeta Get Cid [%d] from CMS Error (%v)", cid, err)
  126. return
  127. }
  128. if videoMeta != nil { // mc found
  129. return
  130. }
  131. if videoMeta, err = d.VideoMetaDB(ctx, cid); err != nil { // db error
  132. log.Error("LoadArcMedia ArcMetaDB Aid ERROR (%d) (%v)", cid, err)
  133. return
  134. }
  135. if videoMeta == nil { // db not found
  136. err = ecode.NothingFound
  137. return
  138. }
  139. d.AddVideoMetaCache(videoMeta) // db found, re-fill the cache
  140. return
  141. }