Skip to content

wathenjiang/redisLock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

1. Quick Start

Install redisLock:

go get github.com/Spongecaptain/redisLock

Create redis client:

import(
	"github.com/go-redis/redis"
)
var redisClient = redis.NewClient(&redis.Options{
	Addr:     "localhost:6379",
	Password: "", // no password set
	DB:       0,  // use default DB
})

Create redisLock:

key := "reids-lock-key"
value := "redis-lock-value"
lock := redisLock.NewRedisLock(redisClient, key, value)

err := lock.Lock()
if err != nil {
  fmt.Println(err.Error())
  return
}
fmt.Println("get redis lock success")
defer func() {
  err = lock.Unlock()
  if err != nil {
    fmt.Println(err.Error())
    return
  }
  fmt.Println("release redis lock success")
}()

2. Feature

redisLock supports the following features:

  • Implements Atomic by Lua scripts
  • Achieves efficient communication by Redis Pub/Sub
  • Avoid deadlock by Redis EXPIRE
  • Avoid concurrency issues by automatic renew

Here are the unsupported features:

  • Reentrant mutex is NOT supported, just like sync.mutex
  • Fairness of mutex is NOT supported, may cause starving problem in extreme cases

中文版

About

A toy dirtributed lock based on Redis.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages