pgc_auth.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. _mcEPKey = "ep_%d"
  11. _mcSeasonKey = "sn_%d"
  12. )
  13. // SeaCacheKey .
  14. func (d *Dao) SeaCacheKey(sid int64) string {
  15. return fmt.Sprintf(_mcSeasonKey, sid)
  16. }
  17. // EPCacheKey .
  18. func (d *Dao) EPCacheKey(epid int64) string {
  19. return fmt.Sprintf(_mcEPKey, epid)
  20. }
  21. // GetSeasonCache get SnAuth cache.
  22. func (d *Dao) GetSeasonCache(c context.Context, sid int64) (s *model.SnAuth, err error) {
  23. var (
  24. key = d.SeaCacheKey(sid)
  25. conn = d.mc.Get(c)
  26. item *memcache.Item
  27. )
  28. defer conn.Close()
  29. if item, err = conn.Get(key); err != nil {
  30. if err == memcache.ErrNotFound {
  31. err = nil
  32. } else {
  33. log.Error("conn.Get(%s) error(%v)", key, err)
  34. }
  35. return
  36. }
  37. if err = conn.Scan(item, &s); err != nil {
  38. log.Error("conn.Get(%s) error(%v)", key, err)
  39. }
  40. return
  41. }
  42. // GetEPCache get EpAuth cache.
  43. func (d *Dao) GetEPCache(c context.Context, epid int64) (ep *model.EpAuth, err error) {
  44. var (
  45. key = d.EPCacheKey(epid)
  46. conn = d.mc.Get(c)
  47. item *memcache.Item
  48. )
  49. defer conn.Close()
  50. if item, err = conn.Get(key); err != nil {
  51. if err == memcache.ErrNotFound {
  52. err = nil
  53. } else {
  54. log.Error("conn.Get(%s) error(%v)", key, err)
  55. }
  56. return
  57. }
  58. if err = conn.Scan(item, &ep); err != nil {
  59. log.Error("conn.Get(%s) error(%v)", key, err)
  60. }
  61. return
  62. }
  63. // AddSnAuthCache save model.SnAuth to memcache
  64. func (d *Dao) AddSnAuthCache(c context.Context, s *model.SnAuth) (err error) {
  65. var (
  66. key = d.SeaCacheKey(s.ID)
  67. conn = d.mc.Get(c)
  68. )
  69. defer conn.Close()
  70. if err = conn.Set(&memcache.Item{Key: key, Object: s, Flags: memcache.FlagJSON, Expiration: d.expireCMS}); err != nil {
  71. log.Error("conn.Set error(%v)", err)
  72. return
  73. }
  74. return
  75. }
  76. // AddEpAuthCache save model.EpAuth to memcache
  77. func (d *Dao) AddEpAuthCache(c context.Context, ep *model.EpAuth) (err error) {
  78. var (
  79. key = d.EPCacheKey(ep.EPID)
  80. conn = d.mc.Get(c)
  81. )
  82. defer conn.Close()
  83. if err = conn.Set(&memcache.Item{Key: key, Object: ep, Flags: memcache.FlagJSON, Expiration: d.expireCMS}); err != nil {
  84. log.Error("conn.Set error(%v)", err)
  85. return
  86. }
  87. return
  88. }
  89. // snAuthCache season auth cache
  90. func (d *Dao) snAuthCache(c context.Context, ids []int64) (cached map[int64]*model.SnAuth, missed []int64, err error) {
  91. if len(ids) == 0 {
  92. return
  93. }
  94. cached = make(map[int64]*model.SnAuth, len(ids))
  95. idmap, allKeys := keysTreat(ids, d.SeaCacheKey)
  96. conn := d.mc.Get(c)
  97. defer conn.Close()
  98. replys, err := conn.GetMulti(allKeys)
  99. if err != nil {
  100. PromError("mc:获取Season信息缓存")
  101. log.Error("conn.Gets(%v) error(%v)", allKeys, err)
  102. return
  103. }
  104. for key, item := range replys {
  105. art := &model.SnAuth{}
  106. if err = conn.Scan(item, art); err != nil {
  107. PromError("mc:获取Season信息缓存json解析")
  108. log.Error("item.Scan(%s) error(%v)", item.Value, err)
  109. err = nil
  110. continue
  111. }
  112. cached[idmap[key]] = art
  113. delete(idmap, key)
  114. }
  115. missed = missedTreat(idmap, len(cached))
  116. return
  117. }
  118. // epAuthCache ep auth cache
  119. func (d *Dao) epAuthCache(c context.Context, ids []int64) (cached map[int64]*model.EpAuth, missed []int64, err error) {
  120. if len(ids) == 0 {
  121. return
  122. }
  123. cached = make(map[int64]*model.EpAuth, len(ids))
  124. idmap, allKeys := keysTreat(ids, d.EPCacheKey)
  125. conn := d.mc.Get(c)
  126. defer conn.Close()
  127. replys, err := conn.GetMulti(allKeys)
  128. if err != nil {
  129. PromError("mc:获取EP信息缓存")
  130. log.Error("conn.Gets(%v) error(%v)", allKeys, err)
  131. return
  132. }
  133. for key, item := range replys {
  134. art := &model.EpAuth{}
  135. if err = conn.Scan(item, art); err != nil {
  136. PromError("mc:获取EP信息缓存json解析")
  137. log.Error("item.Scan(%s) error(%v)", item.Value, err)
  138. err = nil
  139. continue
  140. }
  141. cached[idmap[key]] = art
  142. delete(idmap, key)
  143. }
  144. missed = missedTreat(idmap, len(cached))
  145. return
  146. }
  147. // SnAuth get's season auth info from Cache & DB
  148. func (d *Dao) SnAuth(c context.Context, sid int64) (sn *model.SnAuth, err error) {
  149. if sn, err = d.GetSeasonCache(c, sid); err != nil { // mc Error
  150. return
  151. } else if sn == nil { // mc not found, go DB
  152. if sn, err = d.SnAuthDB(c, int64(sid)); err != nil { // DB error
  153. log.Error("SnAuthDB (%d) ERROR (%v)", sid, err)
  154. return
  155. }
  156. if sn == nil { // DB not found, build a fake item in MC to avoid checking DB next time
  157. log.Error("SnAuthDB (%d) not found(%v) in DB", sid)
  158. sn = &model.SnAuth{ID: int64(sid), Check: 0, IsDeleted: 1}
  159. }
  160. if err = d.AddSnAuthCache(c, sn); err != nil { // set item in MC ( not found - fake, or true )
  161. log.Error("AddSnAuthCache fail(%v)", err)
  162. }
  163. }
  164. return
  165. }
  166. // EpAuth get's ep auth info from Cache & DB
  167. func (d *Dao) EpAuth(c context.Context, epid int64) (ep *model.EpAuth, err error) {
  168. if ep, err = d.GetEPCache(c, epid); err != nil { // MC error
  169. log.Error("GetEPCache(%d) Error(%v)", epid, err)
  170. return
  171. } else if ep == nil { // MC not found, go DB
  172. if ep, err = d.EpAuthDB(c, epid); err != nil { // DB error
  173. log.Error("DBSimpleEP(%d) ERROR (%v)", epid, err)
  174. return
  175. }
  176. if ep == nil { // DB not found, build a fake item in MC to avoid checking DB next time
  177. log.Error("EPID(%d) not found", epid)
  178. ep = &model.EpAuth{EPID: epid, State: 4, IsDeleted: 1}
  179. }
  180. if err = d.AddEpAuthCache(c, ep); err != nil {
  181. log.Error("AddEpAuthCache fail(%v)", err)
  182. }
  183. }
  184. return
  185. }