Debian Codegraph @

commands.go

raw
1 package redis
2
3 import (
4 "io"
5 "strconv"
6 "time"
7
8 "gopkg.in/redis.v5/internal"
9 )
10
11 func readTimeout(timeout time.Duration) time.Duration {
12 if timeout == 0 {
13 return 0
14 }
15 return timeout + time.Second
16 }
17
18 func usePrecise(dur time.Duration) bool {
19 return dur < time.Second || dur%time.Second != 0
20 }
21
22 func formatMs(dur time.Duration) string {
23 if dur > 0 && dur < time.Millisecond {
24 internal.Logf(
25 "specified duration is %s, but minimal supported value is %s",
26 dur, time.Millisecond,
27 )
28 }
29 return strconv.FormatInt(int64(dur/time.Millisecond), 10)
30 }
31
32 func formatSec(dur time.Duration) string {
33 if dur > 0 && dur < time.Second {
34 internal.Logf(
35 "specified duration is %s, but minimal supported value is %s",
36 dur, time.Second,
37 )
38 }
39 return strconv.FormatInt(int64(dur/time.Second), 10)
40 }
41
42 type Cmdable interface {
43 Pipeline() *Pipeline
44 Pipelined(fn func(*Pipeline) error) ([]Cmder, error)
45
46 Echo(message interface{}) *StringCmd
47 Ping() *StatusCmd
48 Quit() *StatusCmd
49 Del(keys ...string) *IntCmd
50 Unlink(keys ...string) *IntCmd
51 Dump(key string) *StringCmd
52 Exists(key string) *BoolCmd
53 // TODO: merge with Exists in v6
54 ExistsMulti(keys ...string) *IntCmd
55 Expire(key string, expiration time.Duration) *BoolCmd
56 ExpireAt(key string, tm time.Time) *BoolCmd
57 Keys(pattern string) *StringSliceCmd
58 Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd
59 Move(key string, db int64) *BoolCmd
60 ObjectRefCount(key string) *IntCmd
61 ObjectEncoding(key string) *StringCmd
62 ObjectIdleTime(key string) *DurationCmd
63 Persist(key string) *BoolCmd
64 PExpire(key string, expiration time.Duration) *BoolCmd
65 PExpireAt(key string, tm time.Time) *BoolCmd
66 PTTL(key string) *DurationCmd
67 RandomKey() *StringCmd
68 Rename(key, newkey string) *StatusCmd
69 RenameNX(key, newkey string) *BoolCmd
70 Restore(key string, ttl time.Duration, value string) *StatusCmd
71 RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
72 Sort(key string, sort Sort) *StringSliceCmd
73 SortInterfaces(key string, sort Sort) *SliceCmd
74 TTL(key string) *DurationCmd
75 Type(key string) *StatusCmd
76 Scan(cursor uint64, match string, count int64) *ScanCmd
77 SScan(key string, cursor uint64, match string, count int64) *ScanCmd
78 HScan(key string, cursor uint64, match string, count int64) *ScanCmd
79 ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
80 Append(key, value string) *IntCmd
81 BitCount(key string, bitCount *BitCount) *IntCmd
82 BitOpAnd(destKey string, keys ...string) *IntCmd
83 BitOpOr(destKey string, keys ...string) *IntCmd
84 BitOpXor(destKey string, keys ...string) *IntCmd
85 BitOpNot(destKey string, key string) *IntCmd
86 BitPos(key string, bit int64, pos ...int64) *IntCmd
87 Decr(key string) *IntCmd
88 DecrBy(key string, decrement int64) *IntCmd
89 Get(key string) *StringCmd
90 GetBit(key string, offset int64) *IntCmd
91 GetRange(key string, start, end int64) *StringCmd
92 GetSet(key string, value interface{}) *StringCmd
93 Incr(key string) *IntCmd
94 IncrBy(key string, value int64) *IntCmd
95 IncrByFloat(key string, value float64) *FloatCmd
96 MGet(keys ...string) *SliceCmd
97 MSet(pairs ...interface{}) *StatusCmd
98 MSetNX(pairs ...interface{}) *BoolCmd
99 Set(key string, value interface{}, expiration time.Duration) *StatusCmd
100 SetBit(key string, offset int64, value int) *IntCmd
101 SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
102 SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd
103 SetRange(key string, offset int64, value string) *IntCmd
104 StrLen(key string) *IntCmd
105 HDel(key string, fields ...string) *IntCmd
106 HExists(key, field string) *BoolCmd
107 HGet(key, field string) *StringCmd
108 HGetAll(key string) *StringStringMapCmd
109 HIncrBy(key, field string, incr int64) *IntCmd
110 HIncrByFloat(key, field string, incr float64) *FloatCmd
111 HKeys(key string) *StringSliceCmd
112 HLen(key string) *IntCmd
113 HMGet(key string, fields ...string) *SliceCmd
114 HMSet(key string, fields map[string]string) *StatusCmd
115 HSet(key, field string, value interface{}) *BoolCmd
116 HSetNX(key, field string, value interface{}) *BoolCmd
117 HVals(key string) *StringSliceCmd
118 BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
119 BRPop(timeout time.Duration, keys ...string) *StringSliceCmd
120 BRPopLPush(source, destination string, timeout time.Duration) *StringCmd
121 LIndex(key string, index int64) *StringCmd
122 LInsert(key, op string, pivot, value interface{}) *IntCmd
123 LInsertBefore(key string, pivot, value interface{}) *IntCmd
124 LInsertAfter(key string, pivot, value interface{}) *IntCmd
125 LLen(key string) *IntCmd
126 LPop(key string) *StringCmd
127 LPush(key string, values ...interface{}) *IntCmd
128 LPushX(key string, value interface{}) *IntCmd
129 LRange(key string, start, stop int64) *StringSliceCmd
130 LRem(key string, count int64, value interface{}) *IntCmd
131 LSet(key string, index int64, value interface{}) *StatusCmd
132 LTrim(key string, start, stop int64) *StatusCmd
133 RPop(key string) *StringCmd
134 RPopLPush(source, destination string) *StringCmd
135 RPush(key string, values ...interface{}) *IntCmd
136 RPushX(key string, value interface{}) *IntCmd
137 SAdd(key string, members ...interface{}) *IntCmd
138 SCard(key string) *IntCmd
139 SDiff(keys ...string) *StringSliceCmd
140 SDiffStore(destination string, keys ...string) *IntCmd
141 SInter(keys ...string) *StringSliceCmd
142 SInterStore(destination string, keys ...string) *IntCmd
143 SIsMember(key string, member interface{}) *BoolCmd
144 SMembers(key string) *StringSliceCmd
145 SMove(source, destination string, member interface{}) *BoolCmd
146 SPop(key string) *StringCmd
147 SPopN(key string, count int64) *StringSliceCmd
148 SRandMember(key string) *StringCmd
149 SRandMemberN(key string, count int64) *StringSliceCmd
150 SRem(key string, members ...interface{}) *IntCmd
151 SUnion(keys ...string) *StringSliceCmd
152 SUnionStore(destination string, keys ...string) *IntCmd
153 ZAdd(key string, members ...Z) *IntCmd
154 ZAddNX(key string, members ...Z) *IntCmd
155 ZAddXX(key string, members ...Z) *IntCmd
156 ZAddCh(key string, members ...Z) *IntCmd
157 ZAddNXCh(key string, members ...Z) *IntCmd
158 ZAddXXCh(key string, members ...Z) *IntCmd
159 ZIncr(key string, member Z) *FloatCmd
160 ZIncrNX(key string, member Z) *FloatCmd
161 ZIncrXX(key string, member Z) *FloatCmd
162 ZCard(key string) *IntCmd
163 ZCount(key, min, max string) *IntCmd
164 ZIncrBy(key string, increment float64, member string) *FloatCmd
165 ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
166 ZRange(key string, start, stop int64) *StringSliceCmd
167 ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
168 ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
169 ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
170 ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
171 ZRank(key, member string) *IntCmd
172 ZRem(key string, members ...interface{}) *IntCmd
173 ZRemRangeByRank(key string, start, stop int64) *IntCmd
174 ZRemRangeByScore(key, min, max string) *IntCmd
175 ZRemRangeByLex(key, min, max string) *IntCmd
176 ZRevRange(key string, start, stop int64) *StringSliceCmd
177 ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
178 ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
179 ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
180 ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
181 ZRevRank(key, member string) *IntCmd
182 ZScore(key, member string) *FloatCmd
183 ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
184 PFAdd(key string, els ...interface{}) *IntCmd
185 PFCount(keys ...string) *IntCmd
186 PFMerge(dest string, keys ...string) *StatusCmd
187 BgRewriteAOF() *StatusCmd
188 BgSave() *StatusCmd
189 ClientKill(ipPort string) *StatusCmd
190 ClientList() *StringCmd
191 ClientPause(dur time.Duration) *BoolCmd
192 ConfigGet(parameter string) *SliceCmd
193 ConfigResetStat() *StatusCmd
194 ConfigSet(parameter, value string) *StatusCmd
195 DbSize() *IntCmd
196 FlushAll() *StatusCmd
197 FlushDb() *StatusCmd
198 Info(section ...string) *StringCmd
199 LastSave() *IntCmd
200 Save() *StatusCmd
201 Shutdown() *StatusCmd
202 ShutdownSave() *StatusCmd
203 ShutdownNoSave() *StatusCmd
204 SlaveOf(host, port string) *StatusCmd
205 Time() *TimeCmd
206 Eval(script string, keys []string, args ...interface{}) *Cmd
207 EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
208 ScriptExists(scripts ...string) *BoolSliceCmd
209 ScriptFlush() *StatusCmd
210 ScriptKill() *StatusCmd
211 ScriptLoad(script string) *StringCmd
212 DebugObject(key string) *StringCmd
213 PubSubChannels(pattern string) *StringSliceCmd
214 PubSubNumSub(channels ...string) *StringIntMapCmd
215 PubSubNumPat() *IntCmd
216 ClusterSlots() *ClusterSlotsCmd
217 ClusterNodes() *StringCmd
218 ClusterMeet(host, port string) *StatusCmd
219 ClusterForget(nodeID string) *StatusCmd
220 ClusterReplicate(nodeID string) *StatusCmd
221 ClusterResetSoft() *StatusCmd
222 ClusterResetHard() *StatusCmd
223 ClusterInfo() *StringCmd
224 ClusterKeySlot(key string) *IntCmd
225 ClusterCountFailureReports(nodeID string) *IntCmd
226 ClusterCountKeysInSlot(slot int) *IntCmd
227 ClusterDelSlots(slots ...int) *StatusCmd
228 ClusterDelSlotsRange(min, max int) *StatusCmd
229 ClusterSaveConfig() *StatusCmd
230 ClusterSlaves(nodeID string) *StringSliceCmd
231 ClusterFailover() *StatusCmd
232 ClusterAddSlots(slots ...int) *StatusCmd
233 ClusterAddSlotsRange(min, max int) *StatusCmd
234 GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
235 GeoPos(key string, members ...string) *GeoPosCmd
236 GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
237 GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
238 GeoDist(key string, member1, member2, unit string) *FloatCmd
239 GeoHash(key string, members ...string) *StringSliceCmd
240 Command() *CommandsInfoCmd
241 }
242
243 var _ Cmdable = (*Client)(nil)
244 var _ Cmdable = (*Tx)(nil)
245 var _ Cmdable = (*Ring)(nil)
246 var _ Cmdable = (*ClusterClient)(nil)
247
248 type cmdable struct {
249 process func(cmd Cmder) error
250 }
251
252 type statefulCmdable struct {
253 process func(cmd Cmder) error
254 }
255
256 //------------------------------------------------------------------------------
257
258 func (c *statefulCmdable) Auth(password string) *StatusCmd {
259 cmd := NewStatusCmd("auth", password)
260 c.process(cmd)
261 return cmd
262 }
263
264 func (c *cmdable) Echo(message interface{}) *StringCmd {
265 cmd := NewStringCmd("echo", message)
266 c.process(cmd)
267 return cmd
268 }
269
270 func (c *cmdable) Ping() *StatusCmd {
271 cmd := NewStatusCmd("ping")
272 c.process(cmd)
273 return cmd
274 }
275
276 func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
277
278 cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
279 c.process(cmd)
280 return cmd
281 }
282
283 func (c *cmdable) Quit() *StatusCmd {
284 panic("not implemented")
285 }
286
287 func (c *statefulCmdable) Select(index int) *StatusCmd {
288 cmd := NewStatusCmd("select", index)
289 c.process(cmd)
290 return cmd
291 }
292
293 //------------------------------------------------------------------------------
294
295 func (c *cmdable) Del(keys ...string) *IntCmd {
296 args := make([]interface{}, 1+len(keys))
297 args[0] = "del"
298 for i, key := range keys {
299 args[1+i] = key
300 }
301 cmd := NewIntCmd(args...)
302 c.process(cmd)
303 return cmd
304 }
305
306 func (c *cmdable) Unlink(keys ...string) *IntCmd {
307 args := make([]interface{}, 1+len(keys))
308 args[0] = "unlink"
309 for i, key := range keys {
310 args[1+i] = key
311 }
312 cmd := NewIntCmd(args...)
313 c.process(cmd)
314 return cmd
315 }
316
317 func (c *cmdable) Dump(key string) *StringCmd {
318 cmd := NewStringCmd("dump", key)
319 c.process(cmd)
320 return cmd
321 }
322
323 func (c *cmdable) Exists(key string) *BoolCmd {
324 cmd := NewBoolCmd("exists", key)
325 c.process(cmd)
326 return cmd
327 }
328
329 func (c *cmdable) ExistsMulti(keys ...string) *IntCmd {
330 args := make([]interface{}, 1+len(keys))
331 args[0] = "exists"
332 for i, key := range keys {
333 args[1+i] = key
334 }
335 cmd := NewIntCmd(args...)
336 c.process(cmd)
337 return cmd
338 }
339
340 func (c *cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
341 cmd := NewBoolCmd("expire", key, formatSec(expiration))
342 c.process(cmd)
343 return cmd
344 }
345
346 func (c *cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
347 cmd := NewBoolCmd("expireat", key, tm.Unix())
348 c.process(cmd)
349 return cmd
350 }
351
352 func (c *cmdable) Keys(pattern string) *StringSliceCmd {
353 cmd := NewStringSliceCmd("keys", pattern)
354 c.process(cmd)
355 return cmd
356 }
357
358 func (c *cmdable) Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd {
359 cmd := NewStatusCmd(
360 "migrate",
361 host,
362 port,
363 key,
364 db,
365 formatMs(timeout),
366 )
367 cmd.setReadTimeout(readTimeout(timeout))
368 c.process(cmd)
369 return cmd
370 }
371
372 func (c *cmdable) Move(key string, db int64) *BoolCmd {
373 cmd := NewBoolCmd("move", key, db)
374 c.process(cmd)
375 return cmd
376 }
377
378 func (c *cmdable) ObjectRefCount(key string) *IntCmd {
379 cmd := NewIntCmd("object", "refcount", key)
380 c.process(cmd)
381 return cmd
382 }
383
384 func (c *cmdable) ObjectEncoding(key string) *StringCmd {
385 cmd := NewStringCmd("object", "encoding", key)
386 c.process(cmd)
387 return cmd
388 }
389
390 func (c *cmdable) ObjectIdleTime(key string) *DurationCmd {
391 cmd := NewDurationCmd(time.Second, "object", "idletime", key)
392 c.process(cmd)
393 return cmd
394 }
395
396 func (c *cmdable) Persist(key string) *BoolCmd {
397 cmd := NewBoolCmd("persist", key)
398 c.process(cmd)
399 return cmd
400 }
401
402 func (c *cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
403 cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
404 c.process(cmd)
405 return cmd
406 }
407
408 func (c *cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
409 cmd := NewBoolCmd(
410 "pexpireat",
411 key,
412 tm.UnixNano()/int64(time.Millisecond),
413 )
414 c.process(cmd)
415 return cmd
416 }
417
418 func (c *cmdable) PTTL(key string) *DurationCmd {
419 cmd := NewDurationCmd(time.Millisecond, "pttl", key)
420 c.process(cmd)
421 return cmd
422 }
423
424 func (c *cmdable) RandomKey() *StringCmd {
425 cmd := NewStringCmd("randomkey")
426 c.process(cmd)
427 return cmd
428 }
429
430 func (c *cmdable) Rename(key, newkey string) *StatusCmd {
431 cmd := NewStatusCmd("rename", key, newkey)
432 c.process(cmd)
433 return cmd
434 }
435
436 func (c *cmdable) RenameNX(key, newkey string) *BoolCmd {
437 cmd := NewBoolCmd("renamenx", key, newkey)
438 c.process(cmd)
439 return cmd
440 }
441
442 func (c *cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
443 cmd := NewStatusCmd(
444 "restore",
445 key,
446 formatMs(ttl),
447 value,
448 )
449 c.process(cmd)
450 return cmd
451 }
452
453 func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
454 cmd := NewStatusCmd(
455 "restore",
456 key,
457 formatMs(ttl),
458 value,
459 "replace",
460 )
461 c.process(cmd)
462 return cmd
463 }
464
465 type Sort struct {
466 By string
467 Offset, Count float64
468 Get []string
469 Order string
470 IsAlpha bool
471 Store string
472 }
473
474 func (sort *Sort) args(key string) []interface{} {
475 args := []interface{}{"sort", key}
476 if sort.By != "" {
477 args = append(args, "by", sort.By)
478 }
479 if sort.Offset != 0 || sort.Count != 0 {
480 args = append(args, "limit", sort.Offset, sort.Count)
481 }
482 for _, get := range sort.Get {
483 args = append(args, "get", get)
484 }
485 if sort.Order != "" {
486 args = append(args, sort.Order)
487 }
488 if sort.IsAlpha {
489 args = append(args, "alpha")
490 }
491 if sort.Store != "" {
492 args = append(args, "store", sort.Store)
493 }
494 return args
495 }
496
497 func (c *cmdable) Sort(key string, sort Sort) *StringSliceCmd {
498 cmd := NewStringSliceCmd(sort.args(key)...)
499 c.process(cmd)
500 return cmd
501 }
502
503 func (c *cmdable) SortInterfaces(key string, sort Sort) *SliceCmd {
504 cmd := NewSliceCmd(sort.args(key)...)
505 c.process(cmd)
506 return cmd
507 }
508
509 func (c *cmdable) TTL(key string) *DurationCmd {
510 cmd := NewDurationCmd(time.Second, "ttl", key)
511 c.process(cmd)
512 return cmd
513 }
514
515 func (c *cmdable) Type(key string) *StatusCmd {
516 cmd := NewStatusCmd("type", key)
517 c.process(cmd)
518 return cmd
519 }
520
521 func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
522 args := []interface{}{"scan", cursor}
523 if match != "" {
524 args = append(args, "match", match)
525 }
526 if count > 0 {
527 args = append(args, "count", count)
528 }
529 cmd := NewScanCmd(c.process, args...)
530 c.process(cmd)
531 return cmd
532 }
533
534 func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
535 args := []interface{}{"sscan", key, cursor}
536 if match != "" {
537 args = append(args, "match", match)
538 }
539 if count > 0 {
540 args = append(args, "count", count)
541 }
542 cmd := NewScanCmd(c.process, args...)
543 c.process(cmd)
544 return cmd
545 }
546
547 func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
548 args := []interface{}{"hscan", key, cursor}
549 if match != "" {
550 args = append(args, "match", match)
551 }
552 if count > 0 {
553 args = append(args, "count", count)
554 }
555 cmd := NewScanCmd(c.process, args...)
556 c.process(cmd)
557 return cmd
558 }
559
560 func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
561 args := []interface{}{"zscan", key, cursor}
562 if match != "" {
563 args = append(args, "match", match)
564 }
565 if count > 0 {
566 args = append(args, "count", count)
567 }
568 cmd := NewScanCmd(c.process, args...)
569 c.process(cmd)
570 return cmd
571 }
572
573 //------------------------------------------------------------------------------
574
575 func (c *cmdable) Append(key, value string) *IntCmd {
576 cmd := NewIntCmd("append", key, value)
577 c.process(cmd)
578 return cmd
579 }
580
581 type BitCount struct {
582 Start, End int64
583 }
584
585 func (c *cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
586 args := []interface{}{"bitcount", key}
587 if bitCount != nil {
588 args = append(
589 args,
590 bitCount.Start,
591 bitCount.End,
592 )
593 }
594 cmd := NewIntCmd(args...)
595 c.process(cmd)
596 return cmd
597 }
598
599 func (c *cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
600 args := make([]interface{}, 3+len(keys))
601 args[0] = "bitop"
602 args[1] = op
603 args[2] = destKey
604 for i, key := range keys {
605 args[3+i] = key
606 }
607 cmd := NewIntCmd(args...)
608 c.process(cmd)
609 return cmd
610 }
611
612 func (c *cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
613 return c.bitOp("and", destKey, keys...)
614 }
615
616 func (c *cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
617 return c.bitOp("or", destKey, keys...)
618 }
619
620 func (c *cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
621 return c.bitOp("xor", destKey, keys...)
622 }
623
624 func (c *cmdable) BitOpNot(destKey string, key string) *IntCmd {
625 return c.bitOp("not", destKey, key)
626 }
627
628 func (c *cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
629 args := make([]interface{}, 3+len(pos))
630 args[0] = "bitpos"
631 args[1] = key
632 args[2] = bit
633 switch len(pos) {
634 case 0:
635 case 1:
636 args[3] = pos[0]
637 case 2:
638 args[3] = pos[0]
639 args[4] = pos[1]
640 default:
641 panic("too many arguments")
642 }
643 cmd := NewIntCmd(args...)
644 c.process(cmd)
645 return cmd
646 }
647
648 func (c *cmdable) Decr(key string) *IntCmd {
649 cmd := NewIntCmd("decr", key)
650 c.process(cmd)
651 return cmd
652 }
653
654 func (c *cmdable) DecrBy(key string, decrement int64) *IntCmd {
655 cmd := NewIntCmd("decrby", key, decrement)
656 c.process(cmd)
657 return cmd
658 }
659
660 func (c *cmdable) Get(key string) *StringCmd {
661 cmd := NewStringCmd("get", key)
662 c.process(cmd)
663 return cmd
664 }
665
666 func (c *cmdable) GetBit(key string, offset int64) *IntCmd {
667 cmd := NewIntCmd("getbit", key, offset)
668 c.process(cmd)
669 return cmd
670 }
671
672 func (c *cmdable) GetRange(key string, start, end int64) *StringCmd {
673 cmd := NewStringCmd("getrange", key, start, end)
674 c.process(cmd)
675 return cmd
676 }
677
678 func (c *cmdable) GetSet(key string, value interface{}) *StringCmd {
679 cmd := NewStringCmd("getset", key, value)
680 c.process(cmd)
681 return cmd
682 }
683
684 func (c *cmdable) Incr(key string) *IntCmd {
685 cmd := NewIntCmd("incr", key)
686 c.process(cmd)
687 return cmd
688 }
689
690 func (c *cmdable) IncrBy(key string, value int64) *IntCmd {
691 cmd := NewIntCmd("incrby", key, value)
692 c.process(cmd)
693 return cmd
694 }
695
696 func (c *cmdable) IncrByFloat(key string, value float64) *FloatCmd {
697 cmd := NewFloatCmd("incrbyfloat", key, value)
698 c.process(cmd)
699 return cmd
700 }
701
702 func (c *cmdable) MGet(keys ...string) *SliceCmd {
703 args := make([]interface{}, 1+len(keys))
704 args[0] = "mget"
705 for i, key := range keys {
706 args[1+i] = key
707 }
708 cmd := NewSliceCmd(args...)
709 c.process(cmd)
710 return cmd
711 }
712
713 func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
714 args := make([]interface{}, 1+len(pairs))
715 args[0] = "mset"
716 for i, pair := range pairs {
717 args[1+i] = pair
718 }
719 cmd := NewStatusCmd(args...)
720 c.process(cmd)
721 return cmd
722 }
723
724 func (c *cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
725 args := make([]interface{}, 1+len(pairs))
726 args[0] = "msetnx"
727 for i, pair := range pairs {
728 args[1+i] = pair
729 }
730 cmd := NewBoolCmd(args...)
731 c.process(cmd)
732 return cmd
733 }
734
735 // Redis `SET key value [expiration]` command.
736 //
737 // Use expiration for `SETEX`-like behavior.
738 // Zero expiration means the key has no expiration time.
739 func (c *cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
740 args := make([]interface{}, 3, 4)
741 args[0] = "set"
742 args[1] = key
743 args[2] = value
744 if expiration > 0 {
745 if usePrecise(expiration) {
746 args = append(args, "px", formatMs(expiration))
747 } else {
748 args = append(args, "ex", formatSec(expiration))
749 }
750 }
751 cmd := NewStatusCmd(args...)
752 c.process(cmd)
753 return cmd
754 }
755
756 func (c *cmdable) SetBit(key string, offset int64, value int) *IntCmd {
757 cmd := NewIntCmd(
758 "setbit",
759 key,
760 offset,
761 value,
762 )
763 c.process(cmd)
764 return cmd
765 }
766
767 // Redis `SET key value [expiration] NX` command.
768 //
769 // Zero expiration means the key has no expiration time.
770 func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
771 var cmd *BoolCmd
772 if expiration == 0 {
773 // Use old `SETNX` to support old Redis versions.
774 cmd = NewBoolCmd("setnx", key, value)
775 } else {
776 if usePrecise(expiration) {
777 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
778 } else {
779 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
780 }
781 }
782 c.process(cmd)
783 return cmd
784 }
785
786 // Redis `SET key value [expiration] XX` command.
787 //
788 // Zero expiration means the key has no expiration time.
789 func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
790 var cmd *BoolCmd
791 if expiration == 0 {
792 cmd = NewBoolCmd("set", key, value, "xx")
793 } else {
794 if usePrecise(expiration) {
795 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
796 } else {
797 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
798 }
799 }
800 c.process(cmd)
801 return cmd
802 }
803
804 func (c *cmdable) SetRange(key string, offset int64, value string) *IntCmd {
805 cmd := NewIntCmd("setrange", key, offset, value)
806 c.process(cmd)
807 return cmd
808 }
809
810 func (c *cmdable) StrLen(key string) *IntCmd {
811 cmd := NewIntCmd("strlen", key)
812 c.process(cmd)
813 return cmd
814 }
815
816 //------------------------------------------------------------------------------
817
818 func (c *cmdable) HDel(key string, fields ...string) *IntCmd {
819 args := make([]interface{}, 2+len(fields))
820 args[0] = "hdel"
821 args[1] = key
822 for i, field := range fields {
823 args[2+i] = field
824 }
825 cmd := NewIntCmd(args...)
826 c.process(cmd)
827 return cmd
828 }
829
830 func (c *cmdable) HExists(key, field string) *BoolCmd {
831 cmd := NewBoolCmd("hexists", key, field)
832 c.process(cmd)
833 return cmd
834 }
835
836 func (c *cmdable) HGet(key, field string) *StringCmd {
837 cmd := NewStringCmd("hget", key, field)
838 c.process(cmd)
839 return cmd
840 }
841
842 func (c *cmdable) HGetAll(key string) *StringStringMapCmd {
843 cmd := NewStringStringMapCmd("hgetall", key)
844 c.process(cmd)
845 return cmd
846 }
847
848 func (c *cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
849 cmd := NewIntCmd("hincrby", key, field, incr)
850 c.process(cmd)
851 return cmd
852 }
853
854 func (c *cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
855 cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
856 c.process(cmd)
857 return cmd
858 }
859
860 func (c *cmdable) HKeys(key string) *StringSliceCmd {
861 cmd := NewStringSliceCmd("hkeys", key)
862 c.process(cmd)
863 return cmd
864 }
865
866 func (c *cmdable) HLen(key string) *IntCmd {
867 cmd := NewIntCmd("hlen", key)
868 c.process(cmd)
869 return cmd
870 }
871
872 func (c *cmdable) HMGet(key string, fields ...string) *SliceCmd {
873 args := make([]interface{}, 2+len(fields))
874 args[0] = "hmget"
875 args[1] = key
876 for i, field := range fields {
877 args[2+i] = field
878 }
879 cmd := NewSliceCmd(args...)
880 c.process(cmd)
881 return cmd
882 }
883
884 func (c *cmdable) HMSet(key string, fields map[string]string) *StatusCmd {
885 args := make([]interface{}, 2+len(fields)*2)
886 args[0] = "hmset"
887 args[1] = key
888 i := 2
889 for k, v := range fields {
890 args[i] = k
891 args[i+1] = v
892 i += 2
893 }
894 cmd := NewStatusCmd(args...)
895 c.process(cmd)
896 return cmd
897 }
898
899 func (c *cmdable) HSet(key, field string, value interface{}) *BoolCmd {
900 cmd := NewBoolCmd("hset", key, field, value)
901 c.process(cmd)
902 return cmd
903 }
904
905 func (c *cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
906 cmd := NewBoolCmd("hsetnx", key, field, value)
907 c.process(cmd)
908 return cmd
909 }
910
911 func (c *cmdable) HVals(key string) *StringSliceCmd {
912 cmd := NewStringSliceCmd("hvals", key)
913 c.process(cmd)
914 return cmd
915 }
916
917 //------------------------------------------------------------------------------
918
919 func (c *cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
920 args := make([]interface{}, 1+len(keys)+1)
921 args[0] = "blpop"
922 for i, key := range keys {
923 args[1+i] = key
924 }
925 args[len(args)-1] = formatSec(timeout)
926 cmd := NewStringSliceCmd(args...)
927 cmd.setReadTimeout(readTimeout(timeout))
928 c.process(cmd)
929 return cmd
930 }
931
932 func (c *cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
933 args := make([]interface{}, 1+len(keys)+1)
934 args[0] = "brpop"
935 for i, key := range keys {
936 args[1+i] = key
937 }
938 args[len(keys)+1] = formatSec(timeout)
939 cmd := NewStringSliceCmd(args...)
940 cmd.setReadTimeout(readTimeout(timeout))
941 c.process(cmd)
942 return cmd
943 }
944
945 func (c *cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
946 cmd := NewStringCmd(
947 "brpoplpush",
948 source,
949 destination,
950 formatSec(timeout),
951 )
952 cmd.setReadTimeout(readTimeout(timeout))
953 c.process(cmd)
954 return cmd
955 }
956
957 func (c *cmdable) LIndex(key string, index int64) *StringCmd {
958 cmd := NewStringCmd("lindex", key, index)
959 c.process(cmd)
960 return cmd
961 }
962
963 func (c *cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
964 cmd := NewIntCmd("linsert", key, op, pivot, value)
965 c.process(cmd)
966 return cmd
967 }
968
969 func (c *cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
970 cmd := NewIntCmd("linsert", key, "before", pivot, value)
971 c.process(cmd)
972 return cmd
973 }
974
975 func (c *cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
976 cmd := NewIntCmd("linsert", key, "after", pivot, value)
977 c.process(cmd)
978 return cmd
979 }
980
981 func (c *cmdable) LLen(key string) *IntCmd {
982 cmd := NewIntCmd("llen", key)
983 c.process(cmd)
984 return cmd
985 }
986
987 func (c *cmdable) LPop(key string) *StringCmd {
988 cmd := NewStringCmd("lpop", key)
989 c.process(cmd)
990 return cmd
991 }
992
993 func (c *cmdable) LPush(key string, values ...interface{}) *IntCmd {
994 args := make([]interface{}, 2+len(values))
995 args[0] = "lpush"
996 args[1] = key
997 for i, value := range values {
998 args[2+i] = value
999 }
1000 cmd := NewIntCmd(args...)
1001 c.process(cmd)
1002 return cmd
1003 }
1004
1005 func (c *cmdable) LPushX(key string, value interface{}) *IntCmd {
1006 cmd := NewIntCmd("lpushx", key, value)
1007 c.process(cmd)
1008 return cmd
1009 }
1010
1011 func (c *cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
1012 cmd := NewStringSliceCmd(
1013 "lrange",
1014 key,
1015 start,
1016 stop,
1017 )
1018 c.process(cmd)
1019 return cmd
1020 }
1021
1022 func (c *cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
1023 cmd := NewIntCmd("lrem", key, count, value)
1024 c.process(cmd)
1025 return cmd
1026 }
1027
1028 func (c *cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
1029 cmd := NewStatusCmd("lset", key, index, value)
1030 c.process(cmd)
1031 return cmd
1032 }
1033
1034 func (c *cmdable) LTrim(key string, start, stop int64) *StatusCmd {
1035 cmd := NewStatusCmd(
1036 "ltrim",
1037 key,
1038 start,
1039 stop,
1040 )
1041 c.process(cmd)
1042 return cmd
1043 }
1044
1045 func (c *cmdable) RPop(key string) *StringCmd {
1046 cmd := NewStringCmd("rpop", key)
1047 c.process(cmd)
1048 return cmd
1049 }
1050
1051 func (c *cmdable) RPopLPush(source, destination string) *StringCmd {
1052 cmd := NewStringCmd("rpoplpush", source, destination)
1053 c.process(cmd)
1054 return cmd
1055 }
1056
1057 func (c *cmdable) RPush(key string, values ...interface{}) *IntCmd {
1058 args := make([]interface{}, 2+len(values))
1059 args[0] = "rpush"
1060 args[1] = key
1061 for i, value := range values {
1062 args[2+i] = value
1063 }
1064 cmd := NewIntCmd(args...)
1065 c.process(cmd)
1066 return cmd
1067 }
1068
1069 func (c *cmdable) RPushX(key string, value interface{}) *IntCmd {
1070 cmd := NewIntCmd("rpushx", key, value)
1071 c.process(cmd)
1072 return cmd
1073 }
1074
1075 //------------------------------------------------------------------------------
1076
1077 func (c *cmdable) SAdd(key string, members ...interface{}) *IntCmd {
1078 args := make([]interface{}, 2+len(members))
1079 args[0] = "sadd"
1080 args[1] = key
1081 for i, member := range members {
1082 args[2+i] = member
1083 }
1084 cmd := NewIntCmd(args...)
1085 c.process(cmd)
1086 return cmd
1087 }
1088
1089 func (c *cmdable) SCard(key string) *IntCmd {
1090 cmd := NewIntCmd("scard", key)
1091 c.process(cmd)
1092 return cmd
1093 }
1094
1095 func (c *cmdable) SDiff(keys ...string) *StringSliceCmd {
1096 args := make([]interface{}, 1+len(keys))
1097 args[0] = "sdiff"
1098 for i, key := range keys {
1099 args[1+i] = key
1100 }
1101 cmd := NewStringSliceCmd(args...)
1102 c.process(cmd)
1103 return cmd
1104 }
1105
1106 func (c *cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
1107 args := make([]interface{}, 2+len(keys))
1108 args[0] = "sdiffstore"
1109 args[1] = destination
1110 for i, key := range keys {
1111 args[2+i] = key
1112 }
1113 cmd := NewIntCmd(args...)
1114 c.process(cmd)
1115 return cmd
1116 }
1117
1118 func (c *cmdable) SInter(keys ...string) *StringSliceCmd {
1119 args := make([]interface{}, 1+len(keys))
1120 args[0] = "sinter"
1121 for i, key := range keys {
1122 args[1+i] = key
1123 }
1124 cmd := NewStringSliceCmd(args...)
1125 c.process(cmd)
1126 return cmd
1127 }
1128
1129 func (c *cmdable) SInterStore(destination string, keys ...string) *IntCmd {
1130 args := make([]interface{}, 2+len(keys))
1131 args[0] = "sinterstore"
1132 args[1] = destination
1133 for i, key := range keys {
1134 args[2+i] = key
1135 }
1136 cmd := NewIntCmd(args...)
1137 c.process(cmd)
1138 return cmd
1139 }
1140
1141 func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
1142 cmd := NewBoolCmd("sismember", key, member)
1143 c.process(cmd)
1144 return cmd
1145 }
1146
1147 func (c *cmdable) SMembers(key string) *StringSliceCmd {
1148 cmd := NewStringSliceCmd("smembers", key)
1149 c.process(cmd)
1150 return cmd
1151 }
1152
1153 func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
1154 cmd := NewBoolCmd("smove", source, destination, member)
1155 c.process(cmd)
1156 return cmd
1157 }
1158
1159 // Redis `SPOP key` command.
1160 func (c *cmdable) SPop(key string) *StringCmd {
1161 cmd := NewStringCmd("spop", key)
1162 c.process(cmd)
1163 return cmd
1164 }
1165
1166 // Redis `SPOP key count` command.
1167 func (c *cmdable) SPopN(key string, count int64) *StringSliceCmd {
1168 cmd := NewStringSliceCmd("spop", key, count)
1169 c.process(cmd)
1170 return cmd
1171 }
1172
1173 // Redis `SRANDMEMBER key` command.
1174 func (c *cmdable) SRandMember(key string) *StringCmd {
1175 cmd := NewStringCmd("srandmember", key)
1176 c.process(cmd)
1177 return cmd
1178 }
1179
1180 // Redis `SRANDMEMBER key count` command.
1181 func (c *cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
1182 cmd := NewStringSliceCmd("srandmember", key, count)
1183 c.process(cmd)
1184 return cmd
1185 }
1186
1187 func (c *cmdable) SRem(key string, members ...interface{}) *IntCmd {
1188 args := make([]interface{}, 2+len(members))
1189 args[0] = "srem"
1190 args[1] = key
1191 for i, member := range members {
1192 args[2+i] = member
1193 }
1194 cmd := NewIntCmd(args...)
1195 c.process(cmd)
1196 return cmd
1197 }
1198
1199 func (c *cmdable) SUnion(keys ...string) *StringSliceCmd {
1200 args := make([]interface{}, 1+len(keys))
1201 args[0] = "sunion"
1202 for i, key := range keys {
1203 args[1+i] = key
1204 }
1205 cmd := NewStringSliceCmd(args...)
1206 c.process(cmd)
1207 return cmd
1208 }
1209
1210 func (c *cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
1211 args := make([]interface{}, 2+len(keys))
1212 args[0] = "sunionstore"
1213 args[1] = destination
1214 for i, key := range keys {
1215 args[2+i] = key
1216 }
1217 cmd := NewIntCmd(args...)
1218 c.process(cmd)
1219 return cmd
1220 }
1221
1222 //------------------------------------------------------------------------------
1223
1224 // Z represents sorted set member.
1225 type Z struct {
1226 Score float64
1227 Member interface{}
1228 }
1229
1230 // ZStore is used as an arg to ZInterStore and ZUnionStore.
1231 type ZStore struct {
1232 Weights []float64
1233 // Can be SUM, MIN or MAX.
1234 Aggregate string
1235 }
1236
1237 func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
1238 for i, m := range members {
1239 a[n+2*i] = m.Score
1240 a[n+2*i+1] = m.Member
1241 }
1242 cmd := NewIntCmd(a...)
1243 c.process(cmd)
1244 return cmd
1245 }
1246
1247 // Redis `ZADD key score member [score member ...]` command.
1248 func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
1249 const n = 2
1250 a := make([]interface{}, n+2*len(members))
1251 a[0], a[1] = "zadd", key
1252 return c.zAdd(a, n, members...)
1253 }
1254
1255 // Redis `ZADD key NX score member [score member ...]` command.
1256 func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
1257 const n = 3
1258 a := make([]interface{}, n+2*len(members))
1259 a[0], a[1], a[2] = "zadd", key, "nx"
1260 return c.zAdd(a, n, members...)
1261 }
1262
1263 // Redis `ZADD key XX score member [score member ...]` command.
1264 func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
1265 const n = 3
1266 a := make([]interface{}, n+2*len(members))
1267 a[0], a[1], a[2] = "zadd", key, "xx"
1268 return c.zAdd(a, n, members...)
1269 }
1270
1271 // Redis `ZADD key CH score member [score member ...]` command.
1272 func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
1273 const n = 3
1274 a := make([]interface{}, n+2*len(members))
1275 a[0], a[1], a[2] = "zadd", key, "ch"
1276 return c.zAdd(a, n, members...)
1277 }
1278
1279 // Redis `ZADD key NX CH score member [score member ...]` command.
1280 func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
1281 const n = 4
1282 a := make([]interface{}, n+2*len(members))
1283 a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
1284 return c.zAdd(a, n, members...)
1285 }
1286
1287 // Redis `ZADD key XX CH score member [score member ...]` command.
1288 func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
1289 const n = 4
1290 a := make([]interface{}, n+2*len(members))
1291 a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
1292 return c.zAdd(a, n, members...)
1293 }
1294
1295 func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
1296 for i, m := range members {
1297 a[n+2*i] = m.Score
1298 a[n+2*i+1] = m.Member
1299 }
1300 cmd := NewFloatCmd(a...)
1301 c.process(cmd)
1302 return cmd
1303 }
1304
1305 // Redis `ZADD key INCR score member` command.
1306 func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
1307 const n = 3
1308 a := make([]interface{}, n+2)
1309 a[0], a[1], a[2] = "zadd", key, "incr"
1310 return c.zIncr(a, n, member)
1311 }
1312
1313 // Redis `ZADD key NX INCR score member` command.
1314 func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
1315 const n = 4
1316 a := make([]interface{}, n+2)
1317 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
1318 return c.zIncr(a, n, member)
1319 }
1320
1321 // Redis `ZADD key XX INCR score member` command.
1322 func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
1323 const n = 4
1324 a := make([]interface{}, n+2)
1325 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
1326 return c.zIncr(a, n, member)
1327 }
1328
1329 func (c *cmdable) ZCard(key string) *IntCmd {
1330 cmd := NewIntCmd("zcard", key)
1331 c.process(cmd)
1332 return cmd
1333 }
1334
1335 func (c *cmdable) ZCount(key, min, max string) *IntCmd {
1336 cmd := NewIntCmd("zcount", key, min, max)
1337 c.process(cmd)
1338 return cmd
1339 }
1340
1341 func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
1342 cmd := NewFloatCmd("zincrby", key, increment, member)
1343 c.process(cmd)
1344 return cmd
1345 }
1346
1347 func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
1348 args := make([]interface{}, 3+len(keys))
1349 args[0] = "zinterstore"
1350 args[1] = destination
1351 args[2] = strconv.Itoa(len(keys))
1352 for i, key := range keys {
1353 args[3+i] = key
1354 }
1355 if len(store.Weights) > 0 {
1356 args = append(args, "weights")
1357 for _, weight := range store.Weights {
1358 args = append(args, weight)
1359 }
1360 }
1361 if store.Aggregate != "" {
1362 args = append(args, "aggregate", store.Aggregate)
1363 }
1364 cmd := NewIntCmd(args...)
1365 c.process(cmd)
1366 return cmd
1367 }
1368
1369 func (c *cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
1370 args := []interface{}{
1371 "zrange",
1372 key,
1373 start,
1374 stop,
1375 }
1376 if withScores {
1377 args = append(args, "withscores")
1378 }
1379 cmd := NewStringSliceCmd(args...)
1380 c.process(cmd)
1381 return cmd
1382 }
1383
1384 func (c *cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
1385 return c.zRange(key, start, stop, false)
1386 }
1387
1388 func (c *cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1389 cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
1390 c.process(cmd)
1391 return cmd
1392 }
1393
1394 type ZRangeBy struct {
1395 Min, Max string
1396 Offset, Count int64
1397 }
1398
1399 func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
1400 args := []interface{}{zcmd, key, opt.Min, opt.Max}
1401 if withScores {
1402 args = append(args, "withscores")
1403 }
1404 if opt.Offset != 0 || opt.Count != 0 {
1405 args = append(
1406 args,
1407 "limit",
1408 opt.Offset,
1409 opt.Count,
1410 )
1411 }
1412 cmd := NewStringSliceCmd(args...)
1413 c.process(cmd)
1414 return cmd
1415 }
1416
1417 func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1418 return c.zRangeBy("zrangebyscore", key, opt, false)
1419 }
1420
1421 func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1422 return c.zRangeBy("zrangebylex", key, opt, false)
1423 }
1424
1425 func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1426 args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
1427 if opt.Offset != 0 || opt.Count != 0 {
1428 args = append(
1429 args,
1430 "limit",
1431 opt.Offset,
1432 opt.Count,
1433 )
1434 }
1435 cmd := NewZSliceCmd(args...)
1436 c.process(cmd)
1437 return cmd
1438 }
1439
1440 func (c *cmdable) ZRank(key, member string) *IntCmd {
1441 cmd := NewIntCmd("zrank", key, member)
1442 c.process(cmd)
1443 return cmd
1444 }
1445
1446 func (c *cmdable) ZRem(key string, members ...interface{}) *IntCmd {
1447 args := make([]interface{}, 2+len(members))
1448 args[0] = "zrem"
1449 args[1] = key
1450 for i, member := range members {
1451 args[2+i] = member
1452 }
1453 cmd := NewIntCmd(args...)
1454 c.process(cmd)
1455 return cmd
1456 }
1457
1458 func (c *cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
1459 cmd := NewIntCmd(
1460 "zremrangebyrank",
1461 key,
1462 start,
1463 stop,
1464 )
1465 c.process(cmd)
1466 return cmd
1467 }
1468
1469 func (c *cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
1470 cmd := NewIntCmd("zremrangebyscore", key, min, max)
1471 c.process(cmd)
1472 return cmd
1473 }
1474
1475 func (c *cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
1476 cmd := NewIntCmd("zremrangebylex", key, min, max)
1477 c.process(cmd)
1478 return cmd
1479 }
1480
1481 func (c *cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
1482 cmd := NewStringSliceCmd("zrevrange", key, start, stop)
1483 c.process(cmd)
1484 return cmd
1485 }
1486
1487 func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1488 cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
1489 c.process(cmd)
1490 return cmd
1491 }
1492
1493 func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
1494 args := []interface{}{zcmd, key, opt.Max, opt.Min}
1495 if opt.Offset != 0 || opt.Count != 0 {
1496 args = append(
1497 args,
1498 "limit",
1499 opt.Offset,
1500 opt.Count,
1501 )
1502 }
1503 cmd := NewStringSliceCmd(args...)
1504 c.process(cmd)
1505 return cmd
1506 }
1507
1508 func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1509 return c.zRevRangeBy("zrevrangebyscore", key, opt)
1510 }
1511
1512 func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1513 return c.zRevRangeBy("zrevrangebylex", key, opt)
1514 }
1515
1516 func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1517 args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
1518 if opt.Offset != 0 || opt.Count != 0 {
1519 args = append(
1520 args,
1521 "limit",
1522 opt.Offset,
1523 opt.Count,
1524 )
1525 }
1526 cmd := NewZSliceCmd(args...)
1527 c.process(cmd)
1528 return cmd
1529 }
1530
1531 func (c *cmdable) ZRevRank(key, member string) *IntCmd {
1532 cmd := NewIntCmd("zrevrank", key, member)
1533 c.process(cmd)
1534 return cmd
1535 }
1536
1537 func (c *cmdable) ZScore(key, member string) *FloatCmd {
1538 cmd := NewFloatCmd("zscore", key, member)
1539 c.process(cmd)
1540 return cmd
1541 }
1542
1543 func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
1544 args := make([]interface{}, 3+len(keys))
1545 args[0] = "zunionstore"
1546 args[1] = dest
1547 args[2] = strconv.Itoa(len(keys))
1548 for i, key := range keys {
1549 args[3+i] = key
1550 }
1551 if len(store.Weights) > 0 {
1552 args = append(args, "weights")
1553 for _, weight := range store.Weights {
1554 args = append(args, weight)
1555 }
1556 }
1557 if store.Aggregate != "" {
1558 args = append(args, "aggregate", store.Aggregate)
1559 }
1560 cmd := NewIntCmd(args...)
1561 c.process(cmd)
1562 return cmd
1563 }
1564
1565 //------------------------------------------------------------------------------
1566
1567 func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
1568 args := make([]interface{}, 2+len(els))
1569 args[0] = "pfadd"
1570 args[1] = key
1571 for i, el := range els {
1572 args[2+i] = el
1573 }
1574 cmd := NewIntCmd(args...)
1575 c.process(cmd)
1576 return cmd
1577 }
1578
1579 func (c *cmdable) PFCount(keys ...string) *IntCmd {
1580 args := make([]interface{}, 1+len(keys))
1581 args[0] = "pfcount"
1582 for i, key := range keys {
1583 args[1+i] = key
1584 }
1585 cmd := NewIntCmd(args...)
1586 c.process(cmd)
1587 return cmd
1588 }
1589
1590 func (c *cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
1591 args := make([]interface{}, 2+len(keys))
1592 args[0] = "pfmerge"
1593 args[1] = dest
1594 for i, key := range keys {
1595 args[2+i] = key
1596 }
1597 cmd := NewStatusCmd(args...)
1598 c.process(cmd)
1599 return cmd
1600 }
1601
1602 //------------------------------------------------------------------------------
1603
1604 func (c *cmdable) BgRewriteAOF() *StatusCmd {
1605 cmd := NewStatusCmd("bgrewriteaof")
1606 c.process(cmd)
1607 return cmd
1608 }
1609
1610 func (c *cmdable) BgSave() *StatusCmd {
1611 cmd := NewStatusCmd("bgsave")
1612 c.process(cmd)
1613 return cmd
1614 }
1615
1616 func (c *cmdable) ClientKill(ipPort string) *StatusCmd {
1617 cmd := NewStatusCmd("client", "kill", ipPort)
1618 c.process(cmd)
1619 return cmd
1620 }
1621
1622 func (c *cmdable) ClientList() *StringCmd {
1623 cmd := NewStringCmd("client", "list")
1624 c.process(cmd)
1625 return cmd
1626 }
1627
1628 func (c *cmdable) ClientPause(dur time.Duration) *BoolCmd {
1629 cmd := NewBoolCmd("client", "pause", formatMs(dur))
1630 c.process(cmd)
1631 return cmd
1632 }
1633
1634 // ClientSetName assigns a name to the connection.
1635 func (c *statefulCmdable) ClientSetName(name string) *BoolCmd {
1636 cmd := NewBoolCmd("client", "setname", name)
1637 c.process(cmd)
1638 return cmd
1639 }
1640
1641 // ClientGetName returns the name of the connection.
1642 func (c *statefulCmdable) ClientGetName() *StringCmd {
1643 cmd := NewStringCmd("client", "getname")
1644 c.process(cmd)
1645 return cmd
1646 }
1647
1648 func (c *cmdable) ConfigGet(parameter string) *SliceCmd {
1649 cmd := NewSliceCmd("config", "get", parameter)
1650 c.process(cmd)
1651 return cmd
1652 }
1653
1654 func (c *cmdable) ConfigResetStat() *StatusCmd {
1655 cmd := NewStatusCmd("config", "resetstat")
1656 c.process(cmd)
1657 return cmd
1658 }
1659
1660 func (c *cmdable) ConfigSet(parameter, value string) *StatusCmd {
1661 cmd := NewStatusCmd("config", "set", parameter, value)
1662 c.process(cmd)
1663 return cmd
1664 }
1665
1666 func (c *cmdable) DbSize() *IntCmd {
1667 cmd := NewIntCmd("dbsize")
1668 c.process(cmd)
1669 return cmd
1670 }
1671
1672 func (c *cmdable) FlushAll() *StatusCmd {
1673 cmd := NewStatusCmd("flushall")
1674 c.process(cmd)
1675 return cmd
1676 }
1677
1678 func (c *cmdable) FlushDb() *StatusCmd {
1679 cmd := NewStatusCmd("flushdb")
1680 c.process(cmd)
1681 return cmd
1682 }
1683
1684 func (c *cmdable) Info(section ...string) *StringCmd {
1685 args := []interface{}{"info"}
1686 if len(section) > 0 {
1687 args = append(args, section[0])
1688 }
1689 cmd := NewStringCmd(args...)
1690 c.process(cmd)
1691 return cmd
1692 }
1693
1694 func (c *cmdable) LastSave() *IntCmd {
1695 cmd := NewIntCmd("lastsave")
1696 c.process(cmd)
1697 return cmd
1698 }
1699
1700 func (c *cmdable) Save() *StatusCmd {
1701 cmd := NewStatusCmd("save")
1702 c.process(cmd)
1703 return cmd
1704 }
1705
1706 func (c *cmdable) shutdown(modifier string) *StatusCmd {
1707 var args []interface{}
1708 if modifier == "" {
1709 args = []interface{}{"shutdown"}
1710 } else {
1711 args = []interface{}{"shutdown", modifier}
1712 }
1713 cmd := NewStatusCmd(args...)
1714 c.process(cmd)
1715 if err := cmd.Err(); err != nil {
1716 if err == io.EOF {
1717 // Server quit as expected.
1718 cmd.err = nil
1719 }
1720 } else {
1721 // Server did not quit. String reply contains the reason.
1722 cmd.err = internal.RedisError(cmd.val)
1723 cmd.val = ""
1724 }
1725 return cmd
1726 }
1727
1728 func (c *cmdable) Shutdown() *StatusCmd {
1729 return c.shutdown("")
1730 }
1731
1732 func (c *cmdable) ShutdownSave() *StatusCmd {
1733 return c.shutdown("save")
1734 }
1735
1736 func (c *cmdable) ShutdownNoSave() *StatusCmd {
1737 return c.shutdown("nosave")
1738 }
1739
1740 func (c *cmdable) SlaveOf(host, port string) *StatusCmd {
1741 cmd := NewStatusCmd("slaveof", host, port)
1742 c.process(cmd)
1743 return cmd
1744 }
1745
1746 func (c *cmdable) SlowLog() {
1747 panic("not implemented")
1748 }
1749
1750 func (c *cmdable) Sync() {
1751 panic("not implemented")
1752 }
1753
1754 func (c *cmdable) Time() *TimeCmd {
1755 cmd := NewTimeCmd("time")
1756 c.process(cmd)
1757 return cmd
1758 }
1759
1760 //------------------------------------------------------------------------------
1761
1762 func (c *cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
1763 cmdArgs := make([]interface{}, 3+len(keys)+len(args))
1764 cmdArgs[0] = "eval"
1765 cmdArgs[1] = script
1766 cmdArgs[2] = strconv.Itoa(len(keys))
1767 for i, key := range keys {
1768 cmdArgs[3+i] = key
1769 }
1770 pos := 3 + len(keys)
1771 for i, arg := range args {
1772 cmdArgs[pos+i] = arg
1773 }
1774 cmd := NewCmd(cmdArgs...)
1775 c.process(cmd)
1776 return cmd
1777 }
1778
1779 func (c *cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
1780 cmdArgs := make([]interface{}, 3+len(keys)+len(args))
1781 cmdArgs[0] = "evalsha"
1782 cmdArgs[1] = sha1
1783 cmdArgs[2] = strconv.Itoa(len(keys))
1784 for i, key := range keys {
1785 cmdArgs[3+i] = key
1786 }
1787 pos := 3 + len(keys)
1788 for i, arg := range args {
1789 cmdArgs[pos+i] = arg
1790 }
1791 cmd := NewCmd(cmdArgs...)
1792 c.process(cmd)
1793 return cmd
1794 }
1795
1796 func (c *cmdable) ScriptExists(scripts ...string) *BoolSliceCmd {
1797 args := make([]interface{}, 2+len(scripts))
1798 args[0] = "script"
1799 args[1] = "exists"
1800 for i, script := range scripts {
1801 args[2+i] = script
1802 }
1803 cmd := NewBoolSliceCmd(args...)
1804 c.process(cmd)
1805 return cmd
1806 }
1807
1808 func (c *cmdable) ScriptFlush() *StatusCmd {
1809 cmd := NewStatusCmd("script", "flush")
1810 c.process(cmd)
1811 return cmd
1812 }
1813
1814 func (c *cmdable) ScriptKill() *StatusCmd {
1815 cmd := NewStatusCmd("script", "kill")
1816 c.process(cmd)
1817 return cmd
1818 }
1819
1820 func (c *cmdable) ScriptLoad(script string) *StringCmd {
1821 cmd := NewStringCmd("script", "load", script)
1822 c.process(cmd)
1823 return cmd
1824 }
1825
1826 //------------------------------------------------------------------------------
1827
1828 func (c *cmdable) DebugObject(key string) *StringCmd {
1829 cmd := NewStringCmd("debug", "object", key)
1830 c.process(cmd)
1831 return cmd
1832 }
1833
1834 //------------------------------------------------------------------------------
1835
1836 // Publish posts the message to the channel.
1837 func (c *cmdable) Publish(channel, message string) *IntCmd {
1838 cmd := NewIntCmd("PUBLISH", channel, message)
1839 c.process(cmd)
1840 return cmd
1841 }
1842
1843 func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd {
1844 args := []interface{}{"pubsub", "channels"}
1845 if pattern != "*" {
1846 args = append(args, pattern)
1847 }
1848 cmd := NewStringSliceCmd(args...)
1849 c.process(cmd)
1850 return cmd
1851 }
1852
1853 func (c *cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
1854 args := make([]interface{}, 2+len(channels))
1855 args[0] = "pubsub"
1856 args[1] = "numsub"
1857 for i, channel := range channels {
1858 args[2+i] = channel
1859 }
1860 cmd := NewStringIntMapCmd(args...)
1861 c.process(cmd)
1862 return cmd
1863 }
1864
1865 func (c *cmdable) PubSubNumPat() *IntCmd {
1866 cmd := NewIntCmd("pubsub", "numpat")
1867 c.process(cmd)
1868 return cmd
1869 }
1870
1871 //------------------------------------------------------------------------------
1872
1873 func (c *cmdable) ClusterSlots() *ClusterSlotsCmd {
1874 cmd := NewClusterSlotsCmd("cluster", "slots")
1875 c.process(cmd)
1876 return cmd
1877 }
1878
1879 func (c *cmdable) ClusterNodes() *StringCmd {
1880 cmd := NewStringCmd("cluster", "nodes")
1881 c.process(cmd)
1882 return cmd
1883 }
1884
1885 func (c *cmdable) ClusterMeet(host, port string) *StatusCmd {
1886 cmd := NewStatusCmd("cluster", "meet", host, port)
1887 c.process(cmd)
1888 return cmd
1889 }
1890
1891 func (c *cmdable) ClusterForget(nodeID string) *StatusCmd {
1892 cmd := NewStatusCmd("cluster", "forget", nodeID)
1893 c.process(cmd)
1894 return cmd
1895 }
1896
1897 func (c *cmdable) ClusterReplicate(nodeID string) *StatusCmd {
1898 cmd := NewStatusCmd("cluster", "replicate", nodeID)
1899 c.process(cmd)
1900 return cmd
1901 }
1902
1903 func (c *cmdable) ClusterResetSoft() *StatusCmd {
1904 cmd := NewStatusCmd("cluster", "reset", "soft")
1905 c.process(cmd)
1906 return cmd
1907 }
1908
1909 func (c *cmdable) ClusterResetHard() *StatusCmd {
1910 cmd := NewStatusCmd("cluster", "reset", "hard")
1911 c.process(cmd)
1912 return cmd
1913 }
1914
1915 func (c *cmdable) ClusterInfo() *StringCmd {
1916 cmd := NewStringCmd("cluster", "info")
1917 c.process(cmd)
1918 return cmd
1919 }
1920
1921 func (c *cmdable) ClusterKeySlot(key string) *IntCmd {
1922 cmd := NewIntCmd("cluster", "keyslot", key)
1923 c.process(cmd)
1924 return cmd
1925 }
1926
1927 func (c *cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
1928 cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
1929 c.process(cmd)
1930 return cmd
1931 }
1932
1933 func (c *cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
1934 cmd := NewIntCmd("cluster", "countkeysinslot", slot)
1935 c.process(cmd)
1936 return cmd
1937 }
1938
1939 func (c *cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
1940 args := make([]interface{}, 2+len(slots))
1941 args[0] = "cluster"
1942 args[1] = "delslots"
1943 for i, slot := range slots {
1944 args[2+i] = slot
1945 }
1946 cmd := NewStatusCmd(args...)
1947 c.process(cmd)
1948 return cmd
1949 }
1950
1951 func (c *cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
1952 size := max - min + 1
1953 slots := make([]int, size)
1954 for i := 0; i < size; i++ {
1955 slots[i] = min + i
1956 }
1957 return c.ClusterDelSlots(slots...)
1958 }
1959
1960 func (c *cmdable) ClusterSaveConfig() *StatusCmd {
1961 cmd := NewStatusCmd("cluster", "saveconfig")
1962 c.process(cmd)
1963 return cmd
1964 }
1965
1966 func (c *cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
1967 cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
1968 c.process(cmd)
1969 return cmd
1970 }
1971
1972 func (c *statefulCmdable) ReadOnly() *StatusCmd {
1973 cmd := NewStatusCmd("readonly")
1974 c.process(cmd)
1975 return cmd
1976 }
1977
1978 func (c *statefulCmdable) ReadWrite() *StatusCmd {
1979 cmd := NewStatusCmd("readwrite")
1980 c.process(cmd)
1981 return cmd
1982 }
1983
1984 func (c *cmdable) ClusterFailover() *StatusCmd {
1985 cmd := NewStatusCmd("cluster", "failover")
1986 c.process(cmd)
1987 return cmd
1988 }
1989
1990 func (c *cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
1991 args := make([]interface{}, 2+len(slots))
1992 args[0] = "cluster"
1993 args[1] = "addslots"
1994 for i, num := range slots {
1995 args[2+i] = strconv.Itoa(num)
1996 }
1997 cmd := NewStatusCmd(args...)
1998 c.process(cmd)
1999 return cmd
2000 }
2001
2002 func (c *cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
2003 size := max - min + 1
2004 slots := make([]int, size)
2005 for i := 0; i < size; i++ {
2006 slots[i] = min + i
2007 }
2008 return c.ClusterAddSlots(slots...)
2009 }
2010
2011 //------------------------------------------------------------------------------
2012
2013 func (c *cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
2014 args := make([]interface{}, 2+3*len(geoLocation))
2015 args[0] = "geoadd"
2016 args[1] = key
2017 for i, eachLoc := range geoLocation {
2018 args[2+3*i] = eachLoc.Longitude
2019 args[2+3*i+1] = eachLoc.Latitude
2020 args[2+3*i+2] = eachLoc.Name
2021 }
2022 cmd := NewIntCmd(args...)
2023 c.process(cmd)
2024 return cmd
2025 }
2026
2027 func (c *cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
2028 cmd := NewGeoLocationCmd(query, "georadius", key, longitude, latitude)
2029 c.process(cmd)
2030 return cmd
2031 }
2032
2033 func (c *cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
2034 cmd := NewGeoLocationCmd(query, "georadiusbymember", key, member)
2035 c.process(cmd)
2036 return cmd
2037 }
2038
2039 func (c *cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
2040 if unit == "" {
2041 unit = "km"
2042 }
2043 cmd := NewFloatCmd("geodist", key, member1, member2, unit)
2044 c.process(cmd)
2045 return cmd
2046 }
2047
2048 func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
2049 args := make([]interface{}, 2+len(members))
2050 args[0] = "geohash"
2051 args[1] = key
2052 for i, member := range members {
2053 args[2+i] = member
2054 }
2055 cmd := NewStringSliceCmd(args...)
2056 c.process(cmd)
2057 return cmd
2058 }
2059
2060 func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
2061 args := make([]interface{}, 2+len(members))
2062 args[0] = "geopos"
2063 args[1] = key
2064 for i, member := range members {
2065 args[2+i] = member
2066 }
2067 cmd := NewGeoPosCmd(args...)
2068 c.process(cmd)
2069 return cmd
2070 }
2071
2072 //------------------------------------------------------------------------------
2073
2074 func (c *cmdable) Command() *CommandsInfoCmd {
2075 cmd := NewCommandsInfoCmd("command")
2076 c.process(cmd)
2077 return cmd
2078 }