README ¶ go-redislock 介绍 使用go编写的基于Redis实现的分布式锁 快速开始 安装 go get -u github.com/fuliang10000/go-redislock 测试 go test github.com/fuliang10000/go-redislock -v -cover Use Demo package main import ( "context" "fmt" redisLock "github.com/fuliang10000/go-redislock" "github.com/go-redis/redis/v8" "time" ) func main() { var opt = &redis.Options{ Addr: "localhost:6379", Password: "a123456", DB: 0, } // 获取客户端 client := redisLock.NewClient(context.Background(), redis.NewClient(opt)) lockKey := "my_lock_key" // 获取锁 locked := client.Lock(lockKey, 10*time.Second) if locked { // 释放锁 defer client.UnLock(lockKey) // 执行业务逻辑 fmt.Println("my work...") } else { panic("system is busy") } } Expand ▾ Collapse ▴ Documentation ¶ Index ¶ type Client func NewClient(ctx context.Context, redis *redis.Client) *Client func (s *Client) Lock(lockKey string, expiration time.Duration) bool func (s *Client) UnLock(lockKey string) Constants ¶ This section is empty. Variables ¶ This section is empty. Functions ¶ This section is empty. Types ¶ type Client ¶ added in v0.0.5 type Client struct { Ctx context.Context // contains filtered or unexported fields } func NewClient ¶ added in v0.0.5 func NewClient(ctx context.Context, redis *redis.Client) *Client NewClient 获取客户端 func (*Client) Lock ¶ added in v0.0.5 func (s *Client) Lock(lockKey string, expiration time.Duration) bool Lock 获取锁 func (*Client) UnLock ¶ added in v0.0.5 func (s *Client) UnLock(lockKey string) UnLock 释放锁 Source Files ¶ View all Source files client.go Click to show internal directories. Click to hide internal directories.