load_upper.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package upper
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. ugcMdl "go-common/app/job/main/tv/model/ugc"
  7. "go-common/library/cache/memcache"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _loadUpper = "SELECT mid,toinit,submit,ori_name,cms_name,ori_face,cms_face,valid,deleted FROM ugc_uploader WHERE mid = ?"
  14. )
  15. func upperMetaKey(MID int64) string {
  16. return fmt.Sprintf("up_cms_%d", MID)
  17. }
  18. // LoadUpMeta loads the upper meta cms data from cache, for missed ones, pick them from the DB
  19. func (d *Dao) LoadUpMeta(ctx context.Context, mid int64) (upper *ugcMdl.Upper, err error) {
  20. if upper, err = d.upMetaCache(ctx, mid); err != nil { // mc error
  21. log.Error("LoadUpMeta Get Mid [%d] from CMS Error (%v)", mid, err)
  22. return
  23. }
  24. if upper != nil { // mc found
  25. return
  26. }
  27. if upper, err = d.upMetaDB(ctx, mid); err != nil { // db error
  28. log.Error("LoadUpMeta Get Mid ERROR (%d) (%v)", mid, err)
  29. return
  30. }
  31. if upper == nil { // db not found
  32. err = ecode.NothingFound
  33. return
  34. }
  35. d.addUpMetaCache(ctx, upper) // db found, re-fill the cache
  36. return
  37. }
  38. // upMetaCache get upper meta cache.
  39. func (d *Dao) upMetaCache(c context.Context, mid int64) (upper *ugcMdl.Upper, err error) {
  40. var (
  41. key = upperMetaKey(mid)
  42. conn = d.mc.Get(c)
  43. item *memcache.Item
  44. )
  45. defer conn.Close()
  46. if item, err = conn.Get(key); err != nil {
  47. if err == memcache.ErrNotFound {
  48. err = nil
  49. missedCount.Add("tv-meta", 1)
  50. } else {
  51. log.Error("conn.Get(%s) error(%v)", key, err)
  52. }
  53. return
  54. }
  55. if err = conn.Scan(item, &upper); err != nil {
  56. log.Error("conn.Get(%s) error(%v)", key, err)
  57. }
  58. cachedCount.Add("tv-meta", 1)
  59. return
  60. }
  61. // upMetaDB gets upper meta info from DB
  62. func (d *Dao) upMetaDB(c context.Context, mid int64) (upper *ugcMdl.Upper, err error) {
  63. var row *xsql.Row
  64. if row = d.DB.QueryRow(c, _loadUpper, mid); err != nil {
  65. log.Error("d.db.QueryRow(%d) error(%v)", mid, err)
  66. return
  67. }
  68. upper = &ugcMdl.Upper{}
  69. // "SELECT id,mid,to_init,submit,ori_name,cms_name,ori_face,cms_face,valid,deleted FROM ugc_uploader WHERE mid = ?"
  70. if err = row.Scan(&upper.MID, &upper.Toinit, &upper.Submit, &upper.OriName,
  71. &upper.CMSName, &upper.OriFace, &upper.CMSFace, &upper.Valid, &upper.Deleted); err != nil {
  72. if err == sql.ErrNoRows {
  73. err = nil
  74. upper = nil
  75. } else {
  76. log.Error("row.Scan(mid %d) error(%v)", mid, err)
  77. }
  78. }
  79. return
  80. }
  81. // setUpMetaCache save ugcMdl.Upper to memcache
  82. func (d *Dao) addUpMetaCache(c context.Context, upper *ugcMdl.Upper) (err error) {
  83. var (
  84. key = upperMetaKey(upper.MID)
  85. conn = d.mc.Get(c)
  86. )
  87. defer conn.Close()
  88. if err = conn.Set(&memcache.Item{Key: key, Object: upper, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  89. log.Error("conn.Set error(%v)", err)
  90. return
  91. }
  92. return
  93. }