redislock

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2021 License: MIT Imports: 1 Imported by: 0

README

redislock

Redis+lua distributed lock implementation based on redigo.

reference documents

http://redisdoc.com/string/set.html

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultExpire 加锁的key默认过期时间,单位s
	DefaultExpire = 10
)

Functions

This section is empty.

Types

type Lock

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

Lock lock data

func New

func New(conn redis.Conn, key string, token interface{}, expire ...int) *Lock

New 实例化redis分布式锁实例对象

func (*Lock) TryLock

func (lock *Lock) TryLock() (bool, error)

TryLock 尝试加锁,如果加锁成功就返回true,nil 利用redis setEx Nx的原子性实现分布式锁 SETEX 是一个原子(atomic)操作, 它可以在同一时间内完成设置值和设置过期时间这两个操作 当redis设置成功返回OK,所以这里需要判断数据是否存在以及结果是否是OK

func (*Lock) Unlock

func (lock *Lock) Unlock() error

Unlock 释放锁采用redis lua脚步执行,成功返回nil

Jump to

Keyboard shortcuts

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