mc.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/tool/saga/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. requiredViableUsersKey = "saga_wechat_require_visible_users_key"
  12. )
  13. func (d *Dao) pingMC(c context.Context) (err error) {
  14. conn := d.mcMR.Get(c)
  15. defer conn.Close()
  16. if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
  17. err = errors.Wrap(err, "conn.Store(set,ping,1)")
  18. }
  19. return
  20. }
  21. func mrRecordKey(mrID int) string {
  22. return fmt.Sprintf("saga_mr_%d", mrID)
  23. }
  24. // MRRecordCache get MRRecord from mc
  25. func (d *Dao) MRRecordCache(c context.Context, mrID int) (record *model.MRRecord, err error) {
  26. var (
  27. key = mrRecordKey(mrID)
  28. conn = d.mcMR.Get(c)
  29. reply *memcache.Item
  30. )
  31. defer conn.Close()
  32. reply, err = conn.Get(key)
  33. if err != nil {
  34. if err == memcache.ErrNotFound {
  35. err = nil
  36. return
  37. }
  38. err = errors.Wrapf(err, "conn.Get(get,%s)", key)
  39. return
  40. }
  41. record = &model.MRRecord{}
  42. if err = conn.Scan(reply, record); err != nil {
  43. err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
  44. }
  45. return
  46. }
  47. // SetMRRecordCache set MRRecord to mc
  48. func (d *Dao) SetMRRecordCache(c context.Context, record *model.MRRecord) (err error) {
  49. var (
  50. key = mrRecordKey(record.MRID)
  51. conn = d.mcMR.Get(c)
  52. )
  53. defer conn.Close()
  54. if err = conn.Set(&memcache.Item{Key: key, Object: record, Expiration: 0, Flags: memcache.FlagJSON}); err != nil {
  55. err = errors.Wrapf(err, "conn.Add(%s,%v)", key, record)
  56. return
  57. }
  58. return
  59. }
  60. func weixinTokenKey(key string) string {
  61. return fmt.Sprintf("saga_weixin_token_%s", key)
  62. }
  63. // AccessToken get access token from mc
  64. func (d *Dao) AccessToken(c context.Context, key string) (token string, err error) {
  65. var (
  66. wkey = weixinTokenKey(key)
  67. conn = d.mcMR.Get(c)
  68. reply *memcache.Item
  69. )
  70. defer conn.Close()
  71. reply, err = conn.Get(wkey)
  72. if err != nil {
  73. if err == memcache.ErrNotFound {
  74. err = nil
  75. return
  76. }
  77. err = errors.Wrapf(err, "conn.Get(get,%s)", wkey)
  78. return
  79. }
  80. if err = conn.Scan(reply, &token); err != nil {
  81. err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
  82. }
  83. return
  84. }
  85. // SetAccessToken set the access token to mc
  86. func (d *Dao) SetAccessToken(c context.Context, key string, token string, expire int32) (err error) {
  87. var (
  88. wkey = weixinTokenKey(key)
  89. conn = d.mcMR.Get(c)
  90. item *memcache.Item
  91. )
  92. defer conn.Close()
  93. item = &memcache.Item{Key: wkey, Object: token, Expiration: expire, Flags: memcache.FlagJSON}
  94. if err = conn.Set(item); err != nil {
  95. err = errors.Wrapf(err, "conn.Add(%s,%v)", wkey, token)
  96. return
  97. }
  98. return
  99. }
  100. // RequireVisibleUsers get wechat require visible users from memcache
  101. func (d *Dao) RequireVisibleUsers(c context.Context, userMap *map[string]model.RequireVisibleUser) (err error) {
  102. var (
  103. conn = d.mcMR.Get(c)
  104. reply *memcache.Item
  105. )
  106. defer conn.Close()
  107. reply, err = conn.Get(requiredViableUsersKey)
  108. if err != nil {
  109. if err == memcache.ErrNotFound {
  110. log.Info("no such key (%s) in cache, err (%s)", requiredViableUsersKey, err.Error())
  111. err = nil
  112. }
  113. return
  114. }
  115. if err = conn.Scan(reply, userMap); err != nil {
  116. err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
  117. }
  118. return
  119. }
  120. // SetRequireVisibleUsers set wechat require visible users to memcache
  121. func (d *Dao) SetRequireVisibleUsers(c context.Context, contactInfo *model.ContactInfo) (err error) {
  122. var (
  123. conn = d.mcMR.Get(c)
  124. item *memcache.Item
  125. userMap = make(map[string]model.RequireVisibleUser)
  126. )
  127. defer conn.Close()
  128. if err = d.RequireVisibleUsers(c, &userMap); err != nil {
  129. log.Error("get require visible user error(%v)", err)
  130. return
  131. }
  132. user := model.RequireVisibleUser{
  133. UserName: contactInfo.UserName,
  134. NickName: contactInfo.NickName,
  135. }
  136. userMap[contactInfo.UserID] = user
  137. item = &memcache.Item{Key: requiredViableUsersKey, Object: userMap, Expiration: 0, Flags: memcache.FlagJSON}
  138. if err = conn.Set(item); err != nil {
  139. err = errors.Wrapf(err, "conn.Set(%s,%v)", requiredViableUsersKey, userMap)
  140. return
  141. }
  142. return
  143. }
  144. // DeleteRequireVisibleUsers delete the wechat require visible key in memcache
  145. func (d *Dao) DeleteRequireVisibleUsers(c context.Context) (err error) {
  146. var (
  147. conn = d.mcMR.Get(c)
  148. )
  149. defer conn.Close()
  150. err = conn.Delete(requiredViableUsersKey)
  151. if err != nil {
  152. err = errors.Wrapf(err, "conn.Delete(%s)", requiredViableUsersKey)
  153. }
  154. return
  155. }