|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|