redis.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/admin/ep/saga/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. requiredViableUsersKeyRedis = "saga_wechat_require_visible_users_key"
  13. )
  14. func (d *Dao) pingRedis(c context.Context) (err error) {
  15. conn := d.redis.Get(c)
  16. _, err = conn.Do("SET", "PING", "PONG")
  17. defer conn.Close()
  18. return
  19. }
  20. func weixinTokenKeyRedis(key string) string {
  21. return fmt.Sprintf("saga_weixin_token_%s", key)
  22. }
  23. // AccessTokenRedis get access token from redis
  24. func (d *Dao) AccessTokenRedis(c context.Context, key string) (token string, err error) {
  25. var (
  26. wkey = weixinTokenKeyRedis(key)
  27. conn = d.redis.Get(c)
  28. value []byte
  29. )
  30. defer conn.Close()
  31. if value, err = redis.Bytes(conn.Do("GET", wkey)); err != nil {
  32. if err == redis.ErrNil {
  33. err = nil
  34. }
  35. return
  36. }
  37. if err = json.Unmarshal(value, &token); err != nil {
  38. err = errors.WithStack(err)
  39. return
  40. }
  41. return
  42. }
  43. // SetAccessTokenRedis set the access token to redis
  44. func (d *Dao) SetAccessTokenRedis(c context.Context, key, token string, expire int32) (err error) {
  45. var (
  46. wkey = weixinTokenKeyRedis(key)
  47. conn = d.redis.Get(c)
  48. item []byte
  49. )
  50. defer conn.Close()
  51. if item, err = json.Marshal(token); err != nil {
  52. err = errors.WithStack(err)
  53. return
  54. }
  55. if err = conn.Send("SET", wkey, item, "EX", expire); err != nil {
  56. return
  57. }
  58. if err = conn.Flush(); err != nil {
  59. return
  60. }
  61. if _, err = conn.Receive(); err != nil {
  62. return
  63. }
  64. return
  65. }
  66. // RequireVisibleUsersRedis get wechat require visible users from redis
  67. func (d *Dao) RequireVisibleUsersRedis(c context.Context, userMap *map[string]model.RequireVisibleUser) (err error) {
  68. var (
  69. conn = d.redis.Get(c)
  70. reply []byte
  71. )
  72. defer conn.Close()
  73. if reply, err = redis.Bytes(conn.Do("GET", requiredViableUsersKeyRedis)); err != nil {
  74. if err == redis.ErrNil {
  75. log.Info("no such key (%s) in cache, err (%s)", requiredViableUsersKeyRedis, err.Error())
  76. err = nil
  77. }
  78. return
  79. }
  80. if err = json.Unmarshal(reply, &userMap); err != nil {
  81. err = errors.WithStack(err)
  82. return
  83. }
  84. return
  85. }
  86. // SetRequireVisibleUsersRedis set wechat require visible users to redis
  87. func (d *Dao) SetRequireVisibleUsersRedis(c context.Context, contactInfo *model.ContactInfo) (err error) {
  88. var (
  89. conn = d.redis.Get(c)
  90. item []byte
  91. userMap = make(map[string]model.RequireVisibleUser)
  92. )
  93. defer conn.Close()
  94. if err = d.RequireVisibleUsersRedis(c, &userMap); err != nil {
  95. log.Error("get require visible user error(%v)", err)
  96. return
  97. }
  98. user := model.RequireVisibleUser{
  99. UserName: contactInfo.UserName,
  100. NickName: contactInfo.NickName,
  101. }
  102. userMap[contactInfo.UserID] = user
  103. if item, err = json.Marshal(userMap); err != nil {
  104. err = errors.WithStack(err)
  105. return
  106. }
  107. if err = conn.Send("SET", requiredViableUsersKeyRedis, item); err != nil {
  108. return
  109. }
  110. if err = conn.Flush(); err != nil {
  111. return
  112. }
  113. if _, err = conn.Receive(); err != nil {
  114. err = errors.Wrapf(err, "conn.Set(%s,%v)", requiredViableUsersKeyRedis, userMap)
  115. return
  116. }
  117. return
  118. }
  119. // DeleteRequireVisibleUsersRedis delete the wechat require visible key in redis
  120. func (d *Dao) DeleteRequireVisibleUsersRedis(c context.Context) (err error) {
  121. var (
  122. conn = d.redis.Get(c)
  123. )
  124. defer conn.Close()
  125. if err = conn.Send("DEL", requiredViableUsersKeyRedis); err != nil {
  126. return
  127. }
  128. if err = conn.Flush(); err != nil {
  129. return
  130. }
  131. if _, err = conn.Receive(); err != nil {
  132. err = errors.Wrapf(err, "conn.Delete(%s)", requiredViableUsersKeyRedis)
  133. return
  134. }
  135. return
  136. }
  137. // SetItemRedis ...
  138. func (d *Dao) SetItemRedis(c context.Context, key string, value interface{}, ttl int) (err error) {
  139. var (
  140. conn = d.redis.Get(c)
  141. bs []byte
  142. )
  143. defer conn.Close()
  144. if bs, err = json.Marshal(value); err != nil {
  145. return errors.WithStack(err)
  146. }
  147. if ttl == 0 {
  148. if _, err = conn.Do("SET", key, bs); err != nil {
  149. return errors.Wrapf(err, "conn.Do(SET %s, %s) error(%v)", key, value, err)
  150. }
  151. } else {
  152. if _, err = conn.Do("SET", key, bs, "EX", ttl); err != nil {
  153. return errors.Wrapf(err, "conn.Do(SET %s, %s) error(%v)", key, value, err)
  154. }
  155. }
  156. return
  157. }
  158. // ItemRedis ...
  159. func (d *Dao) ItemRedis(c context.Context, key string, value interface{}) (err error) {
  160. var (
  161. conn = d.redis.Get(c)
  162. bs []byte
  163. )
  164. defer conn.Close()
  165. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  166. if err == redis.ErrNil {
  167. return
  168. }
  169. return errors.Wrapf(err, "conn.Get(%s)", key)
  170. }
  171. if err = json.Unmarshal(bs, &value); err != nil {
  172. return errors.WithStack(err)
  173. }
  174. return
  175. }