/
redis.go
63 lines (55 loc) · 1.49 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package lock
import (
"context"
"fmt"
"time"
"github.com/go-eagle/eagle/pkg/log"
"github.com/redis/go-redis/v9"
)
// RedisLock is a redis lock.
type RedisLock struct {
key string
redisClient *redis.Client
token string
expiration time.Duration
}
// NewRedisLock new a redis lock instance
// nolint
func NewRedisLock(rdb *redis.Client, key string, expiration time.Duration) *RedisLock {
opt := &RedisLock{
key: getRedisKey(key),
redisClient: rdb,
token: genToken(),
expiration: expiration,
}
return opt
}
// Lock acquires the lock.
func (l *RedisLock) Lock(ctx context.Context) (bool, error) {
isSet, err := l.redisClient.SetNX(ctx, l.key, l.token, l.expiration).Result()
if err == redis.Nil {
return false, nil
} else if err != nil {
log.Errorf("acquires the lock err, key: %s, err: %s", l.key, err.Error())
return false, err
}
return isSet, nil
}
// Unlock del the lock.
// NOTE: token 一致才会执行删除,避免误删,这里用了lua脚本进行事务处理
func (l *RedisLock) Unlock(ctx context.Context) (bool, error) {
luaScript := "if redis.call('GET',KEYS[1]) == ARGV[1] then return redis.call('DEL',KEYS[1]) else return 0 end"
ret, err := l.redisClient.Eval(ctx, luaScript, []string{l.key}, l.token).Result()
if err != nil {
return false, err
}
reply, ok := ret.(int64)
if !ok {
return false, nil
}
return reply == 1, nil
}
// getRedisKey 获取key
func getRedisKey(key string) string {
return fmt.Sprintf(RedisLockKey, key)
}