zredislock

package module
v0.0.0-...-e250062 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

zredislock

redis的并发锁

获得zredislock

go get -u github.com/zlyuancn/zredislock

使用zredislock

// 创建redis客户端
client := redis.NewClient(&redis.Options{
    Addr:     "127.0.0.1:6379",
})
defer client.Close()

// 创建锁生成器
zl := zredislock.New(client)

// 获取一个锁
lock, err := zl.Obtain("lock", 300, 0)
if err != nil {
    panic(err)
}
defer lock.Release() // 别忘记释放锁

// 自动刷新
lock.AutoRefresh(300)

// 模拟程序处理
time.Sleep(5e9)

Documentation

Index

Constants

View Source
const (
	DefaultObtainLoopWait = 0.1e9
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New(client redis.UniversalClient) *Client

func (*Client) Obtain

func (m *Client) Obtain(key string, ttl int64, timeout time.Duration) (*Lock, error)

type Lock

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

func Obtain

func Obtain(client *redis.Client, key string, ttl int64, timeout time.Duration) (*Lock, error)

func (*Lock) AutoRefresh

func (m *Lock) AutoRefresh(ttl int64) (cancel func())

自动刷新TTL(毫秒)

func (*Lock) Key

func (m *Lock) Key() string

func (*Lock) Release

func (m *Lock) Release() error

释放锁

func (*Lock) Token

func (m *Lock) Token() string

Jump to

Keyboard shortcuts

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