pgc_batch.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package cms
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/tv/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _mcSnCMSKey = "sn_cms_%d"
  11. _mcEPCMSKey = "ep_cms_%d"
  12. )
  13. func snCMSCacheKey(sid int64) string {
  14. return fmt.Sprintf(_mcSnCMSKey, sid)
  15. }
  16. func epCMSCacheKey(epid int64) string {
  17. return fmt.Sprintf(_mcEPCMSKey, epid)
  18. }
  19. func keysTreat(ids []int64, keyFunc func(int64) string) (idmap map[string]int64, allKeys []string) {
  20. idmap = make(map[string]int64, len(ids))
  21. for _, id := range ids {
  22. k := keyFunc(id)
  23. allKeys = append(allKeys, k)
  24. idmap[k] = id
  25. }
  26. return
  27. }
  28. func missedTreat(idmap map[string]int64, lenCached int) (missed []int64) {
  29. missed = make([]int64, 0, len(idmap))
  30. for _, id := range idmap {
  31. missed = append(missed, id)
  32. }
  33. missedCount.Add("tv-meta", int64(len(missed)))
  34. cachedCount.Add("tv-meta", int64(lenCached))
  35. return
  36. }
  37. // SeasonsMetaCache season cms meta cache
  38. func (d *Dao) SeasonsMetaCache(c context.Context, ids []int64) (cached map[int64]*model.SeasonCMS, missed []int64, err error) {
  39. if len(ids) == 0 {
  40. return
  41. }
  42. cached = make(map[int64]*model.SeasonCMS, len(ids))
  43. idmap, allKeys := keysTreat(ids, snCMSCacheKey)
  44. conn := d.mc.Get(c)
  45. defer conn.Close()
  46. replys, err := conn.GetMulti(allKeys)
  47. if err != nil {
  48. PromError("mc:获取Season信息缓存")
  49. log.Error("conn.Gets(%v) error(%v)", allKeys, err)
  50. err = nil
  51. return
  52. }
  53. for key, item := range replys {
  54. art := &model.SeasonCMS{}
  55. if err = conn.Scan(item, art); err != nil {
  56. PromError("mc:获取Season信息缓存json解析")
  57. log.Error("item.Scan(%s) error(%v)", item.Value, err)
  58. err = nil
  59. continue
  60. }
  61. cached[idmap[key]] = art
  62. delete(idmap, key)
  63. }
  64. missed = missedTreat(idmap, len(cached))
  65. return
  66. }
  67. // EpMetaCache season cms meta cache
  68. func (d *Dao) EpMetaCache(c context.Context, ids []int64) (cached map[int64]*model.EpCMS, missed []int64, err error) {
  69. if len(ids) == 0 {
  70. return
  71. }
  72. cached = make(map[int64]*model.EpCMS, len(ids))
  73. allKeys := make([]string, 0, len(ids))
  74. idmap := make(map[string]int64, len(ids))
  75. for _, id := range ids {
  76. k := epCMSCacheKey(id)
  77. allKeys = append(allKeys, k)
  78. idmap[k] = id
  79. }
  80. conn := d.mc.Get(c)
  81. defer conn.Close()
  82. replys, err := conn.GetMulti(allKeys)
  83. if err != nil {
  84. PromError("mc:获取EP信息缓存")
  85. log.Error("conn.Gets(%v) error(%v)", allKeys, err)
  86. err = nil
  87. return
  88. }
  89. for key, item := range replys {
  90. art := &model.EpCMS{}
  91. if err = conn.Scan(item, art); err != nil {
  92. PromError("mc:获取EP信息缓存json解析")
  93. log.Error("item.Scan(%s) error(%v)", item.Value, err)
  94. err = nil
  95. continue
  96. }
  97. cached[idmap[key]] = art
  98. delete(idmap, key)
  99. }
  100. missed = make([]int64, 0, len(idmap))
  101. for _, id := range idmap {
  102. missed = append(missed, int64(id))
  103. }
  104. missedCount.Add("tv-meta", int64(len(missed)))
  105. cachedCount.Add("tv-meta", int64(len(cached)))
  106. return
  107. }
  108. //AddSeasonMetaCache add season meta cache
  109. func (d *Dao) AddSeasonMetaCache(c context.Context, vs ...*model.SeasonCMS) (err error) {
  110. conn := d.mc.Get(c)
  111. defer conn.Close()
  112. for _, v := range vs {
  113. if v == nil {
  114. continue
  115. }
  116. item := &memcache.Item{Key: snCMSCacheKey(v.SeasonID), Object: v, Flags: memcache.FlagJSON, Expiration: d.expireCMS}
  117. if err = conn.Set(item); err != nil {
  118. PromError("mc:增加Season信息缓存")
  119. log.Error("conn.Store(%s) error(%v)", snCMSCacheKey(v.SeasonID), err)
  120. return
  121. }
  122. }
  123. return
  124. }
  125. //AddEpMetaCache add ep meta cache
  126. func (d *Dao) AddEpMetaCache(c context.Context, vs ...*model.EpCMS) (err error) {
  127. conn := d.mc.Get(c)
  128. defer conn.Close()
  129. for _, v := range vs {
  130. if v == nil {
  131. continue
  132. }
  133. item := &memcache.Item{Key: epCMSCacheKey(v.EPID), Object: v, Flags: memcache.FlagJSON, Expiration: d.expireCMS}
  134. if err = conn.Set(item); err != nil {
  135. PromError("mc:增加EP信息缓存")
  136. log.Error("conn.Store(%s) error(%v)", epCMSCacheKey(v.EPID), err)
  137. return
  138. }
  139. }
  140. return
  141. }