golang redis 锁
const (
distributedLockExpireTime = 60 * time.Second
dLockRedisKey = "lock"
)
type dlock struct{}
func NewDistributedLock() *dlock {
return &dlock{}
}
func (d *dlock) GetDistributedLock(key string, expireTime time.Duration) (r bool) {
// true设置成功,false设置失败
r, _ = globalRedis.SetNX(key, dLockRedisKey, expireTime).Result()
return
}
func (d *dlock) ReleaseDistributedLock(key string) bool {
r, _ := globalRedis.Del(key).Result()
if r == 1 {
return false //删除成功
}
return true //删除失败
}