社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
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 //删除失败
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!