Skip to content

Distributed mutual exclusion lock based on single redis instance

License

Notifications You must be signed in to change notification settings

maodanp/redis-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

redis-lock

redis-lock provides a distributed mutual exclusion lock implementation for Go, the lock is based on single redis instance, not based on the redis cluster.

It's very simple to use, but if you consider the mutual exclusion lock used in redis cluster, you need use Redsync.go.

Installation

install redis-lock using the go get command:

$ go get github.com/maodanp/redis-lock

The dependencies are the github.com/maodanp/go-log and github.com/garyburd/redigo/redis.

Example Usage

func simpleTest() {
client, err := NewRedisLock("127.0.0.1:6379", Config{})
defer client.Close()
	if err != nil {
			return
	}

if lock, err := client.Lock(); err != nil {
	log.Logger.Warnf("%d lock err %v", idx, err)
		return
	} else if lock {
	log.Logger.Infof("%d locked", idx)
		// time.Sleep mock the work which goroutine do
		time.Sleep(time.Second)
		client.UnLock()	
	}
}

License

kingshard is under the Apache 2.0 license.

About

Distributed mutual exclusion lock based on single redis instance

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages