-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.go
256 lines (245 loc) · 6.16 KB
/
keys.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package red
import (
"context"
xerr "github.com/goclub/error"
xtime "github.com/goclub/time"
"strconv"
"strings"
"time"
)
// MakeKey MakeKey("user", today, "count") // "user:2022-01-01:count"
func MakeKey(elems... string) (key string) {
key = strings.Join(elems, ":")
if key == "" {
panic(xerr.New("goclub/redis: MakeKey(elems... string) elems can not be empty"))
}
return
}
type COPY struct {
Source string
Destination string
DB OptionUint8
Replace bool
}
// When Source is exists and Destination is inexistence return 1 Otherwise return 0
func (data COPY) Do(ctx context.Context, client Connecter) (reply int64, err error) {
args := []string{"COPY"}
args = append(args, data.Source, data.Destination)
if data.DB.Valid {
args = append(args, "DB")
args = append(args, strconv.FormatUint(uint64(data.DB.Uint8), 10))
}
if data.Replace {
args = append(args, "REPLACE")
}
reply, _, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type DUMP struct {
Key string
}
func (data DUMP) Do(ctx context.Context, client Connecter) (value string, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"DUMP", data.Key}
value,_, err = client.DoStringReply(ctx, args) ; if err != nil {
return
}
return
}
type DEL struct {
Key string `note:"DEL If your redis version >= 4.0.0, use UNLINK whenever possible"`
Keys []string
}
func (data DEL) Do(ctx context.Context, client Connecter) (delTotal uint64, err error) {
args := []string{"DEL"}
if data.Key != "" {
data.Keys = []string{data.Key}
}
if len(data.Keys) == 0 { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args = append(args, data.Keys...)
var value int64
value,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
delTotal = uint64(value)
return
}
type UNLINK struct {
Key string
Keys []string
}
func (data UNLINK) Do(ctx context.Context, client Connecter) (unlinkTotal uint64, err error) {
args := []string{"UNLINK"}
if data.Key != "" {
data.Keys = []string{data.Key}
}
if len(data.Keys) == 0 { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args = append(args, data.Keys...)
var value int64
value,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
unlinkTotal = uint64(value)
return
}
type EXISTS struct {
Key string
Keys []string
}
func (data EXISTS) Do(ctx context.Context, client Connecter) (existsCount uint64, err error) {
args := []string{"EXISTS"}
if data.Key != "" {
data.Keys = []string{data.Key}
}
if len(data.Keys) == 0 { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args = append(args, data.Keys...)
var value int64
value,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
existsCount = uint64(value)
return
}
type KEYS struct {
Pattern string
}
func (data KEYS) Do(ctx context.Context, client Connecter) (keys []string, err error) {
args := []string{"KEYS", data.Pattern}
reply, err := client.DoArrayStringReply(ctx, args) ; if err != nil {
return
}
for _, v := range reply {
keys = append(keys, v.String)
}
return
}
type PEXPIRE struct {
Key string
Duration time.Duration
NX bool
XX bool
GT bool
LT bool
}
func (data PEXPIRE) Do(ctx context.Context, client Connecter) (reply int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
if data.Duration < time.Millisecond-1 {
err = xerr.New("red.PEXPIRE{}.Interval can not less than time.Millisecond")
return
}
args := []string{"PEXPIRE", data.Key, strconv.FormatInt(data.Duration.Milliseconds(), 10)}
if data.NX {
args = append(args, "NX")
}
if data.XX {
args = append(args, "XX")
}
if data.GT {
args = append(args, "GT")
}
if data.LT {
args = append(args, "LT")
}
reply,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type EXPIRE struct {
Key string
Duration time.Duration
NX bool
XX bool
GT bool
LT bool
}
func (data EXPIRE) Do(ctx context.Context, client Connecter) (reply int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
if data.Duration < time.Second-1 {
err = xerr.New("red.EXPIRE{}.Interval can not less than time.Second")
return
}
args := []string{"EXPIRE", data.Key, strconv.FormatUint(uint64(data.Duration.Seconds()), 10)}
if data.NX {
args = append(args, "NX")
}
if data.XX {
args = append(args, "XX")
}
if data.GT {
args = append(args, "GT")
}
if data.LT {
args = append(args, "LT")
}
reply,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type EXPIREAT struct {
Key string
Time time.Time
NX bool
XX bool
GT bool
LT bool
}
func (data EXPIREAT) Do(ctx context.Context, client Connecter) (reply int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"EXPIREAT", data.Key, strconv.FormatInt(xtime.UnixMilli(data.Time), 10)}
if data.NX {
args = append(args, "NX")
}
if data.XX {
args = append(args, "XX")
}
if data.GT {
args = append(args, "GT")
}
if data.LT {
args = append(args, "LT")
}
reply,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type EXPIRETIME struct {
Key string
}
func (data EXPIRETIME) Do(ctx context.Context, client Connecter) (reply int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"EXPIRETIME", data.Key}
reply,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type PTTL struct {
Key string
}
type ResultTTL struct {
TTL time.Duration
NeverExpire bool
KeyDoesNotExist bool
}
func (data PTTL) Do(ctx context.Context, client Connecter) (result ResultTTL, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"PTTL", data.Key}
value, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
if value == -1 {
result.NeverExpire = true
return
}
if value == -2 {
result.KeyDoesNotExist = true
return
}
result.TTL = time.Millisecond * time.Duration(value)
return
}