forked from alphazero/Go-Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
532 lines (401 loc) · 15.4 KB
/
redis.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright 2009-2012 Joubin Houshyar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package redis provides both clients and connectors for the Redis
// server. Both synchronous and asynchronous interaction modes are
// supported. Asynchronous clients (using the asynchronous connection)
// use pipelining.
//
// Synchronous semantics are defined by redis.Client interface type
//
//
// Usage example:
//
// func usingRedisSync () Error {
// spec := DefaultConnectionSpec();
// pipeline := NewAsynchClient(spec);
//
// value, reqErr := pipeline.Get("my-key");
// if reqErr != nil { return withError (reqErr); }
// }
//
// Asynchronous semantics are defined by redis.AsyncClient interface type.
// Note that these clients use a pipelining connector and a single instance
// can be used by multiple go routines. Use of pipelining increases throughput
// at the cost of latency. If low latency is more important to you than
// throughput, and you require async call semantics, then you should use only
// 1 go routine per AsyncClient connection.
//
// Usage example without timeouts (caller blocks on Get until the response
// from Redis has been processed.)
//
// func usingRedisAsync () Error {
// spec := DefaultConnectionSpec();
// pipeline := NewRedisPipeline(spec);
//
// // async invoke of GET my-key
// // futureBytes is a FutureBytes that will have the result of the
// // Redis GET operation.
// futureBytes, reqErr := pipline.Get("my-key");
// if reqErr != nil {
// return withError (reqErr);
// }
//
// // ... note that you could issue additional redis commands here ...
//
// []byte, execErr := futureBytes.Get();
// if execErr != nil {
// return withError (execErr);
// }
// }
//
// Usage example with timeouts - same Redis op as above but here we use
// TryGet on the Future result with a timeout of 1 msecs:
//
// func usingRedisAsync () Error {
// spec := DefaultConnectionSpec();
// pipeline := NewRedisPipeline(spec);
//
// // futureBytes is a FutureBytes
// futureBytes, reqErr := pipline.Get("my-key");
// if reqErr != nil { return withError (reqErr); }
//
// // ... note that you could issue additional redis commands here ...
//
// []byte, execErr := futureBytes.Get();
// if execErr != nil {
// return withError (execErr);
// }
//
// timeout := 1000000; // wait 1 msec for result
// []byte, execErr, ok := futureBytes.TryGet (timeout);
// if !ok {
// .. handle timeout here
// }
// else {
// if execErr != nil {
// return withError (execErr);
// }
// }
// }
//
package redis
import (
"flag"
"runtime";
)
// Common interface supported by all clients
// to consolidate common ops
type RedisClient interface {
// Redis QUIT command.
Quit() (err Error)
}
// The synchronous call semantics Client interface.
//
// Method names map one to one to the Redis command set.
// All methods may return an redis.Error, which is either
// a system error (runtime issue or bug) or Redis error (i.e. user error)
// See Error in this package for details of its interface.
//
// The synchronous client interface provides blocking call semantics supported by
// a distinct request/reply sequence at the connector level.
//
// Method names map one to one to the Redis command set.
//
// All methods may return an redis.Error, which is either a Redis error (from
// the server), or a system error indicating a runtime issue (or bug).
// See Error in this package for details of its interface.
type Client interface {
// psuedo inheritance to coerce to RedisClient type
// for the common API (not "algorithm", not "data", but interface ...)
RedisClient() RedisClient
// Redis GET command.
Get(key string) (result []byte, err Error)
// Redis TYPE command.
Type(key string) (result KeyType, err Error)
// Redis SET command.
Set(key string, arg1 []byte) Error
// Redis SAVE command.
Save() Error
// Redis KEYS command using "*" wildcard
AllKeys() (result []string, err Error)
// Redis KEYS command.
Keys(key string) (result []string, err Error)
// Redis EXISTS command.
Exists(key string) (result bool, err Error)
// Redis RENAME command.
Rename(key string, arg1 string) Error
// Redis INFO command.
Info() (result map[string]string, err Error)
// Redis PING command.
Ping() Error
// Redis QUIT command.
Quit() Error
// Redis SETNX command.
Setnx(key string, arg1 []byte) (result bool, err Error)
// Redis GETSET command.
Getset(key string, arg1 []byte) (result []byte, err Error)
// Redis MGET command.
Mget(key string, arg1 []string) (result [][]byte, err Error)
// Redis INCR command.
Incr(key string) (result int64, err Error)
// Redis INCRBY command.
Incrby(key string, arg1 int64) (result int64, err Error)
// Redis DECR command.
Decr(key string) (result int64, err Error)
// Redis DECRBY command.
Decrby(key string, arg1 int64) (result int64, err Error)
// Redis DEL command.
Del(key string) (result bool, err Error)
// Redis RANDOMKEY command.
Randomkey() (result string, err Error)
// Redis RENAMENX command.
Renamenx(key string, arg1 string) (result bool, err Error)
// Redis DBSIZE command.
Dbsize() (result int64, err Error)
// Redis EXPIRE command.
Expire(key string, arg1 int64) (result bool, err Error)
// Redis TTL command.
Ttl(key string) (result int64, err Error)
// Redis RPUSH command.
Rpush(key string, arg1 []byte) Error
// Redis LPUSH command.
Lpush(key string, arg1 []byte) Error
// Redis LSET command.
Lset(key string, arg1 int64, arg2 []byte) Error
// Redis LREM command.
Lrem(key string, arg1 []byte, arg2 int64) (result int64, err Error)
// Redis LLEN command.
Llen(key string) (result int64, err Error)
// Redis LRANGE command.
Lrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)
// Redis LTRIM command.
Ltrim(key string, arg1 int64, arg2 int64) Error
// Redis LINDEX command.
Lindex(key string, arg1 int64) (result []byte, err Error)
// Redis LPOP command.
Lpop(key string) (result []byte, err Error)
// Redis RPOP command.
Rpop(key string) (result []byte, err Error)
// Redis RPOPLPUSH command.
Rpoplpush(key string, arg1 string) (result []byte, err Error)
// Redis SADD command.
Sadd(key string, arg1 []byte) (result bool, err Error)
// Redis SREM command.
Srem(key string, arg1 []byte) (result bool, err Error)
// Redis SISMEMBER command.
Sismember(key string, arg1 []byte) (result bool, err Error)
// Redis SMOVE command.
Smove(key string, arg1 string, arg2 []byte) (result bool, err Error)
// Redis SCARD command.
Scard(key string) (result int64, err Error)
// Redis SINTER command.
Sinter(key string, arg1 []string) (result [][]byte, err Error)
// Redis SINTERSTORE command.
Sinterstore(key string, arg1 []string) Error
// Redis SUNION command.
Sunion(key string, arg1 []string) (result [][]byte, err Error)
// Redis SUNIONSTORE command.
Sunionstore(key string, arg1 []string) Error
// Redis SDIFF command.
Sdiff(key string, arg1 []string) (result [][]byte, err Error)
// Redis SDIFFSTORE command.
Sdiffstore(key string, arg1 []string) Error
// Redis SMEMBERS command.
Smembers(key string) (result [][]byte, err Error)
// Redis SRANDMEMBER command.
Srandmember(key string) (result []byte, err Error)
// Redis ZADD command.
Zadd(key string, arg1 float64, arg2 []byte) (result bool, err Error)
// Redis ZREM command.
Zrem(key string, arg1 []byte) (result bool, err Error)
// Redis ZCARD command.
Zcard(key string) (result int64, err Error)
// Redis ZSCORE command.
Zscore(key string, arg1 []byte) (result float64, err Error)
// Redis ZRANGE command.
Zrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)
// Redis ZREVRANGE command.
Zrevrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)
// Redis ZRANGEBYSCORE command.
Zrangebyscore(key string, arg1 float64, arg2 float64) (result [][]byte, err Error)
// Redis FLUSHDB command.
Flushdb() Error
// Redis FLUSHALL command.
Flushall() Error
// Redis MOVE command.
Move(key string, arg1 int64) (result bool, err Error)
// Redis BGSAVE command.
Bgsave() Error
// Redis LASTSAVE command.
Lastsave() (result int64, err Error)
}
// The asynchronous client interface provides asynchronous call semantics with
// future results supporting both blocking and try-and-timeout result accessors.
//
// Each method provides a type-safe future result return value, in addition to
// any (system) errors encountered in queuing the request.
//
// The returned value may be ignored by clients that are not interested in the
// future response (for example on SET("foo", data)). Alternatively, the caller
// may retain the future result referenced and perform blocking and/or timed wait
// gets on the expected response.
//
// Get() or TryGet() on the future result will return any Redis errors that were sent by
// the server, or, Go-Redis (system) errors encountered in processing the response.
type AsyncClient interface {
// psuedo inheritance to coerce to RedisClient type
// for the common API (not "algorithm", not "data", but interface ...)
RedisClient() RedisClient
// Redis GET command.
Get(key string) (result FutureBytes, err Error)
// Redis TYPE command.
Type(key string) (result FutureKeyType, err Error)
// Redis SET command.
Set(key string, arg1 []byte) (status FutureBool, err Error)
// Redis SAVE command.
Save() (status FutureBool, err Error)
// Redis KEYS command using "*" wildcard
AllKeys() (result FutureKeys, err Error)
// Redis KEYS command.
Keys(key string) (result FutureKeys, err Error)
// Redis EXISTS command.
Exists(key string) (result FutureBool, err Error)
// Redis RENAME command.
Rename(key string, arg1 string) (status FutureBool, err Error)
// Redis INFO command.
Info() (result FutureInfo, err Error)
// Redis PING command.
Ping() (status FutureBool, err Error)
// Redis QUIT command.
Quit() (err Error)
// Redis SETNX command.
Setnx(key string, arg1 []byte) (result FutureBool, err Error)
// Redis GETSET command.
Getset(key string, arg1 []byte) (result FutureBytes, err Error)
// Redis MGET command.
Mget(key string, arg1 []string) (result FutureBytesArray, err Error)
// Redis INCR command.
Incr(key string) (result FutureInt64, err Error)
// Redis INCRBY command.
Incrby(key string, arg1 int64) (result FutureInt64, err Error)
// Redis DECR command.
Decr(key string) (result FutureInt64, err Error)
// Redis DECRBY command.
Decrby(key string, arg1 int64) (result FutureInt64, err Error)
// Redis DEL command.
Del(key string) (result FutureBool, err Error)
// Redis RANDOMKEY command.
Randomkey() (result FutureString, err Error)
// Redis RENAMENX command.
Renamenx(key string, arg1 string) (result FutureBool, err Error)
// Redis DBSIZE command.
Dbsize() (result FutureInt64, err Error)
// Redis EXPIRE command.
Expire(key string, arg1 int64) (result FutureBool, err Error)
// Redis TTL command.
Ttl(key string) (result FutureInt64, err Error)
// Redis RPUSH command.
Rpush(key string, arg1 []byte) (status FutureBool, err Error)
// Redis LPUSH command.
Lpush(key string, arg1 []byte) (status FutureBool, err Error)
// Redis LSET command.
Lset(key string, arg1 int64, arg2 []byte) (status FutureBool, err Error)
// Redis LREM command.
Lrem(key string, arg1 []byte, arg2 int64) (result FutureInt64, err Error)
// Redis LLEN command.
Llen(key string) (result FutureInt64, err Error)
// Redis LRANGE command.
Lrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)
// Redis LTRIM command.
Ltrim(key string, arg1 int64, arg2 int64) (status FutureBool, err Error)
// Redis LINDEX command.
Lindex(key string, arg1 int64) (result FutureBytes, err Error)
// Redis LPOP command.
Lpop(key string) (result FutureBytes, err Error)
// Redis RPOP command.
Rpop(key string) (result FutureBytes, err Error)
// Redis RPOPLPUSH command.
Rpoplpush(key string, arg1 string) (result FutureBytes, err Error)
// Redis SADD command.
Sadd(key string, arg1 []byte) (result FutureBool, err Error)
// Redis SREM command.
Srem(key string, arg1 []byte) (result FutureBool, err Error)
// Redis SISMEMBER command.
Sismember(key string, arg1 []byte) (result FutureBool, err Error)
// Redis SMOVE command.
Smove(key string, arg1 string, arg2 []byte) (result FutureBool, err Error)
// Redis SCARD command.
Scard(key string) (result FutureInt64, err Error)
// Redis SINTER command.
Sinter(key string, arg1 []string) (result FutureBytesArray, err Error)
// Redis SINTERSTORE command.
Sinterstore(key string, arg1 []string) (status FutureBool, err Error)
// Redis SUNION command.
Sunion(key string, arg1 []string) (result FutureBytesArray, err Error)
// Redis SUNIONSTORE command.
Sunionstore(key string, arg1 []string) (status FutureBool, err Error)
// Redis SDIFF command.
Sdiff(key string, arg1 []string) (result FutureBytesArray, err Error)
// Redis SDIFFSTORE command.
Sdiffstore(key string, arg1 []string) (status FutureBool, err Error)
// Redis SMEMBERS command.
Smembers(key string) (result FutureBytesArray, err Error)
// Redis SRANDMEMBER command.
Srandmember(key string) (result FutureBytes, err Error)
// Redis ZADD command.
Zadd(key string, arg1 float64, arg2 []byte) (result FutureBool, err Error)
// Redis ZREM command.
Zrem(key string, arg1 []byte) (result FutureBool, err Error)
// Redis ZCARD command.
Zcard(key string) (result FutureInt64, err Error)
// Redis ZSCORE command.
Zscore(key string, arg1 []byte) (result FutureFloat64, err Error)
// Redis ZRANGE command.
Zrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)
// Redis ZREVRANGE command.
Zrevrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)
// Redis ZRANGEBYSCORE command.
Zrangebyscore(key string, arg1 float64, arg2 float64) (result FutureBytesArray, err Error)
// Redis FLUSHDB command.
Flushdb() (status FutureBool, err Error)
// Redis FLUSHALL command.
Flushall() (status FutureBool, err Error)
// Redis MOVE command.
Move(key string, arg1 int64) (result FutureBool, err Error)
// Redis BGSAVE command.
Bgsave() (status FutureBool, err Error)
// Redis LASTSAVE command.
Lastsave() (result FutureInt64, err Error)
}
// ----------------------------------------------------------------------------
// package initiatization and internal ops and flags
// ----------------------------------------------------------------------------
// ----------------
// flags
//
// go-redis will make use of command line flags where available. flag names
// for this package are all prefixed by "redis:" to prevent possible name collisions.
//
func init() {
runtime.GOMAXPROCS(2);
flag.Parse();
}
// redis:d
//
// global debug flag for redis package components.
//
var _debug *bool = flag.Bool("redis:d", false, "debug flag for go-redis") // TEMP: should default to false
func debug() bool {
return *_debug
}