Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

redis.go 2.4 KiB

1 semana atrás
6 dias atrás
1 semana atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package util
  2. import (
  3. "egg-im/internal/business/comm/utils"
  4. "egg-im/pkg/logger"
  5. "encoding/json"
  6. "time"
  7. "github.com/go-redis/redis"
  8. jsoniter "github.com/json-iterator/go"
  9. )
  10. type RedisUtil struct {
  11. client *redis.Client
  12. }
  13. func NewRedisUtil(client *redis.Client) *RedisUtil {
  14. return &RedisUtil{client: client}
  15. }
  16. // Set 将指定值设置到redis中,使用json的序列化方式
  17. func (u *RedisUtil) Set(key string, value interface{}, duration time.Duration) error {
  18. bytes, err := jsoniter.Marshal(value)
  19. if err != nil {
  20. logger.Sugar.Error(err)
  21. return err
  22. }
  23. err = u.client.Set(key, bytes, duration).Err()
  24. if err != nil {
  25. logger.Sugar.Error(err)
  26. return err
  27. }
  28. return nil
  29. }
  30. // Get 从redis中读取指定值,使用json的反序列化方式
  31. func (u *RedisUtil) Get(key string, value interface{}) error {
  32. bytes, err := u.client.Get(key).Bytes()
  33. if err != nil {
  34. return err
  35. }
  36. err = jsoniter.Unmarshal(bytes, value)
  37. if err != nil {
  38. logger.Sugar.Error(err)
  39. return err
  40. }
  41. return nil
  42. }
  43. func (u *RedisUtil) HGetString(key string, HKey string) (string, error) {
  44. return u.client.Do("HGET", key, HKey).String()
  45. }
  46. func (u *RedisUtil) Exists(key string) bool {
  47. count, err := u.client.Do("EXISTS", key).Int()
  48. if count == 0 || err != nil {
  49. return false
  50. }
  51. return true
  52. }
  53. func (u *RedisUtil) HSet(key string, HKey string, data interface{}) (interface{}, error) {
  54. return u.client.HSet(key, HKey, data).Result()
  55. }
  56. func (u *RedisUtil) Expire(key string, ttl int) (interface{}, error) {
  57. return u.client.Do("EXPIRE", key, ttl).Result()
  58. }
  59. func (u *RedisUtil) Del(key string) (interface{}, error) {
  60. return u.client.Do("DEL", key).Result()
  61. }
  62. func (u *RedisUtil) Do(args ...interface{}) (reply interface{}, err error) {
  63. return u.client.Do(args...).String()
  64. }
  65. func (u *RedisUtil) SetEx(key string, data interface{}, ttl int) (reply interface{}, err error) {
  66. return u.client.Do("SETEX", key, ttl, data).Result()
  67. }
  68. func (u *RedisUtil) GetJson(key string, dst interface{}) error {
  69. b, err := utils.Bytes(u.client.Do("GET", key).Result())
  70. if err != nil {
  71. return err
  72. }
  73. if err = json.Unmarshal(b, dst); err != nil {
  74. return err
  75. }
  76. return nil
  77. }
  78. func (u *RedisUtil) SetJson(key string, data interface{}, ttl int) bool {
  79. c, err := json.Marshal(data)
  80. if err != nil {
  81. return false
  82. }
  83. if ttl < 1 {
  84. err = u.Set(key, c, time.Duration(ttl))
  85. } else {
  86. _, err = u.SetEx(key, c, ttl)
  87. }
  88. if err != nil {
  89. return false
  90. }
  91. return true
  92. }