go_dreamfactory/lego/sys/redis/cluster/string.go
2022-06-13 16:45:09 +08:00

126 lines
4.6 KiB
Go

package cluster
import (
"time"
"github.com/go-redis/redis/v8"
)
/* String *******************************************************************************/
/*
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。
*/
func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) {
return this.client.Set(this.getContext(), string(key), v, expiration).Result()
}
/*
指定的 key 不存在时,为 key 设置指定的值
*/
func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) {
return this.client.SetNX(this.getContext(), key, v, expiration).Result()
}
/*
同时设置一个或多个 key-value 对
*/
func (this *Redis) MSet(keyvalues map[string]interface{}) error {
agrs := make([]interface{}, 0)
agrs = append(agrs, "MSET")
for k, v := range keyvalues {
agrs = append(agrs, k, v)
}
return this.client.Do(this.getContext(), agrs...).Err()
}
/*
命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对
*/
func (this *Redis) MSetNX(keyvalues map[string]interface{}) error {
agrs := make([]interface{}, 0)
agrs = append(agrs, "MSETNX")
for k, v := range keyvalues {
agrs = append(agrs, k, v)
}
return this.client.Do(this.getContext(), agrs...).Err()
}
/*
Redis Incr 命令将 key 中储存的数字值增一。
如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。
如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
本操作的值限制在 64 位(bit)有符号数字表示之内。
*/
func (this *Redis) Incr(key string) (int64, error) {
return this.client.Incr(this.getContext(), key).Result()
}
/*
Redis Incrby 命令将 key 中储存的数字加上指定的增量值。
如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。
如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
本操作的值限制在 64 位(bit)有符号数字表示之内
*/
func (this *Redis) IncrBY(key string, value int64) (int64, error) {
return this.client.IncrBy(this.getContext(), key, value).Result()
}
/*
Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。
如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作
*/
func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) {
return this.client.IncrByFloat(this.getContext(), key, value).Result()
}
/*
Redis Decr 命令将 key 中储存的数字值减一。
如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。
如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
本操作的值限制在 64 位(bit)有符号数字表示之内
*/
func (this *Redis) Decr(key string, value int) (int64, error) {
return this.client.Decr(this.getContext(), key).Result()
}
/*
Redis Decrby 命令将 key 所储存的值减去指定的减量值。
如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。
如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
本操作的值限制在 64 位(bit)有符号数字表示之内
*/
func (this *Redis) DecrBy(key string, value int64) (int64, error) {
return this.client.DecrBy(this.getContext(), key, value).Result()
}
/*
Redis Append 命令用于为指定的 key 追加值。
如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。
*/
func (this *Redis) Append(key string, value string) (err error) {
this.client.Append(this.getContext(), key, value).Result()
return
}
/*
命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型
*/
func (this *Redis) Get(key string, v interface{}) (err error) {
return this.client.Get(this.getContext(), key).Scan(v)
}
/*
设置指定 key 的值,并返回 key 的旧值
*/
func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) {
return this.client.GetSet(this.getContext(), key, v).Scan(result)
}
/*
返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil
*/
func (this *Redis) MGet(keys ...string) *redis.SliceCmd {
return this.client.MGet(this.getContext(), keys...)
}