forked from hyman-m/redis-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
132 lines (114 loc) · 2.94 KB
/
tools.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2022 <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tools
import (
"context"
"fmt"
"time"
)
const (
compareAndDeleteScript = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1])
else
return 0
end
`
compareAndSwapScript = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("SET", KEYS[1], ARGV[2])
else
return 0
end
`
compareAndSwapEXScript = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("SET", KEYS[1], ARGV[2], %s ,ARGV[3])
else
return 0
end
`
compareAndSwapKeepTTLScript = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("SET", KEYS[1], ARGV[2], "keepttl")
else
return 0
end
`
success = "OK"
)
// RedisTools .
type RedisTools struct {
Client RedisClient
}
// NewTools create a new redis tools
func NewTools(client RedisClient) *RedisTools {
return &RedisTools{Client: client}
}
// Cas compare and swap
func (r *RedisTools) Cas(ctx context.Context, key string, oldValue interface{},
newValue interface{}) (bool, error) {
res, err := r.Client.Eval(ctx, compareAndSwapScript, []string{key}, oldValue, newValue).Result()
if err != nil {
return false, err
}
if res == success {
return true, nil
}
return false, nil
}
// CasEx compare and swap with timeout,
// If the timeout is 0, the timeout is not set,
// If the timeout is -1, keep timeout (redis >= 6.0).
func (r *RedisTools) CasEx(ctx context.Context, key string, oldValue interface{},
newValue interface{}, expire time.Duration) (bool, error) {
if expire == 0 {
return r.Cas(ctx, key, oldValue, newValue)
}
var err error
var res interface{}
if usePrecise(expire) {
res, err = r.Client.Eval(ctx, fmt.Sprintf(compareAndSwapEXScript, "PX"),
[]string{key}, oldValue, newValue, formatMs(expire)).Result()
} else if expire > 0 {
res, err = r.Client.Eval(ctx, fmt.Sprintf(compareAndSwapEXScript, "EX"),
[]string{key}, oldValue, newValue, formatSec(expire)).Result()
} else {
res, err = r.Client.Eval(ctx, compareAndSwapKeepTTLScript, []string{key},
oldValue, newValue).Result()
}
if err != nil {
return false, err
}
if res == success {
return true, nil
}
return false, nil
}
// Cad compare and delete
func (r *RedisTools) Cad(ctx context.Context, key string, value interface{}) (bool, error) {
res, err := r.Client.Eval(ctx, compareAndDeleteScript, []string{key}, value).Result()
if err != nil {
return false, err
}
if res == 0 {
return false, nil
}
return true, nil
}
func usePrecise(dur time.Duration) bool {
return dur < time.Second || dur%time.Second != 0
}
func formatMs(dur time.Duration) int64 {
if dur > 0 && dur < time.Millisecond {
return 1
}
return int64(dur / time.Millisecond)
}
func formatSec(dur time.Duration) int64 {
if dur > 0 && dur < time.Second {
return 1
}
return int64(dur / time.Second)
}