associate_mc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/service/main/vip/model"
  6. mc "go-common/library/cache/memcache"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _prefixBindByMid = "b:m:"
  11. _prefixOpenInfoByOpenID = "o:i:"
  12. )
  13. func keyBindByMid(mid int64, appID int64) string {
  14. return _prefixBindByMid + strconv.FormatInt(mid, 10) + ":" + strconv.FormatInt(appID, 10)
  15. }
  16. func keyOpenInfoByOpenID(openID string, appID int64) string {
  17. return _prefixOpenInfoByOpenID + openID + ":" + strconv.FormatInt(appID, 10)
  18. }
  19. // CacheBindInfoByMid get vip bind by mid cache.
  20. func (d *Dao) CacheBindInfoByMid(c context.Context, mid int64, appID int64) (v *model.OpenBindInfo, err error) {
  21. key := keyBindByMid(mid, appID)
  22. conn := d.mc.Get(c)
  23. defer conn.Close()
  24. r, err := conn.Get(key)
  25. if err != nil {
  26. if err == mc.ErrNotFound {
  27. err = nil
  28. return
  29. }
  30. err = errors.Wrapf(err, "dao cache bind by mid")
  31. return
  32. }
  33. v = new(model.OpenBindInfo)
  34. if err = conn.Scan(r, v); err != nil {
  35. err = errors.Wrapf(err, "dao cache scan bind by mid")
  36. }
  37. return
  38. }
  39. // CacheOpenInfoByOpenID get vip open info by open id cache.
  40. func (d *Dao) CacheOpenInfoByOpenID(c context.Context, openID string, appID int64) (v *model.OpenInfo, err error) {
  41. key := keyOpenInfoByOpenID(openID, appID)
  42. conn := d.mc.Get(c)
  43. defer conn.Close()
  44. r, err := conn.Get(key)
  45. if err != nil {
  46. if err == mc.ErrNotFound {
  47. err = nil
  48. return
  49. }
  50. err = errors.Wrapf(err, "dao cache open by openid")
  51. return
  52. }
  53. v = new(model.OpenInfo)
  54. if err = conn.Scan(r, v); err != nil {
  55. err = errors.Wrapf(err, "dao cache scan open by openid")
  56. }
  57. return
  58. }
  59. // AddCacheBindInfoByMid add bind info cache.
  60. func (d *Dao) AddCacheBindInfoByMid(c context.Context, mid int64, v *model.OpenBindInfo, appID int64) (err error) {
  61. item := &mc.Item{
  62. Key: keyBindByMid(mid, appID),
  63. Object: v,
  64. Expiration: d.mcExpire,
  65. Flags: mc.FlagProtobuf,
  66. }
  67. conn := d.mc.Get(c)
  68. err = conn.Set(item)
  69. conn.Close()
  70. if err != nil {
  71. err = errors.Wrapf(err, "dao add cache bind by mid")
  72. }
  73. return
  74. }
  75. // AddCacheOpenInfoByOpenID add open info cache.
  76. func (d *Dao) AddCacheOpenInfoByOpenID(c context.Context, openID string, v *model.OpenInfo, appID int64) (err error) {
  77. item := &mc.Item{
  78. Key: keyOpenInfoByOpenID(openID, appID),
  79. Object: v,
  80. Expiration: d.mcExpire,
  81. Flags: mc.FlagProtobuf,
  82. }
  83. conn := d.mc.Get(c)
  84. err = conn.Set(item)
  85. conn.Close()
  86. if err != nil {
  87. err = errors.Wrapf(err, "dao add cache open by openid")
  88. }
  89. return
  90. }
  91. // DelBindInfoCache del bind info cache.
  92. func (d *Dao) DelBindInfoCache(c context.Context, mid int64, appID int64) (err error) {
  93. return d.delCache(c, keyBindByMid(mid, appID))
  94. }
  95. // DelOpenInfoCache del open info cache.
  96. func (d *Dao) DelOpenInfoCache(c context.Context, openID string, appID int64) (err error) {
  97. return d.delCache(c, keyOpenInfoByOpenID(openID, appID))
  98. }
  99. func (d *Dao) delCache(c context.Context, key string) (err error) {
  100. conn := d.mc.Get(c)
  101. defer conn.Close()
  102. if err = conn.Delete(key); err != nil {
  103. if err == mc.ErrNotFound {
  104. err = nil
  105. } else {
  106. err = errors.Wrapf(err, "del cache")
  107. }
  108. }
  109. return
  110. }