redis_storage

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const RedisConnectionManagerName = "redis-connection-manager"
View Source
const RedisStorageName = "redis-storage"

Variables

View Source
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

func (*RedisConnectionManager) Return

func (x *RedisConnectionManager) Return(ctx context.Context, connection *redis.Client) error

func (*RedisConnectionManager) Shutdown

func (x *RedisConnectionManager) Shutdown(ctx context.Context) error

func (*RedisConnectionManager) Take

Take 获取 Redis 客户端。

漏洞4 修复:原 sync.Once 在连接失败时永久缓存 err,无法重试。 改为 mutex + initialized:成功后缓存 client 不重复 Connect;失败不缓存 err 可重试。

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) Get

func (x *RedisStorage) Get(ctx context.Context, lockId string) (string, error)

Get 获取锁信息

func (*RedisStorage) GetName

func (x *RedisStorage) GetName() string

func (*RedisStorage) GetTime

func (x *RedisStorage) GetTime(ctx context.Context) (time.Time, error)

GetTime 获取 Redis 服务端时间 使用 Redis TIME 命令获取服务端时间,满足 CapabilityReliableTime

func (*RedisStorage) Init

func (x *RedisStorage) Init(ctx context.Context) error

func (*RedisStorage) List

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

Jump to

Keyboard shortcuts

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