Documentation ¶
Overview ¶
Package gredis 提供了一个方便的 Redis 服务器客户端。
Redis 客户端。
Redis 官方命令参考: https://redis.io/commands
Redis 中文文档: http://redisdoc.com/
Index ¶
- Constants
- func ClearConfig()
- func RegisterAdapterFunc(adapterFunc AdapterFunc)
- func RemoveConfig(name ...string)
- func SetConfig(config *Config, name ...string)
- func SetConfigByMap(m map[string]interface{}, name ...string) error
- type Adapter
- type AdapterFunc
- type AdapterGroup
- type Config
- type Conn
- type ConnCommand
- type CopyOption
- type ExpireOption
- type FlushOp
- type GetEXOption
- type IGroupGeneric
- type IGroupHash
- type IGroupList
- type IGroupPubSub
- type IGroupScript
- type IGroupSet
- type IGroupSortedSet
- type IGroupString
- type LInsertOp
- type Message
- type Redis
- func (r *Redis) Close(ctx context.Context) error
- func (r *Redis) Conn(ctx context.Context) (Conn, error)
- func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*泛型类.Var, error)
- func (r *Redis) GetAdapter() Adapter
- func (r *Redis) MustConn(ctx context.Context) Conn
- func (r *Redis) MustDo(ctx context.Context, command string, args ...interface{}) *泛型类.Var
- func (r *Redis) SetAdapter(adapter Adapter)
- type ScriptFlushOption
- type SetOption
- type Subscription
- type TTLOption
- type ZAddMember
- type ZAddOption
- type ZRangeOption
- type ZRangeOptionLimit
- type ZRevRangeOption
Constants ¶
const (
DefaultGroupName = "default" // 默认配置组名称。
)
Variables ¶
This section is empty.
Functions ¶
func RegisterAdapterFunc ¶
func RegisterAdapterFunc(adapterFunc AdapterFunc)
RegisterAdapterFunc 注册一个默认的创建 redis 适配器的函数。
func RemoveConfig ¶
func RemoveConfig(name ...string)
RemoveConfig 删除指定组的全局配置。 如果未传入 `name`,则移除默认组名的配置。
func SetConfigByMap ¶
SetConfigByMap 通过map设置指定组的全局配置。 如果未传递 `name`,则设置默认组名的配置。
Types ¶
type Adapter ¶
type Adapter interface { AdapterGroup // 向服务器发送命令并返回接收到的回复。 // 在将结构体、切片或映射类型值提交到redis前,它使用json.Marshal进行序列化。 Do(ctx context.Context, command string, args ...interface{}) (*泛型类.Var, error) // Conn 获取并返回一个用于连续操作的连接对象。 // 注意,如果你不再使用此连接,应手动调用 Close 函数。 Conn(ctx context.Context) (conn Conn, err error) // Close 关闭当前的 Redis 客户端,关闭其连接池并释放所有相关的资源。 Close(ctx context.Context) (err error) }
Adapter 是一个用于通用 Redis 操作的接口。
type AdapterGroup ¶
type AdapterGroup interface { GroupGeneric() IGroupGeneric GroupHash() IGroupHash GroupList() IGroupList GroupPubSub() IGroupPubSub GroupScript() IGroupScript GroupSet() IGroupSet GroupSortedSet() IGroupSortedSet GroupString() IGroupString }
AdapterGroup 是一个接口,用于管理针对 Redis 的组操作。
type Config ¶
type Config struct { // Address 地址 支持单个和集群模式的Redis服务器。多个地址使用逗号 ',' 连接。例如:192.168.1.1:6379, 192.168.1.2:6379。 Address string `json:"address"` Db int `json:"db"` // Redis db. User string `json:"user"` // AUTH的用户名。 Pass string `json:"pass"` // AUTH的密码。 MinIdle int `json:"minIdle"` // 最小允许空闲的连接数(默认为0) MaxIdle int `json:"maxIdle"` // 最大允许空闲连接数(默认为10) MaxActive int `json:"maxActive"` // 连接数的最大限制(默认值为0,表示无限制)。 MaxConnLifetime time.Duration `json:"maxConnLifetime"` // 连接的最大生命周期(默认为30秒,不允许设置为0) IdleTimeout time.Duration `json:"idleTimeout"` // 连接的最大空闲时间(默认为10秒,不允许设置为0) WaitTimeout time.Duration `json:"waitTimeout"` // 从连接池获取连接时超时的持续时间。 DialTimeout time.Duration `json:"dialTimeout"` // 设置TCP连接的超时时间。 ReadTimeout time.Duration `json:"readTimeout"` // TCP读取超时时间。如果不是必需的,请勿设置。 WriteTimeout time.Duration `json:"writeTimeout"` // TCP写入超时时间 MasterName string `json:"masterName"` // 用于 Redis Sentinel 模式。 TLS bool `json:"tls"` // 指定在连接到服务器时是否应使用TLS(传输层安全协议)。 TLSSkipVerify bool `json:"tlsSkipVerify"` // 禁用通过TLS连接时的服务器名称验证。 TLSConfig *tls.Config `json:"-"` // TLS配置使用。当设置此配置时,将进行TLS协商。 SlaveOnly bool `json:"slaveOnly"` // 将所有命令路由到从节点(只读模式)。 Cluster bool `json:"cluster"` // 指定是否使用集群模式。 Protocol int `json:"protocol"` // 指定 RESP 协议版本(协议 2 或 3) }
Config 是 Redis 配置。
func ConfigFromMap ¶
ConfigFromMap 从给定的 map 中解析并返回配置。
type Conn ¶
type Conn interface { ConnCommand // 向服务器发送命令并返回接收到的回复。 // 在将结构体、切片或映射类型值提交到redis前,它使用json.Marshal进行序列化。 Do(ctx context.Context, command string, args ...interface{}) (result *泛型类.Var, err error) // Close将连接放回连接池。 Close(ctx context.Context) (err error) }
Conn 是一个通用 Redis 客户端连接的接口。
type ConnCommand ¶
type ConnCommand interface { // Subscribe 订阅函数,使客户端订阅指定的频道。 // 参考文档:https://redis.io/commands/subscribe/ Subscribe(ctx context.Context, channel string, channels ...string) ([]*Subscription, error) // PSubscribe 订阅客户端到给定的模式。 // // 支持的glob风格模式: // - h?llo 订阅hello, hallo和hxllo // - h*llo 订阅hllo和heeeello // - h[ae]llo 订阅hello和hallo,但不订阅hillo // // 如果你想精确匹配特殊字符,请使用\进行转义。 // // 参考文档:https://redis.io/commands/psubscribe/ PSubscribe(ctx context.Context, pattern string, patterns ...string) ([]*Subscription, error) // ReceiveMessage 从 Redis 服务器接收订阅的单条消息。 ReceiveMessage(ctx context.Context) (*Message, error) // Receive 从Redis服务器接收单个回复作为gvar.Var。 Receive(ctx context.Context) (result *泛型类.Var, err error) }
ConnCommand 是一个接口,用于管理与特定连接相关的一些操作。
type CopyOption ¶
type CopyOption struct { DB int // DB选项允许为目标键指定一个替代的逻辑数据库索引。 REPLACE bool // REPLACE 选项在复制值到目标键之前会先移除该目标键。 }
CopyOption 提供函数 Copy 的选项。
type ExpireOption ¶
type ExpireOption struct { NX bool // NX -- 当键未设置过期时间时,才设置过期时间 XX bool // XX -- 只在键已存在有效期时设置过期时间 GT bool // GT -- 当新的过期时间大于当前过期时间时,才设置过期时间 LT bool // LT -- 当新的过期时间小于当前过期时间时,才设置过期时间 }
ExpireOption 提供了用于 Expire 函数的选项。
type GetEXOption ¶
GetEXOption为GetEx函数提供额外的选项。
type IGroupGeneric ¶
type IGroupGeneric interface { Copy(ctx context.Context, source, destination string, option ...CopyOption) (int64, error) Exists(ctx context.Context, keys ...string) (int64, error) Type(ctx context.Context, key string) (string, error) Unlink(ctx context.Context, keys ...string) (int64, error) Rename(ctx context.Context, key, newKey string) error RenameNX(ctx context.Context, key, newKey string) (int64, error) Move(ctx context.Context, key string, db int) (int64, error) Del(ctx context.Context, keys ...string) (int64, error) RandomKey(ctx context.Context) (string, error) DBSize(ctx context.Context) (int64, error) Keys(ctx context.Context, pattern string) ([]string, error) FlushDB(ctx context.Context, option ...FlushOp) error FlushAll(ctx context.Context, option ...FlushOp) error Expire(ctx context.Context, key string, seconds int64, option ...ExpireOption) (int64, error) ExpireAt(ctx context.Context, key string, time time.Time, option ...ExpireOption) (int64, error) ExpireTime(ctx context.Context, key string) (*泛型类.Var, error) TTL(ctx context.Context, key string) (int64, error) Persist(ctx context.Context, key string) (int64, error) PExpire(ctx context.Context, key string, milliseconds int64, option ...ExpireOption) (int64, error) PExpireAt(ctx context.Context, key string, time time.Time, option ...ExpireOption) (int64, error) PExpireTime(ctx context.Context, key string) (*泛型类.Var, error) PTTL(ctx context.Context, key string) (int64, error) }
IGroupGeneric 管理通用的 Redis 操作。 实现请参考 redis.GroupGeneric。
type IGroupHash ¶
type IGroupHash interface { HSet(ctx context.Context, key string, fields map[string]interface{}) (int64, error) HSetNX(ctx context.Context, key, field string, value interface{}) (int64, error) HGet(ctx context.Context, key, field string) (*泛型类.Var, error) HStrLen(ctx context.Context, key, field string) (int64, error) HExists(ctx context.Context, key, field string) (int64, error) HDel(ctx context.Context, key string, fields ...string) (int64, error) HLen(ctx context.Context, key string) (int64, error) HIncrBy(ctx context.Context, key, field string, increment int64) (int64, error) HIncrByFloat(ctx context.Context, key, field string, increment float64) (float64, error) HMSet(ctx context.Context, key string, fields map[string]interface{}) error HMGet(ctx context.Context, key string, fields ...string) (泛型类.Vars, error) HKeys(ctx context.Context, key string) ([]string, error) HVals(ctx context.Context, key string) (泛型类.Vars, error) HGetAll(ctx context.Context, key string) (*泛型类.Var, error) }
IGroupHash 管理 Redis hash 操作。 实现细节参见 redis.GroupHash。
type IGroupList ¶
type IGroupList interface { LPush(ctx context.Context, key string, values ...interface{}) (int64, error) LPushX(ctx context.Context, key string, element interface{}, elements ...interface{}) (int64, error) RPush(ctx context.Context, key string, values ...interface{}) (int64, error) RPushX(ctx context.Context, key string, value interface{}) (int64, error) LPop(ctx context.Context, key string, count ...int) (*泛型类.Var, error) RPop(ctx context.Context, key string, count ...int) (*泛型类.Var, error) LRem(ctx context.Context, key string, count int64, value interface{}) (int64, error) LLen(ctx context.Context, key string) (int64, error) LIndex(ctx context.Context, key string, index int64) (*泛型类.Var, error) LInsert(ctx context.Context, key string, op LInsertOp, pivot, value interface{}) (int64, error) LSet(ctx context.Context, key string, index int64, value interface{}) (*泛型类.Var, error) LRange(ctx context.Context, key string, start, stop int64) (泛型类.Vars, error) LTrim(ctx context.Context, key string, start, stop int64) error BLPop(ctx context.Context, timeout int64, keys ...string) (泛型类.Vars, error) BRPop(ctx context.Context, timeout int64, keys ...string) (泛型类.Vars, error) RPopLPush(ctx context.Context, source, destination string) (*泛型类.Var, error) BRPopLPush(ctx context.Context, source, destination string, timeout int64) (*泛型类.Var, error) }
IGroupList 管理 Redis 列表操作。 实现细节参见 redis.GroupList。
type IGroupPubSub ¶
type IGroupPubSub interface { Publish(ctx context.Context, channel string, message interface{}) (int64, error) Subscribe(ctx context.Context, channel string, channels ...string) (Conn, []*Subscription, error) PSubscribe(ctx context.Context, pattern string, patterns ...string) (Conn, []*Subscription, error) }
IGroupPubSub 管理 Redis 发布/订阅操作。 实现请参见 redis.GroupPubSub。
type IGroupScript ¶
type IGroupScript interface { Eval(ctx context.Context, script string, numKeys int64, keys []string, args []interface{}) (*泛型类.Var, error) EvalSha(ctx context.Context, sha1 string, numKeys int64, keys []string, args []interface{}) (*泛型类.Var, error) ScriptLoad(ctx context.Context, script string) (string, error) ScriptExists(ctx context.Context, sha1 string, sha1s ...string) (map[string]bool, error) ScriptFlush(ctx context.Context, option ...ScriptFlushOption) error ScriptKill(ctx context.Context) error }
IGroupScript 管理 Redis 脚本操作。 实现细节参考 redis.GroupScript。
type IGroupSet ¶
type IGroupSet interface { SAdd(ctx context.Context, key string, member interface{}, members ...interface{}) (int64, error) SIsMember(ctx context.Context, key string, member interface{}) (int64, error) SPop(ctx context.Context, key string, count ...int) (*泛型类.Var, error) SRandMember(ctx context.Context, key string, count ...int) (*泛型类.Var, error) SRem(ctx context.Context, key string, member interface{}, members ...interface{}) (int64, error) SMove(ctx context.Context, source, destination string, member interface{}) (int64, error) SCard(ctx context.Context, key string) (int64, error) SMembers(ctx context.Context, key string) (泛型类.Vars, error) SMIsMember(ctx context.Context, key, member interface{}, members ...interface{}) ([]int, error) SInter(ctx context.Context, key string, keys ...string) (泛型类.Vars, error) SInterStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) SUnion(ctx context.Context, key string, keys ...string) (泛型类.Vars, error) SUnionStore(ctx context.Context, destination, key string, keys ...string) (int64, error) SDiff(ctx context.Context, key string, keys ...string) (泛型类.Vars, error) SDiffStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) }
IGroupSet 管理 Redis 集合操作。 实现请参考 redis.GroupSet。
type IGroupSortedSet ¶
type IGroupSortedSet interface { ZAdd(ctx context.Context, key string, option *ZAddOption, member ZAddMember, members ...ZAddMember) (*泛型类.Var, error) ZScore(ctx context.Context, key string, member interface{}) (float64, error) ZIncrBy(ctx context.Context, key string, increment float64, member interface{}) (float64, error) ZCard(ctx context.Context, key string) (int64, error) ZCount(ctx context.Context, key string, min, max string) (int64, error) ZRange(ctx context.Context, key string, start, stop int64, option ...ZRangeOption) (泛型类.Vars, error) ZRevRange(ctx context.Context, key string, start, stop int64, option ...ZRevRangeOption) (*泛型类.Var, error) ZRank(ctx context.Context, key string, member interface{}) (int64, error) ZRevRank(ctx context.Context, key string, member interface{}) (int64, error) ZRem(ctx context.Context, key string, member interface{}, members ...interface{}) (int64, error) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error) ZRemRangeByScore(ctx context.Context, key string, min, max string) (int64, error) ZRemRangeByLex(ctx context.Context, key string, min, max string) (int64, error) ZLexCount(ctx context.Context, key, min, max string) (int64, error) }
IGroupSortedSet 管理 Redis 有序集合操作。 实现参考 redis.GroupSortedSet。
type IGroupString ¶
type IGroupString interface { X设置值(ctx context.Context, key string, value interface{}, option ...SetOption) (*泛型类.Var, error) SetNX(ctx context.Context, key string, value interface{}) (bool, error) SetEX(ctx context.Context, key string, value interface{}, ttlInSeconds int64) error Get(ctx context.Context, key string) (*泛型类.Var, error) GetDel(ctx context.Context, key string) (*泛型类.Var, error) GetEX(ctx context.Context, key string, option ...GetEXOption) (*泛型类.Var, error) GetSet(ctx context.Context, key string, value interface{}) (*泛型类.Var, error) StrLen(ctx context.Context, key string) (int64, error) Append(ctx context.Context, key string, value string) (int64, error) SetRange(ctx context.Context, key string, offset int64, value string) (int64, error) GetRange(ctx context.Context, key string, start, end int64) (string, error) Incr(ctx context.Context, key string) (int64, error) IncrBy(ctx context.Context, key string, increment int64) (int64, error) IncrByFloat(ctx context.Context, key string, increment float64) (float64, error) Decr(ctx context.Context, key string) (int64, error) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) MSet(ctx context.Context, keyValueMap map[string]interface{}) error MSetNX(ctx context.Context, keyValueMap map[string]interface{}) (bool, error) MGet(ctx context.Context, keys ...string) (map[string]*泛型类.Var, error) }
IGroupString 管理 Redis 字符串操作。 实现细节请参考 redis.GroupString。
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis client.
func NewWithAdapter ¶
NewWithAdapter 根据给定的适配器创建并返回一个redis客户端。
func (*Redis) SetAdapter ¶
SetAdapter 更改当前Redis客户端的底层适配器,使用自定义适配器。
type ScriptFlushOption ¶
ScriptFlushOption 提供了函数 ScriptFlush 的选项。
type SetOption ¶
type SetOption struct { TTLOption NX bool // 如果键尚未存在,则设置该键。 XX bool // 只有在键已存在的时候才设置该键。 // 如果键存在,则返回该键存储的旧字符串,否则返回nil。 // 若键中存储的值不是字符串,则会返回错误并中止SET操作。 Get bool }
SetOption为Set函数提供额外的选项。
type Subscription ¶
type Subscription struct { Kind string // 可以是 "subscribe", "unsubscribe", "psubscribe" 或 "punsubscribe"。 Channel string // 我们已订阅的通道名称。 Count int // 当前我们已订阅的频道数量。 }
订阅成功后接收到的订阅信息(针对通道)
type TTLOption ¶
type TTLOption struct { EX *int64 // EX 秒数 -- 设置指定的过期时间,单位为秒。 PX *int64 // PX milliseconds -- 设置指定的过期时间,单位为毫秒。 EXAT *int64 // EXAT 时间戳-秒 -- 设置键在指定的 Unix 时间(单位:秒)时过期。 PXAT *int64 // PXAT 时间戳-毫秒 -- 设置键的过期时间,以毫秒为单位,指定 Unix 时间。 KeepTTL bool // 保留与键关联的生存时间(TTL)。 }
TTLOption 提供了与TTL相关函数的额外选项。
type ZAddMember ¶
type ZAddMember struct { Score float64 Member interface{} }
ZAddMember 是集合中元素的结构体。
type ZAddOption ¶
type ZAddOption struct { XX bool // Only update elements that already exist. Don't add new elements. NX bool // Only add new elements. Don't update already existing elements. // 只有当新分数小于当前分数时,才更新已存在的元素。 // 但请注意,此标志不会阻止添加新的元素。 LT bool // 如果新分数大于当前分数,则仅更新现有元素。 // 此标志不会阻止添加新元素。 GT bool // 将返回值由新增元素的数量修改为更改过的总元素数量(CH 是“changed”的缩写)。 // 更改过的元素包括新添加的元素以及已存在但分数被更新的元素。 // 因此,命令行中指定且其分数与过去相同的元素不会被计算在内。 // 注意:通常情况下,ZAdd 的返回值仅计算新增元素的数量。 CH bool // 当指定了此选项时,ZAdd 表现得如同 ZIncrBy。在这种模式下,只能指定一个分数-元素对。 INCR bool }
ZAddOption 提供了函数 ZAdd 的选项。
type ZRangeOption ¶
type ZRangeOption struct { ByScore bool ByLex bool // 可选参数 REV 用于反转排序顺序,因此元素按从高到低的分数进行排序, // 当分数相同时,采用反字典序进行排序结果的确定。 Rev bool Limit *ZRangeOptionLimit // 可选的 WithScores 参数会用返回元素的分数来补充命令的回复。 WithScores bool }
ZRangeOption 为 ZRange 函数提供了额外的选项。
type ZRangeOptionLimit ¶
ZRangeOptionLimit 为 ZRange 函数提供 LIMIT 参数。 可选的 LIMIT 参数可用于从匹配元素中获取一个子范围(类似于 SQL 中的 SELECT LIMIT offset, count)。 如果 `Count` 为负数,则返回从 `Offset` 开始的所有元素。
type ZRevRangeOption ¶
type ZRevRangeOption struct {
WithScores bool
}
ZRevRangeOption 提供了 ZRevRange 函数的选项。
Source Files ¶
- gredis.go
- gredis_adapter.go
- gredis_config.go
- gredis_instance.go
- gredis_redis.go
- gredis_redis_group_generic.go
- gredis_redis_group_hash.go
- gredis_redis_group_list.go
- gredis_redis_group_pubsub.go
- gredis_redis_group_script.go
- gredis_redis_group_set.go
- gredis_redis_group_sorted_set.go
- gredis_redis_group_string.go