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")
}()
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