Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

31
app/tool/saga/dao/lock.go Normal file
View File

@@ -0,0 +1,31 @@
package dao
import (
"context"
"go-common/library/cache/redis"
"go-common/library/log"
)
//TryLock ...
func (d *Dao) TryLock(c context.Context, key string, value string, timeout int) (ok bool, err error) {
var conn = d.redis.Get(c)
defer conn.Close()
_, err = redis.String(conn.Do("SET", key, value, "EX", timeout, "NX"))
if err == redis.ErrNil {
log.Info("TryLock redis key(%s) is ErrNil!", key)
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// UnLock ...
func (d *Dao) UnLock(c context.Context, key string) (err error) {
var conn = d.redis.Get(c)
defer conn.Close()
_, err = conn.Do("DEL", key)
return
}