/
options.go
233 lines (191 loc) · 4.9 KB
/
options.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
package redis
import (
"time"
"github.com/go-redis/redis"
)
// SetOption 代表设置一个 key 时候用到的各种选项。
type SetOption struct {
t setOptionType
expire time.Duration
}
// Expire 返回一个用于设置 key 超时的选项。
// 详见 https://redis.io/commands/set。
func Expire(timeout time.Duration) SetOption {
return SetOption{
t: setOptionExpire,
expire: timeout,
}
}
// XX 返回一个用于设置仅当 key 存在才改变其值的选项。
// 详见 https://redis.io/commands/set。
func XX() SetOption {
return SetOption{
t: setOptionXX,
}
}
// NX 返回一个用于设置仅当 key 不存在才改变其值的选项。
// 详见 https://redis.io/commands/set。
func NX() SetOption {
return SetOption{
t: setOptionNX,
}
}
// Args 返回用于拼接 Redis 命令的参数。
func (so *SetOption) Args() []interface{} {
switch so.t {
case setOptionExpire:
if so.expire%time.Second == 0 {
return []interface{}{"EX", int64(so.expire / time.Second)}
}
return []interface{}{"PX", int64(so.expire.Round(time.Millisecond) / time.Millisecond)}
case setOptionNX:
return []interface{}{"NX"}
case setOptionXX:
return []interface{}{"XX"}
}
return nil
}
type setOptionType int
const (
setOptionInvalid setOptionType = iota
setOptionExpire
setOptionNX
setOptionXX
)
// StoreOption 代表 sorted set 对计算结果进行存储时的选项。
type StoreOption struct {
t storeOptionType
opt interface{}
}
// Weights 返回一个用于 sorted set 计算结果存储的选项 WEIGHTS。
// 详见 https://redis.io/commands/zunionstore。
func Weights(weights ...float64) StoreOption {
return StoreOption{
t: storeOptionWeights,
opt: weights,
}
}
// AggregateSum 返回一个用于 sorted set 计算结果存储的选项 AGGREGATE SUM。
// 详见 https://redis.io/commands/zunionstore。
func AggregateSum() StoreOption {
return StoreOption{
t: storeOptionAggregate,
opt: "SUM",
}
}
// AggregateMin 返回一个用于 sorted set 计算结果存储的选项 AGGREGATE MIN。
// 详见 https://redis.io/commands/zunionstore。
func AggregateMin() StoreOption {
return StoreOption{
t: storeOptionAggregate,
opt: "MIN",
}
}
// AggregateMax 返回一个用于 sorted set 计算结果存储的选项 AGGREGATE MAX。
// 详见 https://redis.io/commands/zunionstore。
func AggregateMax() StoreOption {
return StoreOption{
t: storeOptionAggregate,
opt: "MAX",
}
}
// Args 返回用于拼接 Redis 命令的参数。
func (so *StoreOption) Args() []interface{} {
switch so.t {
case storeOptionWeights:
weights := so.opt.([]float64)
if len(weights) == 0 {
return nil
}
args := make([]interface{}, 0, len(weights)+1)
args = append(args, "WEIGHTS")
for _, w := range weights {
args = append(args, w)
}
return args
case storeOptionAggregate:
return []interface{}{"AGGREGATE", so.opt}
}
return nil
}
func (so *StoreOption) fillZStore(zstore *redis.ZStore) {
switch so.t {
case storeOptionWeights:
weights := so.opt.([]float64)
zstore.Weights = weights
case storeOptionAggregate:
aggregate := so.opt.(string)
zstore.Aggregate = aggregate
}
}
type storeOptionType int
const (
storeOptionInvalid storeOptionType = iota
storeOptionWeights
storeOptionAggregate
)
// RangeOption 代表 sorted set 对计算结果进行范围查询时的选项。
type RangeOption struct {
offset int
count int
}
// Limit 返回一个用于 sorted set 范围查询的 limit。
func Limit(offset int, count int) RangeOption {
return RangeOption{
offset: offset,
count: count,
}
}
// Args 返回用于拼接 Redis 命令的参数。
func (ro *RangeOption) Args() []interface{} {
return []interface{}{"LIMIT", ro.offset, ro.count}
}
func (ro *RangeOption) fillZRangeBy(zrangeby *redis.ZRangeBy) {
zrangeby.Offset = int64(ro.offset)
zrangeby.Count = int64(ro.count)
}
// ScanOption 代表一个扫描选项。
type ScanOption struct {
t scanOptionType
opt interface{}
}
// Match 返回一个扫描选项,用于在各种 Scan 中实现 MATCH pattern。
func Match(pattern string) ScanOption {
return ScanOption{
t: scanOptionMatch,
opt: pattern,
}
}
// Count 返回一个扫描选项,用于在各种 Scan 中实现 COUNT count。
func Count(count int) ScanOption {
return ScanOption{
t: scanOptionCount,
opt: count,
}
}
// Args 返回用于拼接 Redis 命令的参数。
func (so *ScanOption) Args() []interface{} {
switch so.t {
case scanOptionMatch:
return []interface{}{"MATCH", so.opt}
case scanOptionCount:
return []interface{}{"COUNT", so.opt}
}
return nil
}
type scanOptionType int
const (
scanOptionTypeInvalid scanOptionType = iota
scanOptionMatch
scanOptionCount
)
// FlushOption 代表 FLUSHDB 和 FLUSHALL 的选项。
type FlushOption int
const (
flushOptionInvalid FlushOption = iota
flushOptionAsync
)
// Async 代表用异步模式进行 flush。
func Async() FlushOption {
return flushOptionAsync
}