golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

109 lines
2.4 KiB

  1. package util
  2. import (
  3. "encoding/json"
  4. "gim/internal/business/comm/utils"
  5. "gim/pkg/logger"
  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. }