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

master v1.0.22
李光春 2 years ago
parent 389bd8e65c
commit 841f55a29a

@ -1,3 +1,3 @@
package dorm
const Version = "1.0.21"
const Version = "1.0.22"

@ -3,6 +3,7 @@ package dorm
import (
"context"
"errors"
"fmt"
"time"
)
@ -27,7 +28,7 @@ func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl
// 获取
get, err := rl.operation.Get(ctx, key).Result()
if err != nil {
return resp, errors.New("获取异常")
return resp, errors.New(fmt.Sprintf("获取异常%s", err.Error()))
}
if get != "" {
return resp, errors.New("上锁失败,已存在")
@ -35,7 +36,7 @@ func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl
// 设置
err = rl.operation.Set(ctx, key, val, ttl).Err()
if err != nil {
return resp, errors.New("上锁失败")
return resp, errors.New(fmt.Sprintf("上锁失败%s", err.Error()))
}
return val, nil
}
@ -44,6 +45,9 @@ func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl
// key 锁名
func (rl *RedisClientLock) Unlock(ctx context.Context, key string) error {
_, err := rl.operation.Del(ctx, key).Result()
if err != nil {
return errors.New(fmt.Sprintf("解锁失败:%s", err.Error()))
}
return err
}
@ -54,7 +58,7 @@ func (rl *RedisClientLock) LockForever(ctx context.Context, key string, val stri
// 获取
get, err := rl.operation.Get(ctx, key).Result()
if err != nil {
return resp, errors.New("获取异常")
return resp, errors.New(fmt.Sprintf("获取异常%s", err.Error()))
}
if get != "" {
return resp, errors.New("上锁失败,已存在")
@ -62,7 +66,7 @@ func (rl *RedisClientLock) LockForever(ctx context.Context, key string, val stri
// 设置
err = rl.operation.Set(ctx, key, val, 0).Err()
if err != nil {
return resp, errors.New("上锁失败")
return resp, errors.New(fmt.Sprintf("上锁失败%s", err.Error()))
}
return val, nil
}

Loading…
Cancel
Save