Documentation
¶
Overview ¶
Package redis 提供对 go-redis/v9 客户端的轻量封装与常用扩展接口。
NewRedis 创建基于 go-redis/v9 的客户端,并复用底层命令结果类型;返回的 Redis 实例持有底层连接资源, 调用方在不再使用时应调用 Close 释放连接。
RedisExtension 在基础接口上补充常用 KV 与过期操作;ScriptFlush 和 ScriptKill 会按底层实现暴露的能力分派, 当通过 NewRedisExtension 包装的底层实现未提供对应方法时返回 nil,调用方需要显式处理。
Index ¶
- Variables
- type ACLLogCmd
- type ACLLogEntry
- type BitCount
- type BoolCmd
- type BoolSliceCmd
- type ClientFlags
- type ClientInfo
- type ClientInfoCmd
- type ClusterLink
- type ClusterLinksCmd
- type ClusterNode
- type ClusterShard
- type ClusterShardsCmd
- type ClusterSlot
- type ClusterSlotsCmd
- type Cmd
- type Cmder
- type CommandInfo
- type CommandsInfoCmd
- type DurationCmd
- type Engine
- type FilterBy
- type FloatCmd
- type FloatSliceCmd
- type Function
- type FunctionListCmd
- type FunctionListQuery
- type FunctionStats
- type FunctionStatsCmd
- type GeoLocation
- type GeoLocationCmd
- type GeoPos
- type GeoPosCmd
- type GeoRadiusQuery
- type GeoSearchLocationCmd
- type GeoSearchLocationQuery
- type GeoSearchQuery
- type GeoSearchStoreQuery
- type IntCmd
- type IntSliceCmd
- type KeyFlags
- type KeyFlagsCmd
- type KeyValue
- type KeyValueSliceCmd
- type KeyValuesCmd
- type LCSCmd
- type LCSMatch
- type LCSMatchedPosition
- type LCSPosition
- type LCSQuery
- type LPosArgs
- type Library
- type MapStringIntCmd
- type MapStringInterfaceCmd
- type MapStringStringCmd
- type MapStringStringSliceCmd
- type Message
- type ModuleLoadexConfig
- type Node
- type Option
- type Pipeliner
- type Pong
- type PubSub
- type RankScore
- type RankWithScoreCmd
- type Redis
- type RedisExtension
- type RunningScript
- type ScanCmd
- type Scripter
- type SetArgs
- type SliceCmd
- type SlotRange
- type SlowLog
- type SlowLogCmd
- type Sort
- type StatusCmd
- type StringCmd
- type StringSliceCmd
- type StringStructMapCmd
- type Subscription
- type TimeCmd
- type XAddArgs
- type XAutoClaimCmd
- type XAutoClaimJustIDCmd
- type XClaimArgs
- type XInfoConsumer
- type XInfoConsumersCmd
- type XInfoGroup
- type XInfoGroupsCmd
- type XInfoStream
- type XInfoStreamCmd
- type XInfoStreamConsumer
- type XInfoStreamConsumerPending
- type XInfoStreamFull
- type XInfoStreamFullCmd
- type XInfoStreamGroup
- type XInfoStreamGroupPending
- type XMessage
- type XMessageSliceCmd
- type XPending
- type XPendingCmd
- type XPendingExt
- type XPendingExtArgs
- type XPendingExtCmd
- type XReadArgs
- type XReadGroupArgs
- type XStream
- type XStreamSliceCmd
- type Z
- type ZAddArgs
- type ZRangeArgs
- type ZRangeBy
- type ZSliceCmd
- type ZSliceWithKeyCmd
- type ZStore
- type ZWithKey
- type ZWithKeyCmd
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNil 表示 Redis 命令返回 nil 结果。 // // 该错误是 go-redis/v9 的 redis.Nil 别名,常见于 GET 等命令访问不存在的键。 // 调用方可以使用 errors.Is 与 ErrNil 或 redis.Nil 判断该场景。 ErrNil = redis.Nil // ErrClosed 表示对已关闭的 Redis 客户端执行操作。 // // 该错误是 go-redis/v9 的 redis.ErrClosed 别名,调用方可以据此区分连接生命周期错误。 ErrClosed = redis.ErrClosed // TxFailedErr 表示 Redis 事务监视条件不满足导致事务执行失败。 // // 该错误是 go-redis/v9 的 redis.TxFailedErr 别名,通常由 WATCH 相关事务在提交阶段返回。 TxFailedErr = redis.TxFailedErr //nolint:errname )
Functions ¶
This section is empty.
Types ¶
type ClusterLinksCmd ¶
type ClusterLinksCmd = redis.ClusterLinksCmd
ClusterLinksCmd 表示返回集群链接信息的 Redis 命令。
type ClusterShardsCmd ¶
type ClusterShardsCmd = redis.ClusterShardsCmd
ClusterShardsCmd 表示返回集群分片信息的 Redis 命令。
type ClusterSlotsCmd ¶
type ClusterSlotsCmd = redis.ClusterSlotsCmd
ClusterSlotsCmd 表示返回集群槽位信息的 Redis 命令。
type CommandsInfoCmd ¶
type CommandsInfoCmd = redis.CommandsInfoCmd
CommandsInfoCmd 表示返回命令信息的 Redis 命令。
type FunctionListCmd ¶
type FunctionListCmd = redis.FunctionListCmd
FunctionListCmd 表示返回函数列表的 Redis 命令。
type FunctionListQuery ¶
type FunctionListQuery = redis.FunctionListQuery
FunctionListQuery 表示函数列表查询参数类型。
type FunctionStatsCmd ¶
type FunctionStatsCmd = redis.FunctionStatsCmd
FunctionStatsCmd 表示返回函数统计信息的 Redis 命令。
type GeoLocationCmd ¶
type GeoLocationCmd = redis.GeoLocationCmd
GeoLocationCmd 表示返回地理位置信息的 Redis 命令。
type GeoSearchLocationCmd ¶
type GeoSearchLocationCmd = redis.GeoSearchLocationCmd
GeoSearchLocationCmd 表示返回地理位置搜索结果的 Redis 命令。
type GeoSearchLocationQuery ¶
type GeoSearchLocationQuery = redis.GeoSearchLocationQuery
GeoSearchLocationQuery 表示地理位置搜索位置查询类型。
type GeoSearchStoreQuery ¶
type GeoSearchStoreQuery = redis.GeoSearchStoreQuery
GeoSearchStoreQuery 表示地理位置搜索存储查询类型。
type KeyValueSliceCmd ¶
type KeyValueSliceCmd = redis.KeyValueSliceCmd
KeyValueSliceCmd 表示返回键值对切片类型的 Redis 命令。
type LCSMatchedPosition ¶
type LCSMatchedPosition = redis.LCSMatchedPosition
LCSMatchedPosition 表示最长公共子序列匹配位置类型。
type MapStringIntCmd ¶
type MapStringIntCmd = redis.MapStringIntCmd
MapStringIntCmd 表示返回字符串到整数映射类型的 Redis 命令。
type MapStringInterfaceCmd ¶
type MapStringInterfaceCmd = redis.MapStringInterfaceCmd
MapStringInterfaceCmd 表示返回字符串到接口映射类型的 Redis 命令。
type MapStringStringCmd ¶
type MapStringStringCmd = redis.MapStringStringSliceCmd
MapStringStringCmd 表示返回字符串到字符串映射切片类型的 Redis 命令。
type MapStringStringSliceCmd ¶
type MapStringStringSliceCmd = redis.MapStringStringSliceCmd
MapStringStringSliceCmd 表示返回字符串到字符串切片映射类型的 Redis 命令。
type ModuleLoadexConfig ¶
type ModuleLoadexConfig = redis.ModuleLoadexConfig
ModuleLoadexConfig 表示模块加载配置类型。
type Option ¶
type Option func(*redisClient)
Option 定义 NewRedis 的函数式配置项。
参数:
- *redisClient: 待修改的 Redis 客户端配置实例,调用方不应直接传入 nil。
func WithAddr ¶
WithAddr 设置 Redis 服务器地址。
参数:
- addr: Redis 服务器地址,格式通常为 "host:port"。
返回:
- Option: 应用于 NewRedis 的地址配置项。
func WithPassword ¶
WithPassword 设置 Redis 服务器认证密码。
参数:
- password: Redis 服务器认证密码;为空字符串表示不发送密码。
返回:
- Option: 应用于 NewRedis 的密码配置项。
type RankWithScoreCmd ¶
type RankWithScoreCmd = redis.RankWithScoreCmd
RankWithScoreCmd 表示返回带分数的排名的 Redis 命令。
type Redis ¶
type Redis interface {
Scripter
// Do 执行任意 Redis 命令。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
// - args: 命令名称及其参数,按 Redis 协议顺序传递。
//
// 返回:
// - *Cmd: 通用命令结果;执行错误由返回值的 Err 方法承载。
Do(ctx context.Context, args ...interface{}) *Cmd
// Pipelined 在管道中执行多个命令。
//
// 参数:
// - ctx: 控制管道执行生命周期的上下文。
// - fn: 向管道追加命令的回调;返回错误会中止管道执行。
//
// 返回:
// - []Cmder: 管道内各命令的执行结果,顺序与追加顺序一致;调用方仍应检查各 Cmder.Err。
// - error: fn 返回错误、上下文取消、网络异常或管道执行失败时返回错误。
Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)
// TxPipelined 在 Redis 事务管道中执行多个命令。
//
// 参数:
// - ctx: 控制事务管道执行生命周期的上下文。
// - fn: 向事务管道追加命令的回调;返回错误会中止事务管道执行。
//
// 返回:
// - []Cmder: 事务管道内各命令的执行结果,顺序与追加顺序一致;调用方仍应检查各 Cmder.Err。
// - error: fn 返回错误、上下文取消、网络异常或事务管道执行失败时返回错误。
TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)
// Subscribe 订阅一个或多个 Redis 频道。
//
// 参数:
// - ctx: 控制订阅创建过程的上下文。
// - channels: 要订阅的频道名称列表。
//
// 返回:
// - *PubSub: 发布订阅客户端;调用方在不再接收消息时应关闭该客户端。
Subscribe(ctx context.Context, channels ...string) *PubSub
// PSubscribe 按模式订阅一个或多个 Redis 频道。
//
// 参数:
// - ctx: 控制订阅创建过程的上下文。
// - channels: 要订阅的频道模式列表。
//
// 返回:
// - *PubSub: 发布订阅客户端;调用方在不再接收消息时应关闭该客户端。
PSubscribe(ctx context.Context, channels ...string) *PubSub
// Close 关闭 Redis 客户端并释放底层连接资源。
//
// 参数:无。
//
// 返回:
// - error: 底层 go-redis 客户端关闭失败时返回错误。
Close() error
}
Redis 定义本包暴露的 Redis 客户端基础能力。
Redis 复用 go-redis/v9 的命令结果类型,单命令执行错误通常保存在返回的 Cmd 对象中; 管道和事务管道还会通过返回的 error 承载回调、上下文或执行错误,调用方仍应检查各 Cmder.Err。Redis 实例持有底层连接资源,不再使用时应调用 Close。
type RedisExtension ¶
type RedisExtension interface {
Redis
// Get 获取指定键的值。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
// - key: 要读取的 Redis 键名。
//
// 返回:
// - *Cmd: GET 命令结果;键不存在时 Err 通常为 ErrNil。
Get(ctx context.Context, key string) *Cmd
// Set 设置指定键的值和可选过期时间。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
// - key: 要写入的 Redis 键名。
// - value: 要写入的值,编码规则由底层 go-redis 客户端决定。
// - expiration: 键过期时间;非正值表示不设置过期时间,整秒使用 EX,非整秒向上取整为毫秒并使用 PX。
//
// 返回:
// - *Cmd: SET 命令结果;执行错误由返回值的 Err 方法承载。
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *Cmd
// Del 删除指定键。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
// - key: 要删除的 Redis 键名。
//
// 返回:
// - *Cmd: DEL 命令结果;执行错误由返回值的 Err 方法承载。
Del(ctx context.Context, key string) *Cmd
// Expire 设置指定键的过期时间。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
// - key: 要设置过期语义的 Redis 键名。
// - expiration: 键过期时间;正整秒使用 EXPIRE,正非整秒向上取整为毫秒并使用 PEXPIRE,非正值使用 EXPIRE 0。
//
// 返回:
// - *Cmd: 过期命令结果;执行错误由返回值的 Err 方法承载。
Expire(ctx context.Context, key string, expiration time.Duration) *Cmd
// ScriptFlush 按底层客户端能力清空脚本缓存。
//
// 当底层 Redis 实现提供 ScriptFlush(context.Context) *StatusCmd 时委托调用;否则返回 nil,调用方应处理 nil 返回值。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
//
// 返回:
// - *StatusCmd: 底层支持脚本缓存管理时返回对应命令;否则返回 nil。
ScriptFlush(ctx context.Context) *StatusCmd
// ScriptKill 按底层客户端能力终止当前正在执行的脚本。
//
// 当底层 Redis 实现提供 ScriptKill(context.Context) *StatusCmd 时委托调用;否则返回 nil,调用方应处理 nil 返回值。
//
// 参数:
// - ctx: 控制命令执行生命周期的上下文。
//
// 返回:
// - *StatusCmd: 底层支持脚本终止能力时返回对应命令;否则返回 nil。
ScriptKill(ctx context.Context) *StatusCmd
}
RedisExtension 定义在 Redis 基础能力之上的常用扩展操作。
RedisExtension 通过 Do 组合 GET、SET、DEL 和过期命令,并保留 Redis 的脚本、管道和订阅能力。 扩展方法返回的命令对象遵循 go-redis/v9 约定,执行错误由命令结果的 Err 方法承载。
func NewRedisExtension ¶
func NewRedisExtension(redis Redis) RedisExtension
NewRedisExtension 创建一个 Redis 扩展实例。
参数:
- redis: 被包装的基础 Redis 实现;调用方应传入非 nil 实例。
返回:
- RedisExtension: 基于 redis 转发基础命令并补充常用 KV 操作的扩展实例。
type StringSliceCmd ¶
type StringSliceCmd = redis.StringSliceCmd
StringSliceCmd 表示返回字符串切片类型的 Redis 命令。
type StringStructMapCmd ¶
type StringStructMapCmd = redis.StringStructMapCmd
StringStructMapCmd 表示返回字符串到结构体映射类型的 Redis 命令。
type XAutoClaimJustIDCmd ¶
type XAutoClaimJustIDCmd = redis.XAutoClaimJustIDCmd
XAutoClaimJustIDCmd 表示仅返回自动认领消息 ID 的 Redis 命令。
type XInfoConsumersCmd ¶
type XInfoConsumersCmd = redis.XInfoConsumersCmd
XInfoConsumersCmd 表示返回消费者信息的 Redis 命令。
type XInfoGroupsCmd ¶
type XInfoGroupsCmd = redis.XInfoGroupsCmd
XInfoGroupsCmd 表示返回消费者组信息的 Redis 命令。
type XInfoStreamCmd ¶
type XInfoStreamCmd = redis.XInfoStreamCmd
XInfoStreamCmd 表示返回 Stream 信息的 Redis 命令。
type XInfoStreamConsumer ¶
type XInfoStreamConsumer = redis.XInfoStreamConsumer
XInfoStreamConsumer 表示 Stream 消费者信息类型。
type XInfoStreamConsumerPending ¶
type XInfoStreamConsumerPending = redis.XInfoStreamConsumerPending
XInfoStreamConsumerPending 表示 Stream 消费者待处理消息信息类型。
type XInfoStreamFull ¶
type XInfoStreamFull = redis.XInfoStreamFull
XInfoStreamFull 表示完整 Stream 信息类型。
type XInfoStreamFullCmd ¶
type XInfoStreamFullCmd = redis.XInfoStreamFullCmd
XInfoStreamFullCmd 表示返回完整 Stream 信息的 Redis 命令。
type XInfoStreamGroup ¶
type XInfoStreamGroup = redis.XInfoStreamGroup
XInfoStreamGroup 表示 Stream 组信息类型。
type XInfoStreamGroupPending ¶
type XInfoStreamGroupPending = redis.XInfoStreamGroupPending
XInfoStreamGroupPending 表示 Stream 组待处理消息信息类型。
type XMessageSliceCmd ¶
type XMessageSliceCmd = redis.XMessageSliceCmd
XMessageSliceCmd 表示返回 Stream 消息切片类型的 Redis 命令。
type XPendingExtArgs ¶
type XPendingExtArgs = redis.XPendingExtArgs
XPendingExtArgs 表示扩展待处理消息查询参数类型。
type XPendingExtCmd ¶
type XPendingExtCmd = redis.XPendingExtCmd
XPendingExtCmd 表示返回扩展待处理消息类型的 Redis 命令。
type XReadGroupArgs ¶
type XReadGroupArgs = redis.XReadGroupArgs
XReadGroupArgs 表示 Stream 消费者组读取消息的参数类型。
type XStreamSliceCmd ¶
type XStreamSliceCmd = redis.XStreamSliceCmd
XStreamSliceCmd 表示返回 Stream 切片类型的 Redis 命令。
type ZSliceWithKeyCmd ¶
type ZSliceWithKeyCmd = redis.ZSliceWithKeyCmd
ZSliceWithKeyCmd 表示返回带键的有序集合切片类型的 Redis 命令。