distributed_lock

package
v0.0.0-...-157c77b Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultExpiration redis锁默认过期时间
	DefaultExpiration = 5 * time.Second
	// DefaultRetryDelayTimes 每次重新获取锁的默认间隔时间
	DefaultRetryDelayTimes = 100 * time.Millisecond
)
View Source
const (
	// RedisLockKeyPrefix redis锁的前缀
	RedisLockKeyPrefix = "REDIS_LOCK_PREFIX_"
	// RedisLockDefaultValue redis锁默认值
	RedisLockDefaultValue = "1"
)
View Source
const (
	NativeDefaultValue = "native"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DistributedLock

type DistributedLock interface {
	// TryLock 尝试获得锁
	// key : 锁的key
	// expireTime : 锁的过期时间
	// waitTime : 等待获取锁的超时时间
	TryLock(ctx context.Context, key string, expireTime, waitTime time.Duration) (bool, error)
	// UnLock 释放锁
	UnLock(ctx context.Context, key string) (bool, error)
	// RefreshLock 更新锁,存在则更新过期时间,不存在则创建key
	RefreshLock(ctx context.Context, key string, expireTime time.Duration) (bool, error)
}

type NativeLock

type NativeLock struct {
	// contains filtered or unexported fields
}

NativeLock 本地的锁,理论来说,这个锁只能按照指定的key锁住本机器上的功能,且没有自动的过期时间,需要手动释放

func NewNativeLock

func NewNativeLock() *NativeLock

func (*NativeLock) RefreshLock

func (n *NativeLock) RefreshLock(ctx context.Context, key string, expireTime time.Duration) (bool, error)

func (*NativeLock) TryLock

func (n *NativeLock) TryLock(_ context.Context, key string, _, waitTime time.Duration) (bool, error)

func (*NativeLock) UnLock

func (n *NativeLock) UnLock(_ context.Context, key string) (bool, error)

type RedisLock

type RedisLock struct {
	// contains filtered or unexported fields
}

func NewRedisLock

func NewRedisLock(client *redis.Client) *RedisLock

func (*RedisLock) RefreshLock

func (r *RedisLock) RefreshLock(ctx context.Context, key string, expireTime time.Duration) (bool, error)

func (*RedisLock) TryLock

func (r *RedisLock) TryLock(ctx context.Context, key string, expireTime, waitTime time.Duration) (bool, error)

func (*RedisLock) UnLock

func (r *RedisLock) UnLock(ctx context.Context, key string) (bool, error)

Jump to

Keyboard shortcuts

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