|
@@ -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()
|
|
|
}
|