media_cache.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package ugc
  2. import (
  3. "context"
  4. "fmt"
  5. ugcmdl "go-common/app/job/main/tv/model/ugc"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _mcArcCMSKey = "arc_cms_%d"
  12. _mcVideoCMSKey = "video_cms_%d"
  13. _totalArcs = "SELECT count(1) FROM ugc_archive WHERE mid = ?"
  14. _totalVideos = "SELECT count(1) FROM ugc_video"
  15. _activeVideos = "SELECT count(1) FROM ugc_video WHERE aid = ? AND deleted = 0"
  16. _cntArcVideo = "SELECT count(1) FROM ugc_video WHERE aid = ?"
  17. _pickArcMC = "SELECT title, aid, content, cover, typeid, pubtime, videos, valid, deleted, result,copyright, state, mid,duration FROM ugc_archive " +
  18. "WHERE mid = %d LIMIT %d,%d"
  19. _pickArcVideoMC = "SELECT cid, eptitle, aid, index_order, valid, deleted, result FROM ugc_video " +
  20. "WHERE aid = ? AND cid > ? ORDER BY cid LIMIT 0,%d"
  21. _transFailVideos = "SELECT cid FROM ugc_video WHERE aid = ? AND cid > %d AND transcoded = 2 and deleted = 0"
  22. )
  23. func arcCMSCacheKey(sid int64) string {
  24. return fmt.Sprintf(_mcArcCMSKey, sid)
  25. }
  26. func videoCMSCacheKey(sid int) string {
  27. return fmt.Sprintf(_mcVideoCMSKey, sid)
  28. }
  29. // SetArcCMS in MC
  30. func (d *Dao) SetArcCMS(ctx context.Context, res *ugcmdl.ArcCMS) (err error) {
  31. var (
  32. key = arcCMSCacheKey(res.AID)
  33. conn = d.mc.Get(ctx)
  34. )
  35. defer conn.Close()
  36. if err = conn.Set(&memcache.Item{Key: key, Object: res, Expiration: d.mcExpire, Flags: memcache.FlagJSON}); err != nil {
  37. log.Error("conn.Set(%s,%v) error(%v)", key, res, err)
  38. }
  39. return
  40. }
  41. // SetVideoCMS in MC
  42. func (d *Dao) SetVideoCMS(ctx context.Context, res *ugcmdl.VideoCMS) (err error) {
  43. var (
  44. key = videoCMSCacheKey(res.CID)
  45. conn = d.mc.Get(ctx)
  46. )
  47. defer conn.Close()
  48. if err = conn.Set(&memcache.Item{Key: key, Object: res, Expiration: d.mcExpire, Flags: memcache.FlagJSON}); err != nil {
  49. log.Error("conn.Set(%s,%v) error(%v)", key, res, err)
  50. }
  51. return
  52. }
  53. // UpArcsCnt counts the total number of arcs, including the deleted ones
  54. func (d *Dao) UpArcsCnt(c context.Context, mid int64) (count int, err error) {
  55. if err = d.DB.QueryRow(c, _totalArcs, mid).Scan(&count); err != nil {
  56. log.Error("d.UpArcsCnt.Query error(%v)", err)
  57. }
  58. return
  59. }
  60. // TotalVideos counts the total number of arcs, including the deleted ones
  61. func (d *Dao) TotalVideos(c context.Context) (count int, err error) {
  62. if err = d.DB.QueryRow(c, _totalVideos).Scan(&count); err != nil {
  63. log.Error("d.TotalVideos.Query error(%v)", err)
  64. }
  65. return
  66. }
  67. // ActVideos checks whether there is still some active videos
  68. func (d *Dao) ActVideos(c context.Context, aid int64) (has bool, err error) {
  69. var count int
  70. if err = d.DB.QueryRow(c, _activeVideos, aid).Scan(&count); err != nil {
  71. log.Error("d.TotalVideos.Query error(%v)", err)
  72. return
  73. }
  74. if count == 0 {
  75. has = false
  76. } else {
  77. has = true
  78. }
  79. return
  80. }
  81. // ArcVideoCnt counts one archive's video
  82. func (d *Dao) ArcVideoCnt(c context.Context, aid int64) (count int, err error) {
  83. if err = d.DB.QueryRow(c, _cntArcVideo, aid).Scan(&count); err != nil {
  84. log.Error("d.TotalVideos.Query error(%v)", err)
  85. }
  86. return
  87. }
  88. // PickUpArcs picks data by Piece to sync in MC, attention: nbPiece begins from zero
  89. func (d *Dao) PickUpArcs(ctx context.Context, mid, nbPiece, nbData int) (res []*ugcmdl.ArcFull, err error) {
  90. var (
  91. rows *sql.Rows
  92. query = fmt.Sprintf(_pickArcMC, mid, nbPiece*nbData, nbData)
  93. )
  94. if rows, err = d.DB.Query(ctx, query); err != nil {
  95. log.Error("d.PickUpArcs.Query: %s error(%v)", query, err)
  96. return
  97. }
  98. defer rows.Close()
  99. for rows.Next() {
  100. var li = &ugcmdl.ArcFull{}
  101. // SELECT title, aid, content, cover, typeid, pubtime, videos, valid, deleted, result,copyright, state, mid,duration
  102. if err = rows.Scan(&li.Title, &li.AID, &li.Content, &li.Cover, &li.TypeID, &li.Pubtime,
  103. &li.Videos, &li.Valid, &li.Deleted, &li.Result, &li.Copyright, &li.State, &li.MID, &li.Duration); err != nil {
  104. log.Error("PickUpArcs row.Scan() error(%v)", err)
  105. return
  106. }
  107. res = append(res, li)
  108. }
  109. if err = rows.Err(); err != nil {
  110. log.Error("d.PickUpArcs.Query error(%v)", err)
  111. }
  112. return
  113. }
  114. // PickArcVideo picks data by Piece to sync in MC
  115. func (d *Dao) PickArcVideo(ctx context.Context, aid int64, LastID int, nbData int) (res []*ugcmdl.VideoCMS, myLast int, err error) {
  116. var (
  117. rows *sql.Rows
  118. query = fmt.Sprintf(_pickArcVideoMC, nbData)
  119. )
  120. if rows, err = d.DB.Query(ctx, query, aid, LastID); err != nil {
  121. log.Error("d._pickArcVideoMC.Query: %s error(%v)", query, err)
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. var li = new(ugcmdl.VideoCMS)
  127. if err = rows.Scan(&li.CID, &li.Title, &li.AID, &li.IndexOrder, &li.Valid, &li.Deleted, &li.Result); err != nil {
  128. log.Error("PickVideoMC row.Scan() error(%v)", err)
  129. return
  130. }
  131. res = append(res, li)
  132. myLast = li.CID
  133. }
  134. if err = rows.Err(); err != nil {
  135. log.Error("d.PickArcVideo.Query error(%v)", err)
  136. }
  137. return
  138. }
  139. // TransFailVideos picks transcoding failure videos
  140. func (d *Dao) TransFailVideos(ctx context.Context, aid int64) (cids []int64, err error) {
  141. var (
  142. rows *sql.Rows
  143. query = fmt.Sprintf(_transFailVideos, d.criCID)
  144. )
  145. if rows, err = d.DB.Query(ctx, query, aid); err != nil {
  146. log.Error("d.TransFailVideos.Query: Cid %d error(%v)", aid, err)
  147. return
  148. }
  149. defer rows.Close()
  150. for rows.Next() {
  151. var li int64
  152. if err = rows.Scan(&li); err != nil {
  153. log.Error("PickVideoMC row.Scan() error(%v)", err)
  154. return
  155. }
  156. cids = append(cids, li)
  157. }
  158. if err = rows.Err(); err != nil {
  159. log.Error("d.TransFailVideos.Query error(%v)", err)
  160. }
  161. return
  162. }