- update redis lock
continuous-integration/drone/tag Build is failing Details
continuous-integration/drone/push Build was killed Details

master v1.0.25
李光春 2 years ago
parent 2f71ce5847
commit e8c2b726bf

@ -1,3 +1,3 @@
package dorm package dorm
const Version = "1.0.24" const Version = "1.0.25"

@ -1,12 +1,8 @@
package dorm package dorm
import (
"errors"
)
var ( var (
// RedisNotFound 没有数据 // RedisNotFound 没有数据
RedisNotFound = errors.New("redis: nil") RedisNotFound = "redis: nil"
// RedisKeysNotFound keys没有数据 // RedisKeysNotFound keys没有数据
RedisKeysNotFound = errors.New("ERR wrong number of arguments for 'mget' command") RedisKeysNotFound = "ERR wrong number of arguments for 'mget' command"
) )

@ -27,20 +27,18 @@ func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl
} }
// 获取 // 获取
get, err := rl.operation.Get(ctx, key).Result() get, err := rl.operation.Get(ctx, key).Result()
if err != nil { if err.Error() == RedisNotFound {
if err != RedisNotFound { // 设置
return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error())) err = rl.operation.Set(ctx, key, val, ttl).Err()
if err != nil {
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
} }
return val, nil
} }
if get != "" { if get != "" {
return resp, errors.New("上锁失败,已存在") return resp, errors.New("上锁失败,已存在")
} }
// 设置 return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error()))
err = rl.operation.Set(ctx, key, val, ttl).Err()
if err != nil {
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
}
return val, nil
} }
// Unlock 解锁 // Unlock 解锁
@ -59,18 +57,16 @@ func (rl *RedisClientLock) Unlock(ctx context.Context, key string) error {
func (rl *RedisClientLock) LockForever(ctx context.Context, key string, val string) (resp string, err error) { func (rl *RedisClientLock) LockForever(ctx context.Context, key string, val string) (resp string, err error) {
// 获取 // 获取
get, err := rl.operation.Get(ctx, key).Result() get, err := rl.operation.Get(ctx, key).Result()
if err != nil { if err.Error() == RedisNotFound {
if err != RedisNotFound { // 设置
return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error())) err = rl.operation.Set(ctx, key, val, 0).Err()
if err != nil {
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
} }
return val, nil
} }
if get != "" { if get != "" {
return resp, errors.New("上锁失败,已存在") return resp, errors.New("上锁失败,已存在")
} }
// 设置 return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error()))
err = rl.operation.Set(ctx, key, val, 0).Err()
if err != nil {
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
}
return val, nil
} }

Loading…
Cancel
Save