From 389bd8e65c30b530ff9e622a793a2f4b99393ac7 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 17:56:23 +0800 Subject: [PATCH] - update redis --- const.go | 2 +- redis_lock.go | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/const.go b/const.go index 8e0e220..81c3e37 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.20" +const Version = "1.0.21" diff --git a/redis_lock.go b/redis_lock.go index e61997f..50ecbfd 100644 --- a/redis_lock.go +++ b/redis_lock.go @@ -7,23 +7,25 @@ import ( ) // RedisClientLock https://github.com/go-redis/redis -type RedisClientLock struct{} +type RedisClientLock struct { + operation *RedisClient // 操作 +} // NewLock 实例化锁 func (r *RedisClient) NewLock() *RedisClientLock { - return &RedisClientLock{} + return &RedisClientLock{r} } // Lock 上锁 // key 锁名 // val 锁内容 // ttl 锁过期时间 -func (r *RedisClient) Lock(ctx context.Context, key string, val string, ttl time.Duration) (resp string, err error) { +func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl time.Duration) (resp string, err error) { if ttl <= 0 { return resp, errors.New("长期请使用 LockForever 方法") } // 获取 - get, err := r.Get(ctx, key).Result() + get, err := rl.operation.Get(ctx, key).Result() if err != nil { return resp, errors.New("获取异常") } @@ -31,7 +33,7 @@ func (r *RedisClient) Lock(ctx context.Context, key string, val string, ttl time return resp, errors.New("上锁失败,已存在") } // 设置 - err = r.Set(ctx, key, val, ttl).Err() + err = rl.operation.Set(ctx, key, val, ttl).Err() if err != nil { return resp, errors.New("上锁失败") } @@ -40,17 +42,17 @@ func (r *RedisClient) Lock(ctx context.Context, key string, val string, ttl time // Unlock 解锁 // key 锁名 -func (r *RedisClient) Unlock(ctx context.Context, key string) error { - _, err := r.Del(ctx, key).Result() +func (rl *RedisClientLock) Unlock(ctx context.Context, key string) error { + _, err := rl.operation.Del(ctx, key).Result() return err } // LockForever 永远上锁 // key 锁名 // val 锁内容 -func (r *RedisClient) 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 := r.Get(ctx, key).Result() + get, err := rl.operation.Get(ctx, key).Result() if err != nil { return resp, errors.New("获取异常") } @@ -58,7 +60,7 @@ func (r *RedisClient) LockForever(ctx context.Context, key string, val string) ( return resp, errors.New("上锁失败,已存在") } // 设置 - err = r.Set(ctx, key, val, 0).Err() + err = rl.operation.Set(ctx, key, val, 0).Err() if err != nil { return resp, errors.New("上锁失败") }