Forráskód Böngészése

fix:redis支持集群

Roc 1 éve
szülő
commit
365cf7f6a1
2 módosított fájl, 180 hozzáadás és 34 törlés
  1. 86 11
      utils/redis/cluster_redis.go
  2. 94 23
      utils/redis/standalone_redis.go

+ 86 - 11
utils/redis/cluster_redis.go

@@ -11,13 +11,18 @@ import (
 )
 
 // ClusterRedisClient
-// @Description:
+// @Description: 集群的redis客户端
 type ClusterRedisClient struct {
 	redisClient *redis.ClusterClient
 }
 
 var DefaultKey = "zcmRedis"
 
+// InitClusterRedis
+// @Description: 初始化集群redis客户端
+// @param config
+// @return clusterRedisClient
+// @return err
 func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
 	var cf map[string]string
 	err = json.Unmarshal([]byte(config), &cf)
@@ -67,7 +72,11 @@ func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, er
 	return
 }
 
-// Get cache from redis.
+// Get
+// @Description: 根据key获取数据(其实是返回的字节编码)
+// @receiver rc
+// @param key
+// @return interface{}
 func (rc *ClusterRedisClient) Get(key string) interface{} {
 	data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
 	if err != nil {
@@ -77,25 +86,49 @@ func (rc *ClusterRedisClient) Get(key string) interface{} {
 	return data
 }
 
+// RedisBytes
+// @Description: 根据key获取字节编码数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
 func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
 
 	return
 }
 
+// RedisString
+// @Description: 根据key获取字符串数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
 func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Result()
 
 	return
 }
 
+// RedisInt
+// @Description: 根据key获取int数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
 func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Int()
 
 	return
 }
 
-// Put put cache to redis.
+// Put
+// @Description: put一个数据到redis
+// @receiver rc
+// @param key
+// @param val
+// @param timeout
+// @return error
 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()
@@ -108,6 +141,13 @@ func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Dura
 	return err
 }
 
+// SetNX
+// @Description: 设置一个会过期时间的值
+// @receiver rc
+// @param key
+// @param val
+// @param timeout
+// @return bool
 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" {
@@ -117,7 +157,11 @@ func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Du
 	return true
 }
 
-// Delete delete cache in redis.
+// Delete
+// @Description: 删除redis中的键值对
+// @receiver rc
+// @param key
+// @return error
 func (rc *ClusterRedisClient) Delete(key string) error {
 	var err error
 
@@ -131,7 +175,11 @@ func (rc *ClusterRedisClient) Delete(key string) error {
 	return err
 }
 
-// IsExist check cache's existence in redis.
+// IsExist
+// @Description: 根据key判断是否写入缓存中
+// @receiver rc
+// @param key
+// @return bool
 func (rc *ClusterRedisClient) IsExist(key string) bool {
 	result, err := rc.redisClient.Exists(context.TODO(), key).Result()
 	if err != nil {
@@ -146,7 +194,12 @@ func (rc *ClusterRedisClient) IsExist(key string) bool {
 	return true
 }
 
-// Put put cache to redis.
+// LPush
+// @Description: 写入list
+// @receiver rc
+// @param key
+// @param val
+// @return error
 func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
 	data, _ := json.Marshal(val)
 	err := rc.redisClient.LPush(context.TODO(), key, data).Err()
@@ -154,6 +207,11 @@ func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
 	return err
 }
 
+// Brpop
+// @Description: 从list中读取
+// @receiver rc
+// @param key
+// @param callback
 func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
 	values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
 	if err != nil {
@@ -168,6 +226,11 @@ func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
 
 }
 
+// GetRedisTTL
+// @Description: 获取key的过期时间
+// @receiver rc
+// @param key
+// @return time.Duration
 func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
 	value, err := rc.redisClient.TTL(context.TODO(), key).Result()
 	if err != nil {
@@ -178,14 +241,26 @@ func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
 
 }
 
-// Decr decrease counter in redis.
+// Incrby
+// @Description: 设置自增值
+// @receiver rc
+// @param key
+// @param num
+// @return interface{}
+// @return error
 func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
 	return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
 }
 
-// actually do the redis cmds
+// Do
+// @Description: cmd执行redis命令
+// @receiver rc
+// @param commandName
+// @param args
+// @return reply
+// @return err
 func (rc *ClusterRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
-	newArgs := make([]interface{}, 0)
-	newArgs = append(newArgs, commandName, args)
-	return rc.redisClient.Do(context.TODO(), commandName, newArgs).Result()
+	newArgs := []interface{}{commandName}
+	newArgs = append(newArgs, args...)
+	return rc.redisClient.Do(context.TODO(), newArgs...).Result()
 }

+ 94 - 23
utils/redis/standalone_redis.go

@@ -64,8 +64,12 @@ func InitStandaloneRedis(config string) (standaloneRedis StandaloneRedisClient,
 	return
 }
 
-// Get cache from redis.
-func (rc StandaloneRedisClient) Get(key string) interface{} {
+// Get
+// @Description: 根据key获取数据(其实是返回的字节编码)
+// @receiver rc
+// @param key
+// @return interface{}
+func (rc *StandaloneRedisClient) Get(key string) interface{} {
 	data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
 	if err != nil {
 		return nil
@@ -74,26 +78,50 @@ func (rc StandaloneRedisClient) Get(key string) interface{} {
 	return data
 }
 
-func (rc StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
+// RedisBytes
+// @Description: 根据key获取字节编码数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
+func (rc *StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
 
 	return
 }
 
-func (rc StandaloneRedisClient) RedisString(key string) (data string, err error) {
+// RedisString
+// @Description: 根据key获取字符串数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
+func (rc *StandaloneRedisClient) RedisString(key string) (data string, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Result()
 
 	return
 }
 
-func (rc StandaloneRedisClient) RedisInt(key string) (data int, err error) {
+// RedisInt
+// @Description: 根据key获取int数据
+// @receiver rc
+// @param key
+// @return data
+// @return err
+func (rc *StandaloneRedisClient) RedisInt(key string) (data int, err error) {
 	data, err = rc.redisClient.Get(context.TODO(), key).Int()
 
 	return
 }
 
-// Put put cache to redis.
-func (rc StandaloneRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
+// Put
+// @Description: put一个数据到redis
+// @receiver rc
+// @param key
+// @param val
+// @param timeout
+// @return error
+func (rc *StandaloneRedisClient) 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 {
@@ -105,7 +133,14 @@ func (rc StandaloneRedisClient) Put(key string, val interface{}, timeout time.Du
 	return err
 }
 
-func (rc StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
+// SetNX
+// @Description: 设置一个会过期时间的值
+// @receiver rc
+// @param key
+// @param val
+// @param timeout
+// @return bool
+func (rc *StandaloneRedisClient) 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
@@ -114,8 +149,12 @@ func (rc StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.
 	return true
 }
 
-// Delete delete cache in redis.
-func (rc StandaloneRedisClient) Delete(key string) error {
+// Delete
+// @Description: 删除redis中的键值对
+// @receiver rc
+// @param key
+// @return error
+func (rc *StandaloneRedisClient) Delete(key string) error {
 	var err error
 
 	err = rc.redisClient.Del(context.TODO(), key).Err()
@@ -128,8 +167,12 @@ func (rc StandaloneRedisClient) Delete(key string) error {
 	return err
 }
 
-// IsExist check cache's existence in redis.
-func (rc StandaloneRedisClient) IsExist(key string) bool {
+// IsExist
+// @Description: 根据key判断是否写入缓存中
+// @receiver rc
+// @param key
+// @return bool
+func (rc *StandaloneRedisClient) IsExist(key string) bool {
 	result, err := rc.redisClient.Exists(context.TODO(), key).Result()
 	if err != nil {
 		return false
@@ -143,15 +186,25 @@ func (rc StandaloneRedisClient) IsExist(key string) bool {
 	return true
 }
 
-// Put put cache to redis.
-func (rc StandaloneRedisClient) LPush(key string, val interface{}) error {
+// LPush
+// @Description: 写入list
+// @receiver rc
+// @param key
+// @param val
+// @return error
+func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
 	data, _ := json.Marshal(val)
 	err := rc.redisClient.LPush(context.TODO(), key, data).Err()
 
 	return err
 }
 
-func (rc StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
+// Brpop
+// @Description: 从list中读取
+// @receiver rc
+// @param key
+// @param callback
+func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
 	values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
 	if err != nil {
 		return
@@ -162,9 +215,15 @@ func (rc StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
 	}
 
 	callback([]byte(values[1]))
+
 }
 
-func (rc StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
+// GetRedisTTL
+// @Description: 获取key的过期时间
+// @receiver rc
+// @param key
+// @return time.Duration
+func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
 	value, err := rc.redisClient.TTL(context.TODO(), key).Result()
 	if err != nil {
 		return 0
@@ -174,14 +233,26 @@ func (rc StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
 
 }
 
-// Decr decrease counter in redis.
-func (rc StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
+// Incrby
+// @Description: 设置自增值
+// @receiver rc
+// @param key
+// @param num
+// @return interface{}
+// @return error
+func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
 	return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
 }
 
-// actually do the redis cmds
-func (rc StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
-	newArgs := make([]interface{}, 0)
-	newArgs = append(newArgs, commandName, args)
-	return rc.redisClient.Do(context.TODO(), commandName, newArgs).Result()
+// Do
+// @Description: cmd执行redis命令
+// @receiver rc
+// @param commandName
+// @param args
+// @return reply
+// @return err
+func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
+	newArgs := []interface{}{commandName}
+	newArgs = append(newArgs, args...)
+	return rc.redisClient.Do(context.TODO(), newArgs...).Result()
 }