cluster_redis.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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) GetStr(key string) string {
  56. return rc.redisClient.Get(context.TODO(), key).Val()
  57. }
  58. func (rc *ClusterRedisClient) GetInt64(key string) (int64, error) {
  59. return rc.redisClient.Get(context.TODO(), key).Int64()
  60. }
  61. func (rc *ClusterRedisClient) GetUInt64(key string) (uint64, error) {
  62. return rc.redisClient.Get(context.TODO(), key).Uint64()
  63. }
  64. func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
  65. data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
  66. return
  67. }
  68. func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
  69. data, err = rc.redisClient.Get(context.TODO(), key).Result()
  70. return
  71. }
  72. func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
  73. data, err = rc.redisClient.Get(context.TODO(), key).Int()
  74. return
  75. }
  76. func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
  77. var err error
  78. err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
  79. if err != nil {
  80. return err
  81. }
  82. err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
  83. return err
  84. }
  85. func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
  86. result, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
  87. if err != nil || result != "OK" {
  88. return false
  89. }
  90. return true
  91. }
  92. func (rc *ClusterRedisClient) Delete(key string) error {
  93. var err error
  94. err = rc.redisClient.Del(context.TODO(), key).Err()
  95. if err != nil {
  96. return err
  97. }
  98. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  99. return err
  100. }
  101. func (rc *ClusterRedisClient) IsExist(key string) bool {
  102. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  103. if err != nil {
  104. return false
  105. }
  106. if result == 0 {
  107. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  108. return false
  109. }
  110. return true
  111. }
  112. func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
  113. data, _ := json.Marshal(val)
  114. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  115. return err
  116. }
  117. func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
  118. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  119. if err != nil {
  120. return
  121. }
  122. if len(values) < 2 {
  123. fmt.Println("assert is wrong")
  124. return
  125. }
  126. callback([]byte(values[1]))
  127. }
  128. func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
  129. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  130. if err != nil {
  131. return 0
  132. }
  133. return value
  134. }
  135. func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
  136. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  137. }
  138. func (rc *ClusterRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  139. newArgs := []interface{}{commandName}
  140. newArgs = append(newArgs, args...)
  141. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  142. }