redis

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EXSECONDS       = "EX"
	PXMILLISSECONDS = "PX"
	NOTEXISTS       = "NX"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

func (*Pipeline) Exec

func (p *Pipeline) Exec(ctx *gin.Context) (res []interface{}, err error)

func (*Pipeline) Put

func (p *Pipeline) Put(ctx *gin.Context, cmd string, args ...interface{}) error

type Pipeliner

type Pipeliner interface {
	Exec(ctx *gin.Context) ([]interface{}, error)
	Put(ctx *gin.Context, cmd string, args ...interface{}) error
}

type Redis

type Redis struct {
	Service string
	// contains filtered or unexported fields
}

日志打印Do args部分支持的最大长度

func InitRedisClient

func InitRedisClient(conf RedisConf) (*Redis, error)

func (*Redis) Append

func (r *Redis) Append(ctx *gin.Context, key string, value interface{}) (int, error)

func (*Redis) BLPop

func (r *Redis) BLPop(ctx *gin.Context, key string, timeout int64) ([][]byte, error)

当给定列表内没有任何元素可供弹出的时候,连接将被 BLPOP 命令阻塞,直到等待超时或发现可弹出元素为止。 timout单位为:秒 设置为0表示阻塞时间无限期延长 return: 一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值

func (*Redis) BRPop

func (r *Redis) BRPop(ctx *gin.Context, key string, timeout int64) ([][]byte, error)

当给定列表内没有任何元素可供弹出的时候,连接将被 BRPOP 命令阻塞,直到等待超时或发现可弹出元素为止。 timout单位为:秒 设置为0表示阻塞时间无限期延长 return: 一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值

func (*Redis) BRPopLPush

func (r *Redis) BRPopLPush(ctx *gin.Context, sourceKey string, destKey string, timeout int64) ([][]byte, error)

timeout单位为:秒 设置为0表示阻塞时间无限期延长

func (*Redis) Close

func (r *Redis) Close() error

func (*Redis) Decr

func (r *Redis) Decr(ctx *gin.Context, key string) (int64, error)

func (*Redis) DecrBy

func (r *Redis) DecrBy(ctx *gin.Context, key string, value int64) (int64, error)

func (*Redis) Del

func (r *Redis) Del(ctx *gin.Context, keys ...interface{}) (int64, error)

func (*Redis) Do

func (r *Redis) Do(ctx *gin.Context, commandName string, args ...interface{}) (reply interface{}, err error)

func (*Redis) Exists

func (r *Redis) Exists(ctx *gin.Context, key string) (bool, error)

func (*Redis) Expire

func (r *Redis) Expire(ctx *gin.Context, key string, time int64) (bool, error)

func (*Redis) Get

func (r *Redis) Get(ctx *gin.Context, key string) ([]byte, error)

func (*Redis) HDel

func (r *Redis) HDel(ctx *gin.Context, key string, fields ...string) (int64, error)

func (*Redis) HExists

func (r *Redis) HExists(ctx *gin.Context, key string, field string) (bool, error)

func (*Redis) HGet

func (r *Redis) HGet(ctx *gin.Context, key, field string) ([]byte, error)

func (*Redis) HGetAll

func (r *Redis) HGetAll(ctx *gin.Context, key string) ([][]byte, error)

func (*Redis) HIncrBy

func (r *Redis) HIncrBy(ctx *gin.Context, key, field string, value int64) (int64, error)

func (*Redis) HKeys

func (r *Redis) HKeys(ctx *gin.Context, key string) ([][]byte, error)

func (*Redis) HLen

func (r *Redis) HLen(ctx *gin.Context, key string) (int64, error)

func (*Redis) HMGet

func (r *Redis) HMGet(ctx *gin.Context, key string, fields ...string) ([][]byte, error)

func (*Redis) HMSet

func (r *Redis) HMSet(ctx *gin.Context, key string, fvmap map[string]interface{}) error

HMSet 将一个map存到Redis hash

func (*Redis) HScan

func (r *Redis) HScan(ctx *gin.Context, key string, cursor uint64, pattern string, count int) (uint64, map[string][]byte, error)

基于游标的迭代器,每次被调用会返回新的游标,在下次迭代时,需要使用这个新游标作为游标参数,以此来延续之前的迭代过程 param: key param: cursor 游标 传""表示开始新迭代 param: count 每次迭代返回元素的最大值,limit hint,实际数量并不准确=count param: pattern 模式参数,符合glob风格 ? (一个字符) * (任意个字符) [] (匹配其中的任意一个字符) \x (转义字符) return: 新的cursor,filed-value map 当返回"",空map时,表示迭代已结束

func (*Redis) HSet

func (r *Redis) HSet(ctx *gin.Context, key, field string, val interface{}) (int, error)

func (*Redis) HVals

func (r *Redis) HVals(ctx *gin.Context, key string) ([][]byte, error)

func (*Redis) Incr

func (r *Redis) Incr(ctx *gin.Context, key string) (int64, error)

func (*Redis) IncrBy

func (r *Redis) IncrBy(ctx *gin.Context, key string, value int64) (int64, error)

func (*Redis) IncrByFloat

func (r *Redis) IncrByFloat(ctx *gin.Context, key string, value float64) (float64, error)

func (*Redis) LIndex

func (r *Redis) LIndex(ctx *gin.Context, key string, index int) ([]byte, error)

返回列表 key 中,下标为 index 的元素

func (*Redis) LInsert

func (r *Redis) LInsert(ctx *gin.Context, key string, before bool, pivot interface{}, value interface{}) (int, error)

将值 value 插入到列表 key 当中,位于值 pivot 之前或之后

func (*Redis) LLen

func (r *Redis) LLen(ctx *gin.Context, key string) (int, error)

func (*Redis) LPop

func (r *Redis) LPop(ctx *gin.Context, key string) ([]byte, error)

移除并返回列表 key 的头元素 return: 列表的头元素,当 key 不存在时,返回 nil,nil

func (*Redis) LPush

func (r *Redis) LPush(ctx *gin.Context, key string, members ...interface{}) (int, error)

return: 执行命令后,list的长度

func (*Redis) LPushX

func (r *Redis) LPushX(ctx *gin.Context, key string, member interface{}) (int, error)

将值 value 插入到列表 key 的表头,当且仅当 key 存在并且是一个列表。 return: 命令执行后,list的长度

func (*Redis) LRange

func (r *Redis) LRange(ctx *gin.Context, key string, start int, stop int) ([][]byte, error)

返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。包含 stop 位置的元素

func (*Redis) LRem

func (r *Redis) LRem(ctx *gin.Context, key string, count int, value interface{}) (int, error)

根据参数 count 的值,移除列表中与参数 value 相等的元素 count 的值可以是以下几种:

count > 0 : 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
count < 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。
count = 0 : 移除表中所有与 value 相等的值。

return: 被移除元素的数量

func (*Redis) LSet

func (r *Redis) LSet(ctx *gin.Context, key string, index int, value interface{}) (bool, error)

将列表 key 下标为 index 的元素的值设置为 value

func (*Redis) LTrim

func (r *Redis) LTrim(ctx *gin.Context, key string, start int, stop int) (bool, error)

对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。

func (*Redis) Lua

func (r *Redis) Lua(ctx *gin.Context, script string, keyCount int, keysAndArgs ...interface{}) (interface{}, error)

func (*Redis) MGet

func (r *Redis) MGet(ctx *gin.Context, keys ...string) [][]byte

func (*Redis) MSet

func (r *Redis) MSet(ctx *gin.Context, values ...interface{}) error

func (*Redis) Pipeline

func (r *Redis) Pipeline() Pipeliner

func (*Redis) Pttl

func (r *Redis) Pttl(ctx *gin.Context, key string) (int64, error)

func (*Redis) RPop

func (r *Redis) RPop(ctx *gin.Context, key string) ([]byte, error)

func (*Redis) RPopLPush

func (r *Redis) RPopLPush(ctx *gin.Context, sourceKey string, destKey string) ([]byte, error)

将列表 source 中的最后一个元素(尾元素)弹出,并返回 将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素。

func (*Redis) RPush

func (r *Redis) RPush(ctx *gin.Context, key string, members ...interface{}) (int, error)

func (*Redis) RPushX

func (r *Redis) RPushX(ctx *gin.Context, key string, member interface{}) (int, error)

func (*Redis) SAdd

func (r *Redis) SAdd(ctx *gin.Context, key string, members ...string) (int64, error)

将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略 return: 被添加到集合中的新元素的数量,不包括被忽略的元素

func (*Redis) SCard

func (r *Redis) SCard(ctx *gin.Context, key string) (int64, error)

返回集合 key 的基数(集合中元素的数量)

func (*Redis) SDiff

func (r *Redis) SDiff(ctx *gin.Context, keys ...string) ([][]byte, error)

返回一个集合的全部成员,该集合是所有给定集合之间的差集

func (*Redis) SDiffStore

func (r *Redis) SDiffStore(ctx *gin.Context, dstKey string, keys ...string) (int64, error)

这个命令的作用和 SDIFF key [key …] 类似,但它将结果保存到 destination 集合,而不是简单地返回结果集

func (*Redis) SInter

func (r *Redis) SInter(ctx *gin.Context, keys ...string) ([][]byte, error)

返回一个集合的全部成员,该集合是所有给定集合的交集

func (*Redis) SInterStore

func (r *Redis) SInterStore(ctx *gin.Context, dstKey string, keys ...string) (int64, error)

这个命令类似于 SINTER key [key …] 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集

func (*Redis) SIsMember

func (r *Redis) SIsMember(ctx *gin.Context, key string, member string) (bool, error)

判断 member 元素是否集合 key 的成员

func (*Redis) SMembers

func (r *Redis) SMembers(ctx *gin.Context, key string) ([][]byte, error)

func (*Redis) SMove

func (r *Redis) SMove(ctx *gin.Context, source, destination, member string) (bool, error)

将 member 元素从 source 集合移动到 destination 集合。SMOVE 是原子性操作

func (*Redis) SPop

func (r *Redis) SPop(ctx *gin.Context, key string) ([]byte, error)

移除并返回集合中的一个随机元素

func (*Redis) SRandMember

func (r *Redis) SRandMember(ctx *gin.Context, key string) ([]byte, error)

如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素

func (*Redis) SRandMemberCount

func (r *Redis) SRandMemberCount(ctx *gin.Context, key string, count int) ([][]byte, error)

func (*Redis) SRem

func (r *Redis) SRem(ctx *gin.Context, key string, members ...string) (int64, error)

移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略 return: 被成功移除的元素的数量,不包括被忽略的元素

func (*Redis) SScan

func (r *Redis) SScan(ctx *gin.Context, key string, cursor uint64, pattern string, count int) (uint64, []string, error)

基于游标的迭代器,每次被调用会返回新的游标,在下次迭代时,需要使用这个新游标作为游标参数,以此来延续之前的迭代过程 param: key param: cursor 游标 传""表示开始新迭代 param: count 每次迭代返回元素的最大值,limit hint,实际数量并不准确=count param: pattern 模式参数,符合glob风格 ? (一个字符) * (任意个字符) [] (匹配其中的任意一个字符) \x (转义字符) return: 新的cursor,value[] 当返回"",空切片时,表示迭代已结束

func (*Redis) SUnion

func (r *Redis) SUnion(ctx *gin.Context, keys ...string) ([][]byte, error)

返回一个集合的全部成员,该集合是所有给定集合的并集

func (*Redis) SUnionStore

func (r *Redis) SUnionStore(ctx *gin.Context, dstKey string, keys ...string) (int64, error)

这个命令类似于 SUNION key [key …] 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集

func (*Redis) Set

func (r *Redis) Set(ctx *gin.Context, key string, value interface{}, expire ...int64) error

func (*Redis) SetEx

func (r *Redis) SetEx(ctx *gin.Context, key string, value interface{}, expire int64) error

func (*Redis) SetNxByEX

func (r *Redis) SetNxByEX(ctx *gin.Context, key string, value interface{}, expire uint64) (bool, error)

设置过期时间为秒级的redis分布式锁

func (*Redis) SetNxByPX

func (r *Redis) SetNxByPX(ctx *gin.Context, key string, value interface{}, expire uint64) (bool, error)

设置过期时间为毫秒的redis分布式锁

func (*Redis) Stats

func (r *Redis) Stats() (inUseCount, idleCount, activeCount int)

todo 这个方法之前只针对一个host返回对应的连接池

func (*Redis) Ttl

func (r *Redis) Ttl(ctx *gin.Context, key string) (int64, error)

func (*Redis) ZAdd

func (r *Redis) ZAdd(ctx *gin.Context, key string, maps map[string]float64) (int64, error)

将一个或多个 member 元素加入到有序集 key 当中,已经存在于集合的 member 元素将更新该元素的 score 值 param: maps Member-Score集合 return: 被添加到集合中的新元素的数量,不包括被更新的、已存在的元素

func (*Redis) ZCard

func (r *Redis) ZCard(ctx *gin.Context, key string) (int64, error)

返回有序集 key 的基数

func (*Redis) ZCount

func (r *Redis) ZCount(ctx *gin.Context, key, min, max string) (int64, error)

返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量

func (*Redis) ZIncrBy

func (r *Redis) ZIncrBy(ctx *gin.Context, key string, delta float64, member string) (float64, error)

为有序集 key 的成员 member 的 score 值加上增量 delta

func (*Redis) ZInterStore

func (r *Redis) ZInterStore(ctx *gin.Context, destination string, keys []string, weights []int, aggregate string) (int64, error)

计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。

func (*Redis) ZLexCount

func (r *Redis) ZLexCount(ctx *gin.Context, key, min, max string) (int64, error)

对于一个所有成员的分值都相同的有序集合键 key 来说, 这个命令会返回该集合中, 成员介于 min 和 max 范围内的元素数量。

func (*Redis) ZRange

func (r *Redis) ZRange(ctx *gin.Context, key string, start int, stop int, withscores bool) ([][]byte, error)

返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递增(从小到大)来排序 withscores指定是否返回得分 return: Score-Member集合

func (*Redis) ZRangeByScore

func (r *Redis) ZRangeByScore(ctx *gin.Context, key, min, max string, withscores, limit bool, offset int, count int) ([][]byte, error)

返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。 有序集成员按 score 值递增(从小到大)次序排列。 withscores指定是否返回得分 limit 是否分页方法,false返回所有的数据

func (*Redis) ZRank

func (r *Redis) ZRank(ctx *gin.Context, key string, member string) (int64, error)

返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列。 排名以 0 为底,也就是说, score 值最小的成员排名为 0 。

func (*Redis) ZRem

func (r *Redis) ZRem(ctx *gin.Context, key string, members ...string) (int64, error)

移除有序集 key 中的一个或多个成员,不存在的成员将被忽略 return: 被成功移除的成员的数量,不包括被忽略的成员

func (*Redis) ZRemRangeByLex

func (r *Redis) ZRemRangeByLex(ctx *gin.Context, key, min, max string) (int64, error)

对于一个所有成员的分值都相同的有序集合键 key 来说, 这个命令会移除该集合中, 成员介于 min 和 max 范围内的所有元素。 如:"key", "-inf", "(2"

func (*Redis) ZRemRangeByRank

func (r *Redis) ZRemRangeByRank(ctx *gin.Context, key string, start int, stop int) (int64, error)

移除有序集 key 中,指定排名(rank)区间内的所有成员。 区间分别以下标参数 start 和 stop 指出,包含 start 和 stop 在内。 return: 被移除成员的数量

func (*Redis) ZRemRangeByScore

func (r *Redis) ZRemRangeByScore(ctx *gin.Context, key, min, max string) (int64, error)

移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。 如:"key", "-inf", "(2" return: 被移除成员的数量

func (*Redis) ZRevRange

func (r *Redis) ZRevRange(ctx *gin.Context, key string, start int, stop int, withscores bool) ([][]byte, error)

返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递增(从大到小)来排序 return: Score-Member集合

func (*Redis) ZRevRangeByScore

func (r *Redis) ZRevRangeByScore(ctx *gin.Context, key, min, max string, withscores, limit bool, offset int, count int) ([][]byte, error)

返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从大到小)次序排列。 如:"key", "-inf", "(2"

func (*Redis) ZRevRank

func (r *Redis) ZRevRank(ctx *gin.Context, key string, member string) (int64, error)

返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从大到小)顺序排列。 排名以 0 为底,也就是说, score 值最小的成员排名为 0 。

func (*Redis) ZScan

func (r *Redis) ZScan(ctx *gin.Context, key string, cursor uint64, pattern string, count int) (uint64, []string, error)

基于游标的迭代器,每次被调用会返回新的游标,在下次迭代时,需要使用这个新游标作为游标参数,以此来延续之前的迭代过程 param: key param: cursor 游标 传""表示开始新迭代 param: count 每次迭代返回元素的最大值,limit hint,实际数量并不准确=count param: pattern 模式参数,符合glob风格 ? (一个字符) * (任意个字符) [] (匹配其中的任意一个字符) \x (转义字符) return: 新的cursor,score-member pair 当返回"",空map时,表示迭代已结束

func (*Redis) ZScore

func (r *Redis) ZScore(ctx *gin.Context, key string, member string) (string, error)

返回有序集 key 中,成员 member 的 score 值 return: score值,若该成员不存在,返回nil

func (*Redis) ZUnionStore

func (r *Redis) ZUnionStore(ctx *gin.Context, destination string, keys []string, weights []int, aggregate string) (int64, error)

ZUnionStore destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] 计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。

type RedisConf

type RedisConf struct {
	Service         string        `yaml:"service"`
	Addr            string        `yaml:"addr"`
	Password        string        `yaml:"password"`
	MaxIdle         int           `yaml:"maxIdle"`
	MaxActive       int           `yaml:"maxActive"`
	IdleTimeout     time.Duration `yaml:"idleTimeout"`
	MaxConnLifetime time.Duration `yaml:"maxConnLifetime"`
	ConnTimeOut     time.Duration `yaml:"connTimeOut"`
	ReadTimeOut     time.Duration `yaml:"readTimeOut"`
	WriteTimeOut    time.Duration `yaml:"writeTimeOut"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL