mc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/service/main/vipinfo/model"
  6. mc "go-common/library/cache/memcache"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _prefixInfo = "i:"
  11. _prefixFrozen = "vipfrozen:"
  12. )
  13. func keyInfo(mid int64) string {
  14. return _prefixInfo + strconv.FormatInt(mid, 10)
  15. }
  16. func keyFrozen(mid int64) string {
  17. return _prefixFrozen + strconv.FormatInt(mid, 10)
  18. }
  19. // pingMC ping memcache.
  20. func (d *Dao) pingMC(c context.Context) (err error) {
  21. conn := d.mc.Get(c)
  22. defer conn.Close()
  23. err = conn.Set(&mc.Item{
  24. Key: "ping",
  25. Value: []byte("pong"),
  26. })
  27. return
  28. }
  29. // CacheInfo get vip info cache.
  30. func (d *Dao) CacheInfo(c context.Context, mid int64) (v *model.VipUserInfo, err error) {
  31. key := keyInfo(mid)
  32. conn := d.mc.Get(c)
  33. defer conn.Close()
  34. r, err := conn.Get(key)
  35. if err != nil {
  36. if err == mc.ErrNotFound {
  37. err = nil
  38. return
  39. }
  40. err = errors.Wrapf(err, "dao cache info")
  41. return
  42. }
  43. v = new(model.VipUserInfo)
  44. if err = conn.Scan(r, v); err != nil {
  45. err = errors.Wrapf(err, "dao cache scan info")
  46. }
  47. return
  48. }
  49. // AddCacheInfo add vip info cache.
  50. func (d *Dao) AddCacheInfo(c context.Context, mid int64, v *model.VipUserInfo) (err error) {
  51. item := &mc.Item{
  52. Key: keyInfo(mid),
  53. Object: v,
  54. Expiration: d.mcExpire,
  55. Flags: mc.FlagProtobuf,
  56. }
  57. conn := d.mc.Get(c)
  58. err = conn.Set(item)
  59. conn.Close()
  60. if err != nil {
  61. err = errors.Wrapf(err, "dao add cache info")
  62. }
  63. return
  64. }
  65. // CacheInfos multi get account info from cache.
  66. func (d *Dao) CacheInfos(c context.Context, mids []int64) (res map[int64]*model.VipUserInfo, err error) {
  67. keys := make([]string, 0, len(mids))
  68. keyMidMap := make(map[string]int64, len(mids))
  69. for _, mid := range mids {
  70. key := keyInfo(mid)
  71. if _, ok := keyMidMap[key]; !ok {
  72. // duplicate mid
  73. keyMidMap[key] = mid
  74. keys = append(keys, key)
  75. }
  76. }
  77. conn := d.mc.Get(c)
  78. defer conn.Close()
  79. rs, err := conn.GetMulti(keys)
  80. if err != nil {
  81. if err == mc.ErrNotFound {
  82. err = nil
  83. return
  84. }
  85. err = errors.Wrap(err, "dao infos cache")
  86. return
  87. }
  88. res = make(map[int64]*model.VipUserInfo, len(mids))
  89. for _, r := range rs {
  90. ai := &model.VipUserInfo{}
  91. conn.Scan(r, ai)
  92. res[keyMidMap[r.Key]] = ai
  93. }
  94. return
  95. }
  96. // AddCacheInfos set account infos cache.
  97. func (d *Dao) AddCacheInfos(c context.Context, vs map[int64]*model.VipUserInfo) (err error) {
  98. conn := d.mc.Get(c)
  99. defer conn.Close()
  100. for mid, i := range vs {
  101. item := &mc.Item{
  102. Key: keyInfo(mid),
  103. Object: i,
  104. Flags: mc.FlagProtobuf,
  105. Expiration: d.mcExpire,
  106. }
  107. err = conn.Set(item)
  108. if err != nil {
  109. err = errors.Wrap(err, "dao add infos cache")
  110. }
  111. }
  112. return
  113. }
  114. // CacheVipFrozens multi get vip frozens from cache.
  115. func (d *Dao) CacheVipFrozens(c context.Context, mids []int64) (res map[int64]int, err error) {
  116. keys := make([]string, 0, len(mids))
  117. keyMidMap := make(map[string]int64, len(mids))
  118. for _, mid := range mids {
  119. key := keyFrozen(mid)
  120. if _, ok := keyMidMap[key]; !ok {
  121. // duplicate mid
  122. keyMidMap[key] = mid
  123. keys = append(keys, key)
  124. }
  125. }
  126. conn := d.mc.Get(c)
  127. defer conn.Close()
  128. rs, err := conn.GetMulti(keys)
  129. if err != nil {
  130. if err == mc.ErrNotFound {
  131. err = nil
  132. return
  133. }
  134. err = errors.Wrap(err, "dao frozens cache")
  135. return
  136. }
  137. res = make(map[int64]int, len(mids))
  138. for _, r := range rs {
  139. ai := 0
  140. conn.Scan(r, &ai)
  141. res[keyMidMap[r.Key]] = ai
  142. }
  143. return
  144. }
  145. //CacheVipFrozen get vip frozen flag.
  146. func (d *Dao) CacheVipFrozen(c context.Context, mid int64) (val int, err error) {
  147. key := keyFrozen(mid)
  148. conn := d.mc.Get(c)
  149. defer conn.Close()
  150. item, err := conn.Get(key)
  151. if err != nil {
  152. if err == mc.ErrNotFound {
  153. err = nil
  154. return
  155. }
  156. err = errors.Wrapf(err, "dao cache frozen")
  157. return
  158. }
  159. if err = conn.Scan(item, &val); err != nil {
  160. err = errors.Wrapf(err, "dao cache scan frozen")
  161. return
  162. }
  163. return
  164. }
  165. // AddCacheFrozen add cache frozen.
  166. func (d *Dao) AddCacheFrozen(c context.Context, mid int64, vipFrozenFlag int) (err error) {
  167. item := &mc.Item{
  168. Key: keyFrozen(mid),
  169. Object: vipFrozenFlag,
  170. Expiration: d.mcExpire,
  171. Flags: mc.FlagJSON,
  172. }
  173. conn := d.mc.Get(c)
  174. err = conn.Set(item)
  175. conn.Close()
  176. if err != nil {
  177. err = errors.Wrapf(err, "dao add cache frozen")
  178. }
  179. return
  180. }
  181. // DelInfoCache del vip info cache.
  182. func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
  183. d.delCache(c, keyInfo(mid))
  184. return
  185. }
  186. func (d *Dao) delCache(c context.Context, key string) (err error) {
  187. conn := d.mc.Get(c)
  188. defer conn.Close()
  189. if err = conn.Delete(key); err != nil {
  190. if err == mc.ErrNotFound {
  191. err = nil
  192. } else {
  193. err = errors.Wrapf(err, "del cache")
  194. }
  195. }
  196. return
  197. }