redis-util.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package common
  2. import (
  3. "encoding/json"
  4. "github.com/go-redis/redis"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. type Redis struct {
  10. Addr string
  11. MaxConn int
  12. Db int
  13. IdleTimeOut int
  14. Client *redis.Client
  15. }
  16. type RedisConf struct {
  17. Addr string `json:"address"`
  18. PoolSize int `json:"poolSize"`
  19. DB int `json:"db"`
  20. }
  21. func InitRedis(conf RedisConf) *Redis {
  22. r := &Redis{Addr: conf.Addr, MaxConn: conf.PoolSize, Db: conf.DB}
  23. r.Init()
  24. return r
  25. }
  26. func (r *Redis) Init() {
  27. opt := &redis.Options{
  28. Addr: r.Addr,
  29. DB: r.Db,
  30. DialTimeout: 10 * time.Second,
  31. ReadTimeout: 300 * time.Second,
  32. WriteTimeout: 30 * time.Second,
  33. PoolSize: r.MaxConn,
  34. PoolTimeout: 30 * time.Second,
  35. IdleTimeout: time.Minute,
  36. IdleCheckFrequency: 5 * time.Second,
  37. }
  38. r.Client = redis.NewClient(opt)
  39. }
  40. func (r *Redis) Set(k string, v interface{}, timeout int64) bool {
  41. bt, _ := json.Marshal(v)
  42. cmd := r.Client.Set(k, bt, time.Duration(timeout)*time.Second)
  43. b, err := cmd.Result()
  44. if err != nil {
  45. g.Log().Errorf(gctx.New(), "redis Set err %s", err.Error())
  46. }
  47. return "OK" == b
  48. }
  49. func (r *Redis) Get(k string) interface{} {
  50. cmd := r.Client.Get(k)
  51. bt, _ := cmd.Bytes()
  52. if bt != nil && len(bt) > 0 {
  53. var res interface{}
  54. if err := json.Unmarshal(bt, &res); err != nil {
  55. g.Log().Errorf(gctx.New(), "redis Get Unmarshal err %v", err)
  56. }
  57. return res
  58. }
  59. return nil
  60. }
  61. func (r *Redis) GetBytes(k string) []byte {
  62. cmd := r.Client.Get(k)
  63. bt, err := cmd.Bytes()
  64. if err != nil {
  65. if err.Error() != "redis: nil" {
  66. g.Log().Errorf(gctx.New(), "redis GetBytes err %s", err.Error())
  67. }
  68. return nil
  69. }
  70. return bt
  71. }
  72. func (r *Redis) MGet(k ...string) []interface{} {
  73. cmd := r.Client.MGet(k...)
  74. arr, err := cmd.Result()
  75. if err != nil {
  76. g.Log().Errorf(gctx.New(), "redis MGet err %s", err.Error())
  77. }
  78. return arr
  79. }
  80. func (r *Redis) Incr(k string) int64 {
  81. cmd := r.Client.Incr(k)
  82. res, err := cmd.Result()
  83. if err != nil {
  84. g.Log().Errorf(gctx.New(), "redis Incr err %s", err.Error())
  85. }
  86. return res
  87. }
  88. func (r *Redis) Del(k string) bool {
  89. _, err := r.Client.Del(k).Result()
  90. if err != nil {
  91. g.Log().Errorf(gctx.New(), "redis Del err %s", err.Error())
  92. return false
  93. }
  94. return true
  95. }
  96. func (r *Redis) Exists(k string) bool {
  97. res, err := r.Client.Exists(k).Result()
  98. if err != nil {
  99. g.Log().Errorf(gctx.New(), "redis Exists err %s", err.Error())
  100. }
  101. return res == 1
  102. }
  103. func (r *Redis) IncrTimeout(k string, duration time.Duration) int64 {
  104. if r.Client.TTL(k).Val().Nanoseconds() < 0 {
  105. r.Client.Set(k, 1, duration)
  106. return 1
  107. } else {
  108. return r.Incr(k)
  109. }
  110. }
  111. func (r *Redis) GetTTL(key string) int64 {
  112. cmd := r.Client.TTL(key)
  113. t, err := cmd.Result()
  114. if err != nil {
  115. g.Log().Errorf(gctx.New(), "redis GetTTL err %s", err.Error())
  116. }
  117. if t.Nanoseconds() < 0 {
  118. return -1
  119. }
  120. return time.Unix(time.Now().Unix(), t.Nanoseconds()).Unix()
  121. }