|
@@ -0,0 +1,266 @@
|
|
|
+package redis
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/go-redis/redis/v8"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type ClusterRedisClient struct {
|
|
|
+ redisClient *redis.ClusterClient
|
|
|
+}
|
|
|
+
|
|
|
+var DefaultKey = "zcmRedis"
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
|
|
|
+ var cf map[string]string
|
|
|
+ err = json.Unmarshal([]byte(config), &cf)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ connList := make([]string, 0)
|
|
|
+ if _, ok := cf["conn"]; !ok {
|
|
|
+ err = errors.New("config has no conn key")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ connList = strings.Split(cf["conn"], ",")
|
|
|
+ if len(connList) <= 1 {
|
|
|
+ err = errors.New("conn address less than or equal to 1")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if _, ok := cf["password"]; !ok {
|
|
|
+ cf["password"] = ""
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ clusterOptions := &redis.ClusterOptions{
|
|
|
+ Addrs: connList,
|
|
|
+ Password: cf["password"],
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ client := redis.NewClusterClient(clusterOptions)
|
|
|
+
|
|
|
+
|
|
|
+ _, err = client.Ping(context.TODO()).Result()
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("redis 链接失败:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ clusterRedisClient = &ClusterRedisClient{redisClient: client}
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Get(key string) interface{} {
|
|
|
+ data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
|
|
|
+ data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
|
|
|
+ data, err = rc.redisClient.Get(context.TODO(), key).Result()
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
|
|
|
+ data, err = rc.redisClient.Get(context.TODO(), key).Int()
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
|
|
|
+ var err error
|
|
|
+ err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
|
|
|
+ result, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
|
|
|
+ if err != nil || result != "OK" {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Delete(key string) error {
|
|
|
+ var err error
|
|
|
+
|
|
|
+ err = rc.redisClient.Del(context.TODO(), key).Err()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) IsExist(key string) bool {
|
|
|
+ result, err := rc.redisClient.Exists(context.TODO(), key).Result()
|
|
|
+ if err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if result == 0 {
|
|
|
+ err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
|
|
|
+ if err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
|
|
|
+ data, _ := json.Marshal(val)
|
|
|
+ err := rc.redisClient.LPush(context.TODO(), key, data).Err()
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
|
|
|
+ values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(values) < 2 {
|
|
|
+ fmt.Println("assert is wrong")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ callback([]byte(values[1]))
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
|
|
|
+ value, err := rc.redisClient.TTL(context.TODO(), key).Result()
|
|
|
+ if err != nil {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
|
|
|
+ return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (rc *ClusterRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
|
|
+ newArgs := []interface{}{commandName}
|
|
|
+ newArgs = append(newArgs, args...)
|
|
|
+ return rc.redisClient.Do(context.TODO(), newArgs...).Result()
|
|
|
+}
|