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

master v1.0.21
李光春 2 years ago
parent 8e27344f05
commit 389bd8e65c

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

@ -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("上锁失败")
}

Loading…
Cancel
Save