|
@@ -11,13 +11,18 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
// ClusterRedisClient
|
|
// ClusterRedisClient
|
|
-// @Description:
|
|
|
|
|
|
+// @Description: 集群的redis客户端
|
|
type ClusterRedisClient struct {
|
|
type ClusterRedisClient struct {
|
|
redisClient *redis.ClusterClient
|
|
redisClient *redis.ClusterClient
|
|
}
|
|
}
|
|
|
|
|
|
var DefaultKey = "zcmRedis"
|
|
var DefaultKey = "zcmRedis"
|
|
|
|
|
|
|
|
+// InitClusterRedis
|
|
|
|
+// @Description: 初始化集群redis客户端
|
|
|
|
+// @param config
|
|
|
|
+// @return clusterRedisClient
|
|
|
|
+// @return err
|
|
func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
|
|
func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
|
|
var cf map[string]string
|
|
var cf map[string]string
|
|
err = json.Unmarshal([]byte(config), &cf)
|
|
err = json.Unmarshal([]byte(config), &cf)
|
|
@@ -67,7 +72,11 @@ func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, er
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-// Get cache from redis.
|
|
|
|
|
|
+// Get
|
|
|
|
+// @Description: 根据key获取数据(其实是返回的字节编码)
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @return interface{}
|
|
func (rc *ClusterRedisClient) Get(key string) interface{} {
|
|
func (rc *ClusterRedisClient) Get(key string) interface{} {
|
|
data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -77,25 +86,49 @@ func (rc *ClusterRedisClient) Get(key string) interface{} {
|
|
return data
|
|
return data
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// RedisBytes
|
|
|
|
+// @Description: 根据key获取字节编码数据
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @return data
|
|
|
|
+// @return err
|
|
func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
|
|
func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
|
|
|
|
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// RedisString
|
|
|
|
+// @Description: 根据key获取字符串数据
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @return data
|
|
|
|
+// @return err
|
|
func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
|
|
func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Result()
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Result()
|
|
|
|
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// RedisInt
|
|
|
|
+// @Description: 根据key获取int数据
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @return data
|
|
|
|
+// @return err
|
|
func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
|
|
func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Int()
|
|
data, err = rc.redisClient.Get(context.TODO(), key).Int()
|
|
|
|
|
|
return
|
|
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 {
|
|
func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
|
|
var err error
|
|
var err error
|
|
err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
|
|
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
|
|
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 {
|
|
func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
|
|
result, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
|
|
result, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
|
|
if err != nil || result != "OK" {
|
|
if err != nil || result != "OK" {
|
|
@@ -117,7 +157,11 @@ func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Du
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
|
|
|
|
-// Delete delete cache in redis.
|
|
|
|
|
|
+// Delete
|
|
|
|
+// @Description: 删除redis中的键值对
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @return error
|
|
func (rc *ClusterRedisClient) Delete(key string) error {
|
|
func (rc *ClusterRedisClient) Delete(key string) error {
|
|
var err error
|
|
var err error
|
|
|
|
|
|
@@ -131,7 +175,11 @@ func (rc *ClusterRedisClient) Delete(key string) error {
|
|
return err
|
|
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 {
|
|
func (rc *ClusterRedisClient) IsExist(key string) bool {
|
|
result, err := rc.redisClient.Exists(context.TODO(), key).Result()
|
|
result, err := rc.redisClient.Exists(context.TODO(), key).Result()
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -146,7 +194,12 @@ func (rc *ClusterRedisClient) IsExist(key string) bool {
|
|
return true
|
|
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 {
|
|
func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
|
|
data, _ := json.Marshal(val)
|
|
data, _ := json.Marshal(val)
|
|
err := rc.redisClient.LPush(context.TODO(), key, data).Err()
|
|
err := rc.redisClient.LPush(context.TODO(), key, data).Err()
|
|
@@ -154,6 +207,11 @@ func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Brpop
|
|
|
|
+// @Description: 从list中读取
|
|
|
|
+// @receiver rc
|
|
|
|
+// @param key
|
|
|
|
+// @param callback
|
|
func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
|
|
func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
|
|
values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
|
|
values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
|
|
if err != nil {
|
|
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 {
|
|
func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
|
|
value, err := rc.redisClient.TTL(context.TODO(), key).Result()
|
|
value, err := rc.redisClient.TTL(context.TODO(), key).Result()
|
|
if err != nil {
|
|
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) {
|
|
func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
|
|
return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
|
|
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) {
|
|
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()
|
|
}
|
|
}
|