From 841f55a29ad4fefb08ccc71a3c5c838acabbd986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 13 Aug 2022 21:52:25 +0800 Subject: [PATCH] - update redis lock --- const.go | 2 +- redis_lock.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/const.go b/const.go index 81c3e37..475f9fd 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.21" +const Version = "1.0.22" diff --git a/redis_lock.go b/redis_lock.go index 50ecbfd..75158e9 100644 --- a/redis_lock.go +++ b/redis_lock.go @@ -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 }