cluster_redis.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "strings"
  9. "time"
  10. )
  11. type ClusterRedisClient struct {
  12. redisClient *redis.ClusterClient
  13. }
  14. var DefaultKey = "zcmRedis"
  15. func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
  16. var cf map[string]string
  17. err = json.Unmarshal([]byte(config), &cf)
  18. if err != nil {
  19. return
  20. }
  21. connList := make([]string, 0)
  22. if _, ok := cf["conn"]; !ok {
  23. err = errors.New("config has no conn key")
  24. return
  25. } else {
  26. connList = strings.Split(cf["conn"], ",")
  27. if len(connList) <= 1 {
  28. err = errors.New("conn address less than or equal to 1")
  29. return
  30. }
  31. }
  32. if _, ok := cf["password"]; !ok {
  33. cf["password"] = ""
  34. }
  35. clusterOptions := &redis.ClusterOptions{
  36. Addrs: connList, // 设置 Redis 节点的 IP 地址和端口号
  37. Password: cf["password"],
  38. }
  39. client := redis.NewClusterClient(clusterOptions)
  40. _, err = client.Ping(context.TODO()).Result()
  41. if err != nil {
  42. err = errors.New("redis 链接失败:" + err.Error())
  43. return
  44. }
  45. clusterRedisClient = &ClusterRedisClient{redisClient: client}
  46. return
  47. }
  48. func (rc *ClusterRedisClient) Get(key string) interface{} {
  49. data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
  50. if err != nil {
  51. return nil
  52. }
  53. return data
  54. }
  55. func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
  56. data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
  57. return
  58. }
  59. func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
  60. data, err = rc.redisClient.Get(context.TODO(), key).Result()
  61. return
  62. }
  63. func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
  64. data, err = rc.redisClient.Get(context.TODO(), key).Int()
  65. return
  66. }
  67. func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
  68. var err error
  69. err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
  70. if err != nil {
  71. return err
  72. }
  73. err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
  74. return err
  75. }
  76. func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
  77. result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
  78. if err != nil {
  79. return false
  80. }
  81. return result
  82. }
  83. func (rc *ClusterRedisClient) Delete(key string) error {
  84. var err error
  85. err = rc.redisClient.Del(context.TODO(), key).Err()
  86. if err != nil {
  87. return err
  88. }
  89. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  90. return err
  91. }
  92. func (rc *ClusterRedisClient) IsExist(key string) bool {
  93. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  94. if err != nil {
  95. return false
  96. }
  97. if result == 0 {
  98. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  99. return false
  100. }
  101. return true
  102. }
  103. func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
  104. data, _ := json.Marshal(val)
  105. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  106. return err
  107. }
  108. func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
  109. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  110. if err != nil {
  111. return
  112. }
  113. if len(values) < 2 {
  114. fmt.Println("assert is wrong")
  115. return
  116. }
  117. callback([]byte(values[1]))
  118. }
  119. func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
  120. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  121. if err != nil {
  122. return 0
  123. }
  124. return value
  125. }
  126. func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
  127. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  128. }
  129. func (rc *ClusterRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  130. newArgs := []interface{}{commandName}
  131. newArgs = append(newArgs, args...)
  132. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  133. }