mc.cache.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/service/main/card/model"
  7. mc "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "go-common/library/stat/prom"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _prequip = "e_"
  14. )
  15. func equipKey(mid int64) string {
  16. return _prequip + strconv.FormatInt(mid, 10)
  17. }
  18. // pingMC ping memcache.
  19. func (d *Dao) pingMC(c context.Context) (err error) {
  20. conn := d.mc.Get(c)
  21. defer conn.Close()
  22. err = conn.Set(&mc.Item{
  23. Key: "ping",
  24. Value: []byte("pong"),
  25. })
  26. return
  27. }
  28. // CacheEquips get data from mc
  29. func (d *Dao) CacheEquips(c context.Context, mids []int64) (res map[int64]*model.UserEquip, err error) {
  30. keys := make([]string, 0, len(mids))
  31. keyMidMap := make(map[string]int64, len(mids))
  32. for _, mid := range mids {
  33. key := equipKey(mid)
  34. if _, ok := keyMidMap[key]; !ok {
  35. // duplicate mid
  36. keyMidMap[key] = mid
  37. keys = append(keys, key)
  38. }
  39. }
  40. conn := d.mc.Get(c)
  41. defer conn.Close()
  42. rs, err := conn.GetMulti(keys)
  43. if err != nil {
  44. if err == mc.ErrNotFound {
  45. err = nil
  46. return
  47. }
  48. err = errors.Wrap(err, "dao equips cache")
  49. return
  50. }
  51. res = make(map[int64]*model.UserEquip, len(mids))
  52. for k, r := range rs {
  53. e := &model.UserEquip{}
  54. conn.Scan(r, e)
  55. res[keyMidMap[k]] = e
  56. }
  57. return
  58. }
  59. // CacheEquip get user card equip from cache.
  60. func (d *Dao) CacheEquip(c context.Context, mid int64) (v *model.UserEquip, err error) {
  61. key := equipKey(mid)
  62. conn := d.mc.Get(c)
  63. defer conn.Close()
  64. r, err := conn.Get(key)
  65. if err != nil {
  66. if err == mc.ErrNotFound {
  67. err = nil
  68. return
  69. }
  70. err = errors.Wrap(err, "dao cache equip")
  71. return
  72. }
  73. v = &model.UserEquip{}
  74. if err = conn.Scan(r, v); err != nil {
  75. err = errors.Wrap(err, "dao cache scan equip")
  76. }
  77. return
  78. }
  79. // AddCacheEquips Set data to mc
  80. func (d *Dao) AddCacheEquips(c context.Context, values map[int64]*model.UserEquip) (err error) {
  81. conn := d.mc.Get(c)
  82. defer conn.Close()
  83. for k, v := range values {
  84. item := &mc.Item{
  85. Key: equipKey(k),
  86. Object: v,
  87. Flags: mc.FlagProtobuf,
  88. Expiration: d.mcExpire,
  89. }
  90. if err = conn.Set(item); err != nil {
  91. err = errors.Wrap(err, "dao add equips cache")
  92. }
  93. }
  94. return
  95. }
  96. // AddCacheEquip set user card equip info into cache.
  97. func (d *Dao) AddCacheEquip(c context.Context, mid int64, v *model.UserEquip) (err error) {
  98. item := &mc.Item{
  99. Key: equipKey(mid),
  100. Object: v,
  101. Flags: mc.FlagProtobuf,
  102. Expiration: d.mcExpire,
  103. }
  104. conn := d.mc.Get(c)
  105. defer conn.Close()
  106. if err = conn.Set(item); err != nil {
  107. err = errors.Wrap(err, "dao add equip cache")
  108. }
  109. return
  110. }
  111. // DelCacheEquips delete data from mc
  112. func (d *Dao) DelCacheEquips(c context.Context, ids []int64) (err error) {
  113. if len(ids) == 0 {
  114. return
  115. }
  116. conn := d.mc.Get(c)
  117. defer conn.Close()
  118. for _, id := range ids {
  119. key := equipKey(id)
  120. if err = conn.Delete(key); err != nil {
  121. if err == mc.ErrNotFound {
  122. err = nil
  123. continue
  124. }
  125. prom.BusinessErrCount.Incr("mc:DelCacheEquips")
  126. log.Errorv(c, log.KV("DelCacheEquips", fmt.Sprintf("%+v", err)), log.KV("key", key))
  127. return
  128. }
  129. }
  130. return
  131. }
  132. // DelCacheEquip delete data from mc
  133. func (d *Dao) DelCacheEquip(c context.Context, id int64) (err error) {
  134. conn := d.mc.Get(c)
  135. defer conn.Close()
  136. key := equipKey(id)
  137. if err = conn.Delete(key); err != nil {
  138. if err == mc.ErrNotFound {
  139. err = nil
  140. return
  141. }
  142. prom.BusinessErrCount.Incr("mc:DelCacheEquip")
  143. log.Errorv(c, log.KV("DelCacheEquip", fmt.Sprintf("%+v", err)), log.KV("key", key))
  144. return
  145. }
  146. return
  147. }