Documentation
¶
Index ¶
- Constants
- Variables
- type RedisConnectionManager
- type RedisStorage
- func (x *RedisStorage) Capabilities() []storage.StorageCapability
- func (x *RedisStorage) Close(ctx context.Context) error
- func (x *RedisStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, ...) error
- func (x *RedisStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, ...) error
- func (x *RedisStorage) Get(ctx context.Context, lockId string) (string, error)
- func (x *RedisStorage) GetName() string
- func (x *RedisStorage) GetTime(ctx context.Context) (time.Time, error)
- func (x *RedisStorage) Init(ctx context.Context) error
- func (x *RedisStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
- func (x *RedisStorage) UpdateWithVersion(ctx context.Context, lockId string, ...) error
- func (x *RedisStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
- type RedisStorageOptions
Constants ¶
const RedisConnectionManagerName = "redis-connection-manager"
const RedisStorageName = "redis-storage"
Variables ¶
var ( // ErrKeyPrefixEmpty 参数中 KeyPrefix 为空 ErrKeyPrefixEmpty = fmt.Errorf("KeyPrefix can not empty") // ErrConnectionManagerNil 连接管理器没有指定 ErrConnectionManagerNil = fmt.Errorf("ConnectionManager can not nil") )
Functions ¶
This section is empty.
Types ¶
type RedisConnectionManager ¶
type RedisConnectionManager struct {
// 连接到 Redis 的地址
URI string
// Redis 客户端选项(与 URI 互斥,优先使用 Options)
Options *redis.Options
// contains filtered or unexported fields
}
RedisConnectionManager 负责维护与 Redis 的连接
func NewRedisConnectionManagerFromClient ¶
func NewRedisConnectionManagerFromClient(client *redis.Client) *RedisConnectionManager
NewRedisConnectionManagerFromClient 复用已经存在的 Redis 客户端,从其创建连接管理器
func NewRedisConnectionManagerFromOptions ¶
func NewRedisConnectionManagerFromOptions(options *redis.Options) *RedisConnectionManager
NewRedisConnectionManagerFromOptions 从 Redis 选项创建连接管理器
func NewRedisConnectionManagerFromURI ¶
func NewRedisConnectionManagerFromURI(uri string) *RedisConnectionManager
NewRedisConnectionManagerFromURI 从 Redis URI 创建连接管理器
func (*RedisConnectionManager) Name ¶
func (x *RedisConnectionManager) Name() string
type RedisStorage ¶
type RedisStorage struct {
// contains filtered or unexported fields
}
RedisStorage 基于 Redis 的存储引擎实现
func NewRedisStorage ¶
func NewRedisStorage(ctx context.Context, options *RedisStorageOptions) (*RedisStorage, error)
NewRedisStorage 创建一个基于 Redis 的存储引擎
func (*RedisStorage) Capabilities ¶
func (x *RedisStorage) Capabilities() []storage.StorageCapability
Capabilities 声明 Redis 存储支持的能力 Redis 通过 Lua 脚本实现原子 CAS,通过 TIME 命令提供服务端时间,Lua DEL 实现真删, 且支持 Keyspace Notifications 作为 Watch 能力。
func (*RedisStorage) Close ¶
func (x *RedisStorage) Close(ctx context.Context) error
Close 关闭存储引擎 连接由 ConnectionManager 管理,Storage 只清空引用
func (*RedisStorage) CreateWithVersion ¶
func (x *RedisStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error
CreateWithVersion 原子创建锁记录 Lua 脚本保证"检查 key 不存在 + 写入"是原子操作,不存在 Check-Then-Act 竞态
func (*RedisStorage) DeleteWithVersion ¶
func (x *RedisStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error
DeleteWithVersion 原子删除锁记录 Lua 脚本保证"读版本 + 删除"是原子操作,不存在 Check-Then-Act 竞态
func (*RedisStorage) GetName ¶
func (x *RedisStorage) GetName() string
func (*RedisStorage) GetTime ¶
GetTime 获取 Redis 服务端时间 使用 Redis TIME 命令获取服务端时间,满足 CapabilityReliableTime
func (*RedisStorage) List ¶
func (x *RedisStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
List 列出当前所有的锁信息 使用 SCAN 匹配 <KeyPrefix>:lock:* 模式 + HGETALL 收集 注意:SCAN 非原子快照,仅运维查看,不参与互斥性
func (*RedisStorage) UpdateWithVersion ¶
func (x *RedisStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error
UpdateWithVersion 原子更新锁记录 Lua 脚本保证"读版本 + 写"是原子操作,不存在 Check-Then-Act 竞态
func (*RedisStorage) Watch ¶
func (x *RedisStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
Watch 监听指定锁的状态变更。 首选基于 Redis Pub/Sub 的轻量通知通道;失败时回退为 Keyevent 通知。
type RedisStorageOptions ¶
type RedisStorageOptions struct {
// 获取连接
ConnectionManager storage.ConnectionManager[*redis.Client]
// KeyPrefix Redis 中 key 的前缀,用于隔离不同的锁实例
// 实际的 key 格式为:<KeyPrefix>:lock:<lockId>
KeyPrefix string
}
RedisStorageOptions Redis 的存储选项
func NewRedisStorageOptions ¶
func NewRedisStorageOptions() *RedisStorageOptions
func NewRedisStorageOptionsWithURI ¶
func NewRedisStorageOptionsWithURI(uri string) *RedisStorageOptions
func (*RedisStorageOptions) Check ¶
func (x *RedisStorageOptions) Check() error
func (*RedisStorageOptions) SetConnectionManager ¶
func (x *RedisStorageOptions) SetConnectionManager(connectionManager storage.ConnectionManager[*redis.Client]) *RedisStorageOptions
func (*RedisStorageOptions) SetKeyPrefix ¶
func (x *RedisStorageOptions) SetKeyPrefix(keyPrefix string) *RedisStorageOptions