redis.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/service/main/figure/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _figureKey = "f:%d"
  13. )
  14. func figureKey(mid int64) string {
  15. return fmt.Sprintf(_figureKey, mid)
  16. }
  17. // PingRedis check redis connection
  18. func (d *Dao) PingRedis(c context.Context) (err error) {
  19. conn := d.redis.Get(c)
  20. defer conn.Close()
  21. if _, err = conn.Do("SET", "PING", "PONG"); err != nil {
  22. err = errors.WithStack(err)
  23. }
  24. return
  25. }
  26. // AddFigureInfoCache put figure to redis
  27. func (d *Dao) AddFigureInfoCache(c context.Context, f *model.Figure) (err error) {
  28. var (
  29. key = figureKey(f.Mid)
  30. conn = d.redis.Get(c)
  31. values []byte
  32. )
  33. defer conn.Close()
  34. if values, err = json.Marshal(f); err != nil {
  35. err = errors.Wrapf(err, "%+v", f)
  36. return
  37. }
  38. if err = conn.Send("SET", key, values); err != nil {
  39. err = errors.Wrapf(err, "conn.Send(SET, %s, %d)", key, values)
  40. return
  41. }
  42. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  43. err = errors.Wrapf(err, "conn.Send(Expire, %s, %d)", key, d.redisExpire)
  44. return
  45. }
  46. return
  47. }
  48. // FigureInfoCache get user figure info from redis
  49. func (d *Dao) FigureInfoCache(c context.Context, mid int64) (f *model.Figure, err error) {
  50. key := figureKey(mid)
  51. conn := d.redis.Get(c)
  52. defer conn.Close()
  53. item, err := redis.Bytes(conn.Do("GET", key))
  54. if err != nil {
  55. if err == redis.ErrNil {
  56. err = nil
  57. }
  58. return
  59. }
  60. if err = json.Unmarshal(item, &f); err != nil {
  61. log.Error("json.Unmarshal(%v) err(%v)", item, err)
  62. }
  63. return
  64. }
  65. // FigureBatchInfoCache ...
  66. func (d *Dao) FigureBatchInfoCache(c context.Context, mids []int64) (fs []*model.Figure, missIndex []int, err error) {
  67. if len(mids) == 0 {
  68. return
  69. }
  70. fs = make([]*model.Figure, len(mids))
  71. var (
  72. conn = d.redis.Get(c)
  73. valueBytes [][]byte
  74. keys []interface{}
  75. )
  76. defer conn.Close()
  77. for _, mid := range mids {
  78. keys = append(keys, figureKey(mid))
  79. }
  80. if valueBytes, err = redis.ByteSlices(conn.Do("MGET", keys...)); err != nil {
  81. if err == redis.ErrNil {
  82. err = nil
  83. return
  84. }
  85. err = errors.WithStack(err)
  86. return
  87. }
  88. for i, value := range valueBytes {
  89. if value == nil {
  90. missIndex = append(missIndex, i)
  91. continue
  92. }
  93. f := &model.Figure{}
  94. if err = json.Unmarshal(value, &f); err != nil {
  95. log.Error("%+v", errors.Wrapf(err, "json.Unmarshal(%s)", value))
  96. err = nil
  97. missIndex = append(missIndex, i)
  98. continue
  99. }
  100. fs[i] = f
  101. }
  102. return
  103. }