-
Notifications
You must be signed in to change notification settings - Fork 6
/
xredis.go
419 lines (336 loc) · 11.5 KB
/
xredis.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package xredis
import (
"github.com/garyburd/redigo/redis"
"strconv"
)
const (
expireOption = "EX"
notExistsOption = "NX"
matchOption = "MATCH"
setCommand = "SET"
delCommand = "DEL"
getCommand = "GET"
keysCommand = "KEYS"
pingCommand = "PING"
echoCommand = "ECHO"
infoCommand = "INFO"
hSetCommand = "HSET"
hGetCommand = "HGET"
hDelCommand = "HDEL"
hKeysCommand = "HKEYS"
scanCommand = "SCAN"
hScanCommand = "HSCAN"
appendCommand = "APPEND"
getRangeCommand = "GETRANGE"
setRangeCommand = "SETRANGE"
expireCommand = "EXPIRE"
flushDbCommand = "FLUSHDB"
flushAllCommand = "FLUSHALL"
existsCommand = "EXISTS"
hExistsCommand = "HEXISTS"
hGetAllCommand = "HGETALL"
incrByCommand = "INCRBY"
incrByFloatCommand = "INCRBYFLOAT"
hIncrByCommand = "HINCRBY"
hIncrByFloatCommand = "HINCRBYFLOAT"
)
// DefaultClient returns a client with default options
func DefaultClient() *Client {
pool := newServerPool(&Options{})
return NewClient(pool)
}
// SetupClient returns a client with provided options
func SetupClient(options *Options) *Client {
pool := newServerPool(options)
return NewClient(pool)
}
// SetupSentinelClient returns a client with provided options
func SetupSentinelClient(options *SentinelOptions) *Client {
writePool := newWriteSentinelPool(options)
readPool := newReadSentinelPool(options)
return &Client{writePool: writePool, readPool: readPool}
}
// NewClient returns a client using provided redis.Pool
func NewClient(pool *redis.Pool) *Client {
return &Client{writePool: pool, readPool: pool}
}
// Client redis client
type Client struct {
writePool *redis.Pool
readPool *redis.Pool
}
// GetConnection gets a connection from the pool
func (c *Client) GetConnection() redis.Conn {
return c.writePool.Get()
}
// Ping pings redis
func (c *Client) Ping() (string, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.String(connection.Do(pingCommand))
}
// FlushDb flushes the keys of the current database
func (c *Client) FlushDb() error {
connection := c.getWriteConnection()
defer connection.Close()
return toError(connection.Do(flushDbCommand))
}
// FlushAll flushes the keys of all databases
func (c *Client) FlushAll() error {
connection := c.getWriteConnection()
defer connection.Close()
return toError(connection.Do(flushAllCommand))
}
// Echo echoes the message
func (c *Client) Echo(message string) (string, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.String(connection.Do(echoCommand, message))
}
// Info returns redis information and statistics
func (c *Client) Info() (string, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.String(connection.Do(infoCommand))
}
// Scan incrementally iterate over keys
func (c *Client) Scan(startIndex int64, pattern string) (int64, []string, error) {
connection := c.getWriteConnection()
defer connection.Close()
results, err := redis.Values(connection.Do(scanCommand, startIndex, matchOption, pattern))
if err != nil {
return 0, nil, err
}
return parseScanResults(results)
}
// Append to a key's value
func (c *Client) Append(key string, value string) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Int64(connection.Do(appendCommand, key, value))
}
// GetRange to get a key's value's range
func (c *Client) GetRange(key string, start int, end int) (string, error) {
connection := c.getReadConnection()
defer connection.Close()
return redis.String(connection.Do(getRangeCommand, key, start, end))
}
// SetRange to set a key's value's range
func (c *Client) SetRange(key string, start int, value string) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Int64(connection.Do(setRangeCommand, key, start, value))
}
// Expire sets a key's timeout in seconds
func (c *Client) Expire(key string, timeout int64) (bool, error) {
connection := c.getWriteConnection()
defer connection.Close()
count, err := redis.Int64(connection.Do(expireCommand, key, timeout))
return count > 0, err
}
// Set sets a key/value pair
func (c *Client) Set(key string, value string) (bool, error) {
connection := c.getWriteConnection()
defer connection.Close()
return toBool(connection.Do(setCommand, key, value))
}
// SetNx sets a key/value pair if the key does not exist
func (c *Client) SetNx(key string, value string) (bool, error) {
connection := c.getWriteConnection()
defer connection.Close()
return toBool(connection.Do(setCommand, key, value, notExistsOption))
}
// SetEx sets a key/value pair with a timeout in seconds
func (c *Client) SetEx(key string, value string, timeout int64) (bool, error) {
connection := c.getWriteConnection()
defer connection.Close()
return toBool(connection.Do(setCommand, key, value, expireOption, timeout))
}
// Get retrieves a key's value
func (c *Client) Get(key string) (string, bool, error) {
connection := c.getReadConnection()
defer connection.Close()
return toString(connection.Do(getCommand, key))
}
// Exists checks how many keys exist
func (c *Client) Exists(keys ...string) (bool, error) {
connection := c.getReadConnection()
defer connection.Close()
interfaces := make([]interface{}, len(keys))
for i, key := range keys {
interfaces[i] = key
}
count, err := redis.Int64(connection.Do(existsCommand, interfaces...))
return count > 0, err
}
// Del deletes keys
func (c *Client) Del(keys ...string) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
interfaces := make([]interface{}, len(keys))
for i, key := range keys {
interfaces[i] = key
}
return redis.Int64(connection.Do(delCommand, interfaces...))
}
// Keys retrieves keys that match a pattern
func (c *Client) Keys(pattern string) ([]string, error) {
connection := c.getReadConnection()
defer connection.Close()
return redis.Strings(connection.Do(keysCommand, pattern))
}
// Incr increments the key's value
func (c *Client) Incr(key string) (int64, error) {
return c.IncrBy(key, 1)
}
// IncrBy increments the key's value by the increment provided
func (c *Client) IncrBy(key string, increment int64) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Int64(connection.Do(incrByCommand, key, increment))
}
// IncrByFloat increments the key's value by the increment provided
func (c *Client) IncrByFloat(key string, increment float64) (float64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Float64(connection.Do(incrByFloatCommand, key, increment))
}
// Decr decrements the key's value
func (c *Client) Decr(key string) (int64, error) {
return c.IncrBy(key, -1)
}
// DecrBy decrements the key's value by the decrement provided
func (c *Client) DecrBy(key string, decrement int64) (int64, error) {
return c.IncrBy(key, -decrement)
}
// DecrByFloat decrements the key's value by the decrement provided
func (c *Client) DecrByFloat(key string, decrement float64) (float64, error) {
return c.IncrByFloat(key, -decrement)
}
// HScan incrementally iterate over key's fields and values
func (c *Client) HScan(key string, startIndex int64, pattern string) (int64, []string, error) {
connection := c.getWriteConnection()
defer connection.Close()
results, err := redis.Values(connection.Do(hScanCommand, key, startIndex, matchOption, pattern))
if err != nil {
return 0, nil, err
}
return parseScanResults(results)
}
// HSet sets a key's field/value pair
func (c *Client) HSet(key string, field string, value string) (bool, error) {
connection := c.getWriteConnection()
defer connection.Close()
code, err := redis.Int(connection.Do(hSetCommand, key, field, value))
return code > 0, err
}
// HKeys retrieves a hash's keys
func (c *Client) HKeys(key string) ([]string, error) {
connection := c.getReadConnection()
defer connection.Close()
return redis.Strings(connection.Do(hKeysCommand, key))
}
// HExists determine's a key's field's existence
func (c *Client) HExists(key string, field string) (bool, error) {
connection := c.getReadConnection()
defer connection.Close()
return redis.Bool(connection.Do(hExistsCommand, key, field))
}
// HGet retrieves a key's field's value
func (c *Client) HGet(key string, field string) (string, bool, error) {
connection := c.getReadConnection()
defer connection.Close()
return toString(connection.Do(hGetCommand, key, field))
}
// HGetAll retrieves the key
func (c *Client) HGetAll(key string) (map[string]string, error) {
connection := c.getReadConnection()
defer connection.Close()
return redis.StringMap(connection.Do(hGetAllCommand, key))
}
// HDel deletes a key's fields
func (c *Client) HDel(key string, fields ...string) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
interfaces := make([]interface{}, len(fields)+1)
interfaces[0] = key
for i, key := range fields {
interfaces[i+1] = key
}
return redis.Int64(connection.Do(hDelCommand, interfaces...))
}
// HIncr increments the key's field's value
func (c *Client) HIncr(key string, field string) (int64, error) {
return c.HIncrBy(key, field, 1)
}
// HIncrBy increments the key's field's value by the increment provided
func (c *Client) HIncrBy(key string, field string, increment int64) (int64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Int64(connection.Do(hIncrByCommand, key, field, increment))
}
// HIncrByFloat increments the key's field's value by the increment provided
func (c *Client) HIncrByFloat(key string, field string, increment float64) (float64, error) {
connection := c.getWriteConnection()
defer connection.Close()
return redis.Float64(connection.Do(hIncrByFloatCommand, key, field, increment))
}
// HDecr decrements the key's field's value
func (c *Client) HDecr(key string, field string) (int64, error) {
return c.HIncrBy(key, field, -1)
}
// HDecrBy decrements the key's field's value by the decrement provided
func (c *Client) HDecrBy(key string, field string, decrement int64) (int64, error) {
return c.HIncrBy(key, field, -decrement)
}
// HDecrByFloat decrements the key's field's value by the decrement provided
func (c *Client) HDecrByFloat(key string, field string, decrement float64) (float64, error) {
return c.HIncrByFloat(key, field, -decrement)
}
// Close closes connections writePool
func (c *Client) Close() error {
err := c.writePool.Close()
if err != nil {
return err
}
return c.readPool.Close()
}
func (c *Client) getWriteConnection() redis.Conn {
return c.writePool.Get()
}
func (c *Client) getReadConnection() redis.Conn {
return c.readPool.Get()
}
func toError(reply interface{}, err error) error {
_, _, e := toString(reply, err)
return e
}
func toBool(reply interface{}, err error) (bool, error) {
_, ok, e := toString(reply, err)
return ok, e
}
func toString(reply interface{}, err error) (string, bool, error) {
result, e := redis.String(reply, err)
if e == redis.ErrNil {
return result, false, nil
}
if e != nil {
return result, false, e
}
return result, true, nil
}
func parseScanResults(results []interface{}) (int64, []string, error) {
if len(results) != 2 {
return 0, []string{}, nil
}
cursorIndex, err := strconv.ParseInt(string(results[0].([]byte)), 10, 64)
if err != nil {
return 0, nil, err
}
keyInterfaces := results[1].([]interface{})
keys := make([]string, len(keyInterfaces))
for index, keyInterface := range keyInterfaces {
keys[index] = string(keyInterface.([]byte))
}
return cursorIndex, keys, nil
}