/
wrapper.go
318 lines (275 loc) · 7.64 KB
/
wrapper.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package redis
import (
"time"
"github.com/go-redis/redis"
)
// ===== Command output / return value types
// Result generic interface
type Result interface {
Int() int64
String() string
Bool() bool
Duration() time.Duration
Result() (int64, error)
ResultString() (string, error)
Multi() ([]string, error)
MultiInterface() ([]interface{}, error)
Err() error
MapStringString() (map[string]string, error)
}
// ResultImpl generic interface
type ResultImpl struct {
value int64
valueString string
valueBool bool
valueDuration time.Duration
err error
multi []string
multiInterface []interface{}
mapStringString map[string]string
}
// Int implementation
func (r *ResultImpl) Int() int64 {
return r.value
}
// String implementation
func (r *ResultImpl) String() string {
return r.valueString
}
// Bool implementation
func (r *ResultImpl) Bool() bool {
return r.valueBool
}
// Duration implementation
func (r *ResultImpl) Duration() time.Duration {
return r.valueDuration
}
// Err implementation
func (r *ResultImpl) Err() error {
return r.err
}
// Result implementation
func (r *ResultImpl) Result() (int64, error) {
return r.value, r.err
}
// ResultString implementation
func (r *ResultImpl) ResultString() (string, error) {
return r.valueString, r.err
}
// Multi implementation
func (r *ResultImpl) Multi() ([]string, error) {
return r.multi, r.err
}
// MultiInterface implementation
func (r *ResultImpl) MultiInterface() ([]interface{}, error) {
return r.multiInterface, r.err
}
// MapStringString implementation
func (r *ResultImpl) MapStringString() (map[string]string, error) {
return r.mapStringString, r.err
}
// ====== Client
// Client interface which specifies the currently used subset of redis operations
type Client interface {
Del(keys ...string) Result
Exists(keys ...string) Result
Get(key string) Result
Set(key string, value interface{}, expiration time.Duration) Result
Ping() Result
Keys(pattern string) Result
SMembers(key string) Result
SIsMember(key string, member interface{}) Result
SAdd(key string, members ...interface{}) Result
SRem(key string, members ...interface{}) Result
Incr(key string) Result
Decr(key string) Result
RPush(key string, values ...interface{}) Result
LRange(key string, start, stop int64) Result
LTrim(key string, start, stop int64) Result
LLen(key string) Result
Expire(key string, value time.Duration) Result
TTL(key string) Result
MGet(keys []string) Result
SCard(key string) Result
Eval(script string, keys []string, args ...interface{}) Result
HIncrBy(key string, field string, value int64) Result
HGetAll(key string) Result
}
// ClientImpl wrapps redis client
type ClientImpl struct {
wrapped redis.UniversalClient
}
func (c *ClientImpl) wrapResult(result interface{}) Result {
if result == nil {
return nil
}
switch v := result.(type) {
case *redis.StatusCmd:
return &ResultImpl{
valueString: v.Val(),
err: v.Err(),
}
case *redis.IntCmd:
return &ResultImpl{
value: v.Val(),
err: v.Err(),
}
case *redis.StringCmd:
return &ResultImpl{
valueString: v.Val(),
err: v.Err(),
}
case *redis.StringSliceCmd:
return &ResultImpl{
err: v.Err(),
multi: v.Val(),
}
case *redis.BoolCmd:
return &ResultImpl{
valueBool: v.Val(),
err: v.Err(),
}
case *redis.DurationCmd:
return &ResultImpl{
valueDuration: v.Val(),
err: v.Err(),
}
case *redis.SliceCmd:
return &ResultImpl{
err: v.Err(),
multiInterface: v.Val(),
}
case *redis.Cmd:
return &ResultImpl{
err: v.Err(),
}
case *redis.StringStringMapCmd:
return &ResultImpl{
err: v.Err(),
mapStringString: v.Val(),
}
default:
return nil
}
}
// Del implements Del wrapper for redis
func (c *ClientImpl) Del(keys ...string) Result {
res := c.wrapped.Del(keys...)
return c.wrapResult(res)
}
// Exists implements Exists wrapper for redis
func (c *ClientImpl) Exists(keys ...string) Result {
res := c.wrapped.Exists(keys...)
return c.wrapResult(res)
}
// Get implements Get wrapper for redis
func (c *ClientImpl) Get(key string) Result {
res := c.wrapped.Get(key)
return c.wrapResult(res)
}
// Set implements Set wrapper for redis
func (c *ClientImpl) Set(key string, value interface{}, expiration time.Duration) Result {
res := c.wrapped.Set(key, value, expiration)
return c.wrapResult(res)
}
// Ping implements Ping wrapper for redis
func (c *ClientImpl) Ping() Result {
res := c.wrapped.Ping()
return c.wrapResult(res)
}
// Keys implements Keys wrapper for redis
func (c *ClientImpl) Keys(pattern string) Result {
res := c.wrapped.Keys(pattern)
return c.wrapResult(res)
}
// SMembers implements SMembers wrapper for redis
func (c *ClientImpl) SMembers(key string) Result {
res := c.wrapped.SMembers(key)
return c.wrapResult(res)
}
// SIsMember implements SIsMember wrapper for redis
func (c *ClientImpl) SIsMember(key string, member interface{}) Result {
res := c.wrapped.SIsMember(key, member)
return c.wrapResult(res)
}
// SAdd implements SAdd wrapper for redis
func (c *ClientImpl) SAdd(key string, members ...interface{}) Result {
res := c.wrapped.SAdd(key, members...)
return c.wrapResult(res)
}
// SRem implements SRem wrapper for redis
func (c *ClientImpl) SRem(key string, members ...interface{}) Result {
res := c.wrapped.SRem(key, members...)
return c.wrapResult(res)
}
// Incr implements Incr wrapper for redis
func (c *ClientImpl) Incr(key string) Result {
res := c.wrapped.Incr(key)
return c.wrapResult(res)
}
// Decr implements Decr wrapper for redis
func (c *ClientImpl) Decr(key string) Result {
res := c.wrapped.Decr(key)
return c.wrapResult(res)
}
// RPush implements RPush wrapper for redis
func (c *ClientImpl) RPush(key string, values ...interface{}) Result {
res := c.wrapped.RPush(key, values...)
return c.wrapResult(res)
}
// LRange implements LRange wrapper for redis
func (c *ClientImpl) LRange(key string, start, stop int64) Result {
res := c.wrapped.LRange(key, start, stop)
return c.wrapResult(res)
}
// LTrim implements LTrim wrapper for redis
func (c *ClientImpl) LTrim(key string, start, stop int64) Result {
res := c.wrapped.LTrim(key, start, stop)
return c.wrapResult(res)
}
// LLen implements LLen wrapper for redis
func (c *ClientImpl) LLen(key string) Result {
res := c.wrapped.LLen(key)
return c.wrapResult(res)
}
// Expire implements Expire wrapper for redis
func (c *ClientImpl) Expire(key string, value time.Duration) Result {
res := c.wrapped.Expire(key, value)
return c.wrapResult(res)
}
// TTL implements TTL wrapper for redis
func (c *ClientImpl) TTL(key string) Result {
res := c.wrapped.TTL(key)
return c.wrapResult(res)
}
// MGet implements MGet wrapper for redis
func (c *ClientImpl) MGet(keys []string) Result {
res := c.wrapped.MGet(keys...)
return c.wrapResult(res)
}
// SCard implements SCard wrapper for redis
func (c *ClientImpl) SCard(key string) Result {
res := c.wrapped.SCard(key)
return c.wrapResult(res)
}
// Eval implements Eval wrapper for redis
func (c *ClientImpl) Eval(script string, keys []string, args ...interface{}) Result {
res := c.wrapped.Eval(script, keys, args...)
return c.wrapResult(res)
}
// HIncrBy implements HIncrBy wrapper for redis
func (c *ClientImpl) HIncrBy(key string, field string, value int64) Result {
res := c.wrapped.HIncrBy(key, field, value)
return c.wrapResult(res)
}
// HGetAll implements HGetAll wrapper for redis
func (c *ClientImpl) HGetAll(key string) Result {
res := c.wrapped.HGetAll(key)
return c.wrapResult(res)
}
// NewClient returns new client implementation
func NewClient(options *UniversalOptions) (Client, error) {
return &ClientImpl{
wrapped: redis.NewUniversalClient(options),
}, nil
}