Documentation
¶
Overview ¶
Package redis 提供基于 Redis 的基建适配:分布式锁/选主(实现 pkg/dlock)与 带 TTL 的原子 KV 存储(实现 pkg/kvstore.Store,给 counter/cooldown/idempotency 等原语一个真实的跨实例后端)。薄封装 github.com/redis/go-redis,不重新发明算法。
锁语义是"单节点 Redis"级别:SET NX PX 抢占 + Lua CAS 释放/续期。这在单个 Redis (或主从,主挂了 failover 期间有极小的双持窗口)上正确、够用,但不是跨多个独立 Redis master 的 Redlock——如实标注这一点,不假装提供 Redlock 的强度。需要更强 保证时用 pkg/infra/etcd 或 pkg/infra/consul 后端。传入 ClusterClient 亦不改变 单节点语义(锁 key 落在单个 slot 上)。
Index ¶
- func NewClient(c *Config, opts ...Option) *redis.Client
- type Config
- type DLock
- type DLockOption
- type Option
- type Store
- func (s *Store) Delete(ctx context.Context, key string) error
- func (s *Store) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (s *Store) GetInt(ctx context.Context, key string) (int64, bool, error)
- func (s *Store) Incr(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
- func (s *Store) Set(ctx context.Context, key string, val []byte, ttl time.Duration) error
- func (s *Store) SetNX(ctx context.Context, key string, val []byte, ttl time.Duration) (bool, error)
- func (s *Store) TTL(ctx context.Context, key string) (time.Duration, bool, error)
- type StoreOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Addr string // host:port
Password string
DB int
PoolSize int // 连接池大小(0 用默认:10*GOMAXPROCS)
MinIdleConns int // 最小空闲连接
DialTimeout time.Duration // 建连超时
ReadTimeout time.Duration // 读超时
WriteTimeout time.Duration // 写超时
}
Config Redis 连接配置(单节点)。连接池/超时字段为可选,零值用 go-redis 默认。
type DLock ¶
type DLock struct {
// contains filtered or unexported fields
}
DLock 基于 Redis 实现 pkg/dlock 的 Locker 与 Elector。单节点语义见包注释。
Locker(Lock/TryLock)用固定 TTL 的 SET NX 抢占,不自动续期:持有超过 TTL 会 被动过期,长任务请设足够大的 TTL,或改用 Elector(它带自动续期,当选期间持续 持有,直到续期失败才让位)。这是单节点 Redis 锁的固有取舍,如实标注。
零值不可用,用 NewDLock / NewDLockFromConfig 构造。
func NewDLock ¶
func NewDLock(client redis.UniversalClient, opts ...DLockOption) *DLock
NewDLock 用已有 Redis 客户端创建 DLock。client 由调用方管理生命周期。
func NewDLockFromConfig ¶
func NewDLockFromConfig(c *Config, opts ...DLockOption) (*DLock, error)
NewDLockFromConfig 复用 Config 建连接(并 Ping 校验)后创建 DLock。
type DLockOption ¶
type DLockOption func(*DLock)
DLockOption 配置 DLock。
func WithKeyPrefix ¶
func WithKeyPrefix(prefix string) DLockOption
WithKeyPrefix 设置 key 前缀(默认 "beauty:dlock:"),隔离不同应用/环境。
func WithRetryInterval ¶
func WithRetryInterval(d time.Duration) DLockOption
WithRetryInterval 设置阻塞 Lock / 竞选的轮询间隔(默认 100ms)。
func WithTTL ¶
func WithTTL(ttl time.Duration) DLockOption
WithTTL 设置锁/leader key 的 TTL(默认 15s)。进程崩溃/卡死后,锁最长在此时长后 被动过期释放。Elector 会按 TTL/3 自动续期,故当选期间不受此上限约束。
type Option ¶
type Option func(*clientConfig)
Option 配置 NewClient(如接入 OTel 埋点)。
func WithMetrics ¶
func WithMetrics() Option
WithMetrics 给客户端接入 OTel 命令级 metrics(redisotel):命令耗时、连接池状态等, 用全局 MeterProvider(未配则 no-op)。
func WithTracing ¶
func WithTracing() Option
WithTracing 给客户端接入 OTel 链路追踪(redisotel):每条 redis 命令产生 span, 用 beauty telemetry 配好的全局 TracerProvider(未配则 no-op)。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 用 Redis 实现 pkg/kvstore.Store,给 counter/cooldown/idempotency 等原语一个 跨实例共享的后端(替代它们默认的单进程内存实现)。所有方法都是原子操作。
零值不可用,用 NewStore / NewStoreFromConfig 构造。
func NewStore ¶
func NewStore(client redis.UniversalClient, opts ...StoreOption) *Store
NewStore 用已有 Redis 客户端创建 Store。client 由调用方管理生命周期。
func NewStoreFromConfig ¶
func NewStoreFromConfig(c *Config, opts ...StoreOption) (*Store, error)
NewStoreFromConfig 复用 Config 建连接(并 Ping 校验)后创建 Store。
func (*Store) Incr ¶
func (s *Store) Incr(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
Incr 实现 kvstore.Store(INCRBY + 首次 PEXPIRE)。
type StoreOption ¶
type StoreOption func(*Store)
StoreOption 配置 Store。
func WithStoreKeyPrefix ¶
func WithStoreKeyPrefix(prefix string) StoreOption
WithStoreKeyPrefix 设置 key 前缀(默认无前缀,由各原语自行命名 key)。