8 "github.com/go-redis/redis/internal"
11 func usePrecise(dur time.Duration) bool {
12 return dur < time.Second || dur%time.Second != 0
15 func formatMs(dur time.Duration) int64 {
16 if dur > 0 && dur < time.Millisecond {
18 "specified duration is %s, but minimal supported value is %s",
19 dur, time.Millisecond,
22 return int64(dur / time.Millisecond)
25 func formatSec(dur time.Duration) int64 {
26 if dur > 0 && dur < time.Second {
28 "specified duration is %s, but minimal supported value is %s",
32 return int64(dur / time.Second)
35 func appendArgs(dst, src []interface{}) []interface{} {
37 if ss, ok := src[0].([]string); ok {
38 for _, s := range ss {
45 for _, v := range src {
51 type Cmdable interface {
53 Pipelined(fn func(Pipeliner) error) ([]Cmder, error)
55 TxPipelined(fn func(Pipeliner) error) ([]Cmder, error)
56 TxPipeline() Pipeliner
58 Command() *CommandsInfoCmd
59 ClientGetName() *StringCmd
60 Echo(message interface{}) *StringCmd
63 Del(keys ...string) *IntCmd
64 Unlink(keys ...string) *IntCmd
65 Dump(key string) *StringCmd
66 Exists(keys ...string) *IntCmd
67 Expire(key string, expiration time.Duration) *BoolCmd
68 ExpireAt(key string, tm time.Time) *BoolCmd
69 Keys(pattern string) *StringSliceCmd
70 Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd
71 Move(key string, db int64) *BoolCmd
72 ObjectRefCount(key string) *IntCmd
73 ObjectEncoding(key string) *StringCmd
74 ObjectIdleTime(key string) *DurationCmd
75 Persist(key string) *BoolCmd
76 PExpire(key string, expiration time.Duration) *BoolCmd
77 PExpireAt(key string, tm time.Time) *BoolCmd
78 PTTL(key string) *DurationCmd
79 RandomKey() *StringCmd
80 Rename(key, newkey string) *StatusCmd
81 RenameNX(key, newkey string) *BoolCmd
82 Restore(key string, ttl time.Duration, value string) *StatusCmd
83 RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
84 Sort(key string, sort *Sort) *StringSliceCmd
85 SortStore(key, store string, sort *Sort) *IntCmd
86 SortInterfaces(key string, sort *Sort) *SliceCmd
87 Touch(keys ...string) *IntCmd
88 TTL(key string) *DurationCmd
89 Type(key string) *StatusCmd
90 Scan(cursor uint64, match string, count int64) *ScanCmd
91 SScan(key string, cursor uint64, match string, count int64) *ScanCmd
92 HScan(key string, cursor uint64, match string, count int64) *ScanCmd
93 ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
94 Append(key, value string) *IntCmd
95 BitCount(key string, bitCount *BitCount) *IntCmd
96 BitOpAnd(destKey string, keys ...string) *IntCmd
97 BitOpOr(destKey string, keys ...string) *IntCmd
98 BitOpXor(destKey string, keys ...string) *IntCmd
99 BitOpNot(destKey string, key string) *IntCmd
100 BitPos(key string, bit int64, pos ...int64) *IntCmd
101 Decr(key string) *IntCmd
102 DecrBy(key string, decrement int64) *IntCmd
103 Get(key string) *StringCmd
104 GetBit(key string, offset int64) *IntCmd
105 GetRange(key string, start, end int64) *StringCmd
106 GetSet(key string, value interface{}) *StringCmd
107 Incr(key string) *IntCmd
108 IncrBy(key string, value int64) *IntCmd
109 IncrByFloat(key string, value float64) *FloatCmd
110 MGet(keys ...string) *SliceCmd
111 MSet(pairs ...interface{}) *StatusCmd
112 MSetNX(pairs ...interface{}) *BoolCmd
113 Set(key string, value interface{}, expiration time.Duration) *StatusCmd
114 SetBit(key string, offset int64, value int) *IntCmd
115 SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
116 SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd
117 SetRange(key string, offset int64, value string) *IntCmd
118 StrLen(key string) *IntCmd
119 HDel(key string, fields ...string) *IntCmd
120 HExists(key, field string) *BoolCmd
121 HGet(key, field string) *StringCmd
122 HGetAll(key string) *StringStringMapCmd
123 HIncrBy(key, field string, incr int64) *IntCmd
124 HIncrByFloat(key, field string, incr float64) *FloatCmd
125 HKeys(key string) *StringSliceCmd
126 HLen(key string) *IntCmd
127 HMGet(key string, fields ...string) *SliceCmd
128 HMSet(key string, fields map[string]interface{}) *StatusCmd
129 HSet(key, field string, value interface{}) *BoolCmd
130 HSetNX(key, field string, value interface{}) *BoolCmd
131 HVals(key string) *StringSliceCmd
132 BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
133 BRPop(timeout time.Duration, keys ...string) *StringSliceCmd
134 BRPopLPush(source, destination string, timeout time.Duration) *StringCmd
135 LIndex(key string, index int64) *StringCmd
136 LInsert(key, op string, pivot, value interface{}) *IntCmd
137 LInsertBefore(key string, pivot, value interface{}) *IntCmd
138 LInsertAfter(key string, pivot, value interface{}) *IntCmd
139 LLen(key string) *IntCmd
140 LPop(key string) *StringCmd
141 LPush(key string, values ...interface{}) *IntCmd
142 LPushX(key string, value interface{}) *IntCmd
143 LRange(key string, start, stop int64) *StringSliceCmd
144 LRem(key string, count int64, value interface{}) *IntCmd
145 LSet(key string, index int64, value interface{}) *StatusCmd
146 LTrim(key string, start, stop int64) *StatusCmd
147 RPop(key string) *StringCmd
148 RPopLPush(source, destination string) *StringCmd
149 RPush(key string, values ...interface{}) *IntCmd
150 RPushX(key string, value interface{}) *IntCmd
151 SAdd(key string, members ...interface{}) *IntCmd
152 SCard(key string) *IntCmd
153 SDiff(keys ...string) *StringSliceCmd
154 SDiffStore(destination string, keys ...string) *IntCmd
155 SInter(keys ...string) *StringSliceCmd
156 SInterStore(destination string, keys ...string) *IntCmd
157 SIsMember(key string, member interface{}) *BoolCmd
158 SMembers(key string) *StringSliceCmd
159 SMembersMap(key string) *StringStructMapCmd
160 SMove(source, destination string, member interface{}) *BoolCmd
161 SPop(key string) *StringCmd
162 SPopN(key string, count int64) *StringSliceCmd
163 SRandMember(key string) *StringCmd
164 SRandMemberN(key string, count int64) *StringSliceCmd
165 SRem(key string, members ...interface{}) *IntCmd
166 SUnion(keys ...string) *StringSliceCmd
167 SUnionStore(destination string, keys ...string) *IntCmd
168 XAdd(a *XAddArgs) *StringCmd
169 XDel(stream string, ids ...string) *IntCmd
170 XLen(stream string) *IntCmd
171 XRange(stream, start, stop string) *XMessageSliceCmd
172 XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd
173 XRevRange(stream string, start, stop string) *XMessageSliceCmd
174 XRevRangeN(stream string, start, stop string, count int64) *XMessageSliceCmd
175 XRead(a *XReadArgs) *XStreamSliceCmd
176 XReadStreams(streams ...string) *XStreamSliceCmd
177 XGroupCreate(stream, group, start string) *StatusCmd
178 XGroupCreateMkStream(stream, group, start string) *StatusCmd
179 XGroupSetID(stream, group, start string) *StatusCmd
180 XGroupDestroy(stream, group string) *IntCmd
181 XGroupDelConsumer(stream, group, consumer string) *IntCmd
182 XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd
183 XAck(stream, group string, ids ...string) *IntCmd
184 XPending(stream, group string) *XPendingCmd
185 XPendingExt(a *XPendingExtArgs) *XPendingExtCmd
186 XClaim(a *XClaimArgs) *XMessageSliceCmd
187 XClaimJustID(a *XClaimArgs) *StringSliceCmd
188 XTrim(key string, maxLen int64) *IntCmd
189 XTrimApprox(key string, maxLen int64) *IntCmd
190 BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd
191 BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd
192 ZAdd(key string, members ...Z) *IntCmd
193 ZAddNX(key string, members ...Z) *IntCmd
194 ZAddXX(key string, members ...Z) *IntCmd
195 ZAddCh(key string, members ...Z) *IntCmd
196 ZAddNXCh(key string, members ...Z) *IntCmd
197 ZAddXXCh(key string, members ...Z) *IntCmd
198 ZIncr(key string, member Z) *FloatCmd
199 ZIncrNX(key string, member Z) *FloatCmd
200 ZIncrXX(key string, member Z) *FloatCmd
201 ZCard(key string) *IntCmd
202 ZCount(key, min, max string) *IntCmd
203 ZLexCount(key, min, max string) *IntCmd
204 ZIncrBy(key string, increment float64, member string) *FloatCmd
205 ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
206 ZPopMax(key string, count ...int64) *ZSliceCmd
207 ZPopMin(key string, count ...int64) *ZSliceCmd
208 ZRange(key string, start, stop int64) *StringSliceCmd
209 ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
210 ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
211 ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
212 ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
213 ZRank(key, member string) *IntCmd
214 ZRem(key string, members ...interface{}) *IntCmd
215 ZRemRangeByRank(key string, start, stop int64) *IntCmd
216 ZRemRangeByScore(key, min, max string) *IntCmd
217 ZRemRangeByLex(key, min, max string) *IntCmd
218 ZRevRange(key string, start, stop int64) *StringSliceCmd
219 ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
220 ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
221 ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
222 ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
223 ZRevRank(key, member string) *IntCmd
224 ZScore(key, member string) *FloatCmd
225 ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
226 PFAdd(key string, els ...interface{}) *IntCmd
227 PFCount(keys ...string) *IntCmd
228 PFMerge(dest string, keys ...string) *StatusCmd
229 BgRewriteAOF() *StatusCmd
231 ClientKill(ipPort string) *StatusCmd
232 ClientKillByFilter(keys ...string) *IntCmd
233 ClientList() *StringCmd
234 ClientPause(dur time.Duration) *BoolCmd
236 ConfigGet(parameter string) *SliceCmd
237 ConfigResetStat() *StatusCmd
238 ConfigSet(parameter, value string) *StatusCmd
239 ConfigRewrite() *StatusCmd
241 FlushAll() *StatusCmd
242 FlushAllAsync() *StatusCmd
244 FlushDBAsync() *StatusCmd
245 Info(section ...string) *StringCmd
248 Shutdown() *StatusCmd
249 ShutdownSave() *StatusCmd
250 ShutdownNoSave() *StatusCmd
251 SlaveOf(host, port string) *StatusCmd
253 Eval(script string, keys []string, args ...interface{}) *Cmd
254 EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
255 ScriptExists(hashes ...string) *BoolSliceCmd
256 ScriptFlush() *StatusCmd
257 ScriptKill() *StatusCmd
258 ScriptLoad(script string) *StringCmd
259 DebugObject(key string) *StringCmd
260 Publish(channel string, message interface{}) *IntCmd
261 PubSubChannels(pattern string) *StringSliceCmd
262 PubSubNumSub(channels ...string) *StringIntMapCmd
263 PubSubNumPat() *IntCmd
264 ClusterSlots() *ClusterSlotsCmd
265 ClusterNodes() *StringCmd
266 ClusterMeet(host, port string) *StatusCmd
267 ClusterForget(nodeID string) *StatusCmd
268 ClusterReplicate(nodeID string) *StatusCmd
269 ClusterResetSoft() *StatusCmd
270 ClusterResetHard() *StatusCmd
271 ClusterInfo() *StringCmd
272 ClusterKeySlot(key string) *IntCmd
273 ClusterGetKeysInSlot(slot int, count int) *StringSliceCmd
274 ClusterCountFailureReports(nodeID string) *IntCmd
275 ClusterCountKeysInSlot(slot int) *IntCmd
276 ClusterDelSlots(slots ...int) *StatusCmd
277 ClusterDelSlotsRange(min, max int) *StatusCmd
278 ClusterSaveConfig() *StatusCmd
279 ClusterSlaves(nodeID string) *StringSliceCmd
280 ClusterFailover() *StatusCmd
281 ClusterAddSlots(slots ...int) *StatusCmd
282 ClusterAddSlotsRange(min, max int) *StatusCmd
283 GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
284 GeoPos(key string, members ...string) *GeoPosCmd
285 GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
286 GeoRadiusRO(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
287 GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
288 GeoRadiusByMemberRO(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
289 GeoDist(key string, member1, member2, unit string) *FloatCmd
290 GeoHash(key string, members ...string) *StringSliceCmd
291 ReadOnly() *StatusCmd
292 ReadWrite() *StatusCmd
293 MemoryUsage(key string, samples ...int) *IntCmd
296 type StatefulCmdable interface {
298 Auth(password string) *StatusCmd
299 Select(index int) *StatusCmd
300 SwapDB(index1, index2 int) *StatusCmd
301 ClientSetName(name string) *BoolCmd
304 var _ Cmdable = (*Client)(nil)
305 var _ Cmdable = (*Tx)(nil)
306 var _ Cmdable = (*Ring)(nil)
307 var _ Cmdable = (*ClusterClient)(nil)
309 type cmdable struct {
310 process func(cmd Cmder) error
313 func (c *cmdable) setProcessor(fn func(Cmder) error) {
317 type statefulCmdable struct {
319 process func(cmd Cmder) error
322 func (c *statefulCmdable) setProcessor(fn func(Cmder) error) {
324 c.cmdable.setProcessor(fn)
327 //------------------------------------------------------------------------------
329 func (c *statefulCmdable) Auth(password string) *StatusCmd {
330 cmd := NewStatusCmd("auth", password)
335 func (c *cmdable) Echo(message interface{}) *StringCmd {
336 cmd := NewStringCmd("echo", message)
341 func (c *cmdable) Ping() *StatusCmd {
342 cmd := NewStatusCmd("ping")
347 func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
348 cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
353 func (c *cmdable) Quit() *StatusCmd {
354 panic("not implemented")
357 func (c *statefulCmdable) Select(index int) *StatusCmd {
358 cmd := NewStatusCmd("select", index)
363 func (c *statefulCmdable) SwapDB(index1, index2 int) *StatusCmd {
364 cmd := NewStatusCmd("swapdb", index1, index2)
369 //------------------------------------------------------------------------------
371 func (c *cmdable) Command() *CommandsInfoCmd {
372 cmd := NewCommandsInfoCmd("command")
377 func (c *cmdable) Del(keys ...string) *IntCmd {
378 args := make([]interface{}, 1+len(keys))
380 for i, key := range keys {
383 cmd := NewIntCmd(args...)
388 func (c *cmdable) Unlink(keys ...string) *IntCmd {
389 args := make([]interface{}, 1+len(keys))
391 for i, key := range keys {
394 cmd := NewIntCmd(args...)
399 func (c *cmdable) Dump(key string) *StringCmd {
400 cmd := NewStringCmd("dump", key)
405 func (c *cmdable) Exists(keys ...string) *IntCmd {
406 args := make([]interface{}, 1+len(keys))
408 for i, key := range keys {
411 cmd := NewIntCmd(args...)
416 func (c *cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
417 cmd := NewBoolCmd("expire", key, formatSec(expiration))
422 func (c *cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
423 cmd := NewBoolCmd("expireat", key, tm.Unix())
428 func (c *cmdable) Keys(pattern string) *StringSliceCmd {
429 cmd := NewStringSliceCmd("keys", pattern)
434 func (c *cmdable) Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd {
443 cmd.setReadTimeout(timeout)
448 func (c *cmdable) Move(key string, db int64) *BoolCmd {
449 cmd := NewBoolCmd("move", key, db)
454 func (c *cmdable) ObjectRefCount(key string) *IntCmd {
455 cmd := NewIntCmd("object", "refcount", key)
460 func (c *cmdable) ObjectEncoding(key string) *StringCmd {
461 cmd := NewStringCmd("object", "encoding", key)
466 func (c *cmdable) ObjectIdleTime(key string) *DurationCmd {
467 cmd := NewDurationCmd(time.Second, "object", "idletime", key)
472 func (c *cmdable) Persist(key string) *BoolCmd {
473 cmd := NewBoolCmd("persist", key)
478 func (c *cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
479 cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
484 func (c *cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
488 tm.UnixNano()/int64(time.Millisecond),
494 func (c *cmdable) PTTL(key string) *DurationCmd {
495 cmd := NewDurationCmd(time.Millisecond, "pttl", key)
500 func (c *cmdable) RandomKey() *StringCmd {
501 cmd := NewStringCmd("randomkey")
506 func (c *cmdable) Rename(key, newkey string) *StatusCmd {
507 cmd := NewStatusCmd("rename", key, newkey)
512 func (c *cmdable) RenameNX(key, newkey string) *BoolCmd {
513 cmd := NewBoolCmd("renamenx", key, newkey)
518 func (c *cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
529 func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
549 func (sort *Sort) args(key string) []interface{} {
550 args := []interface{}{"sort", key}
552 args = append(args, "by", sort.By)
554 if sort.Offset != 0 || sort.Count != 0 {
555 args = append(args, "limit", sort.Offset, sort.Count)
557 for _, get := range sort.Get {
558 args = append(args, "get", get)
560 if sort.Order != "" {
561 args = append(args, sort.Order)
564 args = append(args, "alpha")
569 func (c *cmdable) Sort(key string, sort *Sort) *StringSliceCmd {
570 cmd := NewStringSliceCmd(sort.args(key)...)
575 func (c *cmdable) SortStore(key, store string, sort *Sort) *IntCmd {
576 args := sort.args(key)
578 args = append(args, "store", store)
580 cmd := NewIntCmd(args...)
585 func (c *cmdable) SortInterfaces(key string, sort *Sort) *SliceCmd {
586 cmd := NewSliceCmd(sort.args(key)...)
591 func (c *cmdable) Touch(keys ...string) *IntCmd {
592 args := make([]interface{}, len(keys)+1)
594 for i, key := range keys {
597 cmd := NewIntCmd(args...)
602 func (c *cmdable) TTL(key string) *DurationCmd {
603 cmd := NewDurationCmd(time.Second, "ttl", key)
608 func (c *cmdable) Type(key string) *StatusCmd {
609 cmd := NewStatusCmd("type", key)
614 func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
615 args := []interface{}{"scan", cursor}
617 args = append(args, "match", match)
620 args = append(args, "count", count)
622 cmd := NewScanCmd(c.process, args...)
627 func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
628 args := []interface{}{"sscan", key, cursor}
630 args = append(args, "match", match)
633 args = append(args, "count", count)
635 cmd := NewScanCmd(c.process, args...)
640 func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
641 args := []interface{}{"hscan", key, cursor}
643 args = append(args, "match", match)
646 args = append(args, "count", count)
648 cmd := NewScanCmd(c.process, args...)
653 func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
654 args := []interface{}{"zscan", key, cursor}
656 args = append(args, "match", match)
659 args = append(args, "count", count)
661 cmd := NewScanCmd(c.process, args...)
666 //------------------------------------------------------------------------------
668 func (c *cmdable) Append(key, value string) *IntCmd {
669 cmd := NewIntCmd("append", key, value)
674 type BitCount struct {
678 func (c *cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
679 args := []interface{}{"bitcount", key}
687 cmd := NewIntCmd(args...)
692 func (c *cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
693 args := make([]interface{}, 3+len(keys))
697 for i, key := range keys {
700 cmd := NewIntCmd(args...)
705 func (c *cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
706 return c.bitOp("and", destKey, keys...)
709 func (c *cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
710 return c.bitOp("or", destKey, keys...)
713 func (c *cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
714 return c.bitOp("xor", destKey, keys...)
717 func (c *cmdable) BitOpNot(destKey string, key string) *IntCmd {
718 return c.bitOp("not", destKey, key)
721 func (c *cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
722 args := make([]interface{}, 3+len(pos))
734 panic("too many arguments")
736 cmd := NewIntCmd(args...)
741 func (c *cmdable) Decr(key string) *IntCmd {
742 cmd := NewIntCmd("decr", key)
747 func (c *cmdable) DecrBy(key string, decrement int64) *IntCmd {
748 cmd := NewIntCmd("decrby", key, decrement)
753 // Redis `GET key` command. It returns redis.Nil error when key does not exist.
754 func (c *cmdable) Get(key string) *StringCmd {
755 cmd := NewStringCmd("get", key)
760 func (c *cmdable) GetBit(key string, offset int64) *IntCmd {
761 cmd := NewIntCmd("getbit", key, offset)
766 func (c *cmdable) GetRange(key string, start, end int64) *StringCmd {
767 cmd := NewStringCmd("getrange", key, start, end)
772 func (c *cmdable) GetSet(key string, value interface{}) *StringCmd {
773 cmd := NewStringCmd("getset", key, value)
778 func (c *cmdable) Incr(key string) *IntCmd {
779 cmd := NewIntCmd("incr", key)
784 func (c *cmdable) IncrBy(key string, value int64) *IntCmd {
785 cmd := NewIntCmd("incrby", key, value)
790 func (c *cmdable) IncrByFloat(key string, value float64) *FloatCmd {
791 cmd := NewFloatCmd("incrbyfloat", key, value)
796 func (c *cmdable) MGet(keys ...string) *SliceCmd {
797 args := make([]interface{}, 1+len(keys))
799 for i, key := range keys {
802 cmd := NewSliceCmd(args...)
807 func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
808 args := make([]interface{}, 1, 1+len(pairs))
810 args = appendArgs(args, pairs)
811 cmd := NewStatusCmd(args...)
816 func (c *cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
817 args := make([]interface{}, 1, 1+len(pairs))
819 args = appendArgs(args, pairs)
820 cmd := NewBoolCmd(args...)
825 // Redis `SET key value [expiration]` command.
827 // Use expiration for `SETEX`-like behavior.
828 // Zero expiration means the key has no expiration time.
829 func (c *cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
830 args := make([]interface{}, 3, 4)
835 if usePrecise(expiration) {
836 args = append(args, "px", formatMs(expiration))
838 args = append(args, "ex", formatSec(expiration))
841 cmd := NewStatusCmd(args...)
846 func (c *cmdable) SetBit(key string, offset int64, value int) *IntCmd {
857 // Redis `SET key value [expiration] NX` command.
859 // Zero expiration means the key has no expiration time.
860 func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
863 // Use old `SETNX` to support old Redis versions.
864 cmd = NewBoolCmd("setnx", key, value)
866 if usePrecise(expiration) {
867 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
869 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
876 // Redis `SET key value [expiration] XX` command.
878 // Zero expiration means the key has no expiration time.
879 func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
882 cmd = NewBoolCmd("set", key, value, "xx")
884 if usePrecise(expiration) {
885 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
887 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
894 func (c *cmdable) SetRange(key string, offset int64, value string) *IntCmd {
895 cmd := NewIntCmd("setrange", key, offset, value)
900 func (c *cmdable) StrLen(key string) *IntCmd {
901 cmd := NewIntCmd("strlen", key)
906 //------------------------------------------------------------------------------
908 func (c *cmdable) HDel(key string, fields ...string) *IntCmd {
909 args := make([]interface{}, 2+len(fields))
912 for i, field := range fields {
915 cmd := NewIntCmd(args...)
920 func (c *cmdable) HExists(key, field string) *BoolCmd {
921 cmd := NewBoolCmd("hexists", key, field)
926 func (c *cmdable) HGet(key, field string) *StringCmd {
927 cmd := NewStringCmd("hget", key, field)
932 func (c *cmdable) HGetAll(key string) *StringStringMapCmd {
933 cmd := NewStringStringMapCmd("hgetall", key)
938 func (c *cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
939 cmd := NewIntCmd("hincrby", key, field, incr)
944 func (c *cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
945 cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
950 func (c *cmdable) HKeys(key string) *StringSliceCmd {
951 cmd := NewStringSliceCmd("hkeys", key)
956 func (c *cmdable) HLen(key string) *IntCmd {
957 cmd := NewIntCmd("hlen", key)
962 func (c *cmdable) HMGet(key string, fields ...string) *SliceCmd {
963 args := make([]interface{}, 2+len(fields))
966 for i, field := range fields {
969 cmd := NewSliceCmd(args...)
974 func (c *cmdable) HMSet(key string, fields map[string]interface{}) *StatusCmd {
975 args := make([]interface{}, 2+len(fields)*2)
979 for k, v := range fields {
984 cmd := NewStatusCmd(args...)
989 func (c *cmdable) HSet(key, field string, value interface{}) *BoolCmd {
990 cmd := NewBoolCmd("hset", key, field, value)
995 func (c *cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
996 cmd := NewBoolCmd("hsetnx", key, field, value)
1001 func (c *cmdable) HVals(key string) *StringSliceCmd {
1002 cmd := NewStringSliceCmd("hvals", key)
1007 //------------------------------------------------------------------------------
1009 func (c *cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
1010 args := make([]interface{}, 1+len(keys)+1)
1012 for i, key := range keys {
1015 args[len(args)-1] = formatSec(timeout)
1016 cmd := NewStringSliceCmd(args...)
1017 cmd.setReadTimeout(timeout)
1022 func (c *cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
1023 args := make([]interface{}, 1+len(keys)+1)
1025 for i, key := range keys {
1028 args[len(keys)+1] = formatSec(timeout)
1029 cmd := NewStringSliceCmd(args...)
1030 cmd.setReadTimeout(timeout)
1035 func (c *cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
1036 cmd := NewStringCmd(
1042 cmd.setReadTimeout(timeout)
1047 func (c *cmdable) LIndex(key string, index int64) *StringCmd {
1048 cmd := NewStringCmd("lindex", key, index)
1053 func (c *cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
1054 cmd := NewIntCmd("linsert", key, op, pivot, value)
1059 func (c *cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
1060 cmd := NewIntCmd("linsert", key, "before", pivot, value)
1065 func (c *cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
1066 cmd := NewIntCmd("linsert", key, "after", pivot, value)
1071 func (c *cmdable) LLen(key string) *IntCmd {
1072 cmd := NewIntCmd("llen", key)
1077 func (c *cmdable) LPop(key string) *StringCmd {
1078 cmd := NewStringCmd("lpop", key)
1083 func (c *cmdable) LPush(key string, values ...interface{}) *IntCmd {
1084 args := make([]interface{}, 2, 2+len(values))
1087 args = appendArgs(args, values)
1088 cmd := NewIntCmd(args...)
1093 func (c *cmdable) LPushX(key string, value interface{}) *IntCmd {
1094 cmd := NewIntCmd("lpushx", key, value)
1099 func (c *cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
1100 cmd := NewStringSliceCmd(
1110 func (c *cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
1111 cmd := NewIntCmd("lrem", key, count, value)
1116 func (c *cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
1117 cmd := NewStatusCmd("lset", key, index, value)
1122 func (c *cmdable) LTrim(key string, start, stop int64) *StatusCmd {
1123 cmd := NewStatusCmd(
1133 func (c *cmdable) RPop(key string) *StringCmd {
1134 cmd := NewStringCmd("rpop", key)
1139 func (c *cmdable) RPopLPush(source, destination string) *StringCmd {
1140 cmd := NewStringCmd("rpoplpush", source, destination)
1145 func (c *cmdable) RPush(key string, values ...interface{}) *IntCmd {
1146 args := make([]interface{}, 2, 2+len(values))
1149 args = appendArgs(args, values)
1150 cmd := NewIntCmd(args...)
1155 func (c *cmdable) RPushX(key string, value interface{}) *IntCmd {
1156 cmd := NewIntCmd("rpushx", key, value)
1161 //------------------------------------------------------------------------------
1163 func (c *cmdable) SAdd(key string, members ...interface{}) *IntCmd {
1164 args := make([]interface{}, 2, 2+len(members))
1167 args = appendArgs(args, members)
1168 cmd := NewIntCmd(args...)
1173 func (c *cmdable) SCard(key string) *IntCmd {
1174 cmd := NewIntCmd("scard", key)
1179 func (c *cmdable) SDiff(keys ...string) *StringSliceCmd {
1180 args := make([]interface{}, 1+len(keys))
1182 for i, key := range keys {
1185 cmd := NewStringSliceCmd(args...)
1190 func (c *cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
1191 args := make([]interface{}, 2+len(keys))
1192 args[0] = "sdiffstore"
1193 args[1] = destination
1194 for i, key := range keys {
1197 cmd := NewIntCmd(args...)
1202 func (c *cmdable) SInter(keys ...string) *StringSliceCmd {
1203 args := make([]interface{}, 1+len(keys))
1205 for i, key := range keys {
1208 cmd := NewStringSliceCmd(args...)
1213 func (c *cmdable) SInterStore(destination string, keys ...string) *IntCmd {
1214 args := make([]interface{}, 2+len(keys))
1215 args[0] = "sinterstore"
1216 args[1] = destination
1217 for i, key := range keys {
1220 cmd := NewIntCmd(args...)
1225 func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
1226 cmd := NewBoolCmd("sismember", key, member)
1231 // Redis `SMEMBERS key` command output as a slice
1232 func (c *cmdable) SMembers(key string) *StringSliceCmd {
1233 cmd := NewStringSliceCmd("smembers", key)
1238 // Redis `SMEMBERS key` command output as a map
1239 func (c *cmdable) SMembersMap(key string) *StringStructMapCmd {
1240 cmd := NewStringStructMapCmd("smembers", key)
1245 func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
1246 cmd := NewBoolCmd("smove", source, destination, member)
1251 // Redis `SPOP key` command.
1252 func (c *cmdable) SPop(key string) *StringCmd {
1253 cmd := NewStringCmd("spop", key)
1258 // Redis `SPOP key count` command.
1259 func (c *cmdable) SPopN(key string, count int64) *StringSliceCmd {
1260 cmd := NewStringSliceCmd("spop", key, count)
1265 // Redis `SRANDMEMBER key` command.
1266 func (c *cmdable) SRandMember(key string) *StringCmd {
1267 cmd := NewStringCmd("srandmember", key)
1272 // Redis `SRANDMEMBER key count` command.
1273 func (c *cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
1274 cmd := NewStringSliceCmd("srandmember", key, count)
1279 func (c *cmdable) SRem(key string, members ...interface{}) *IntCmd {
1280 args := make([]interface{}, 2, 2+len(members))
1283 args = appendArgs(args, members)
1284 cmd := NewIntCmd(args...)
1289 func (c *cmdable) SUnion(keys ...string) *StringSliceCmd {
1290 args := make([]interface{}, 1+len(keys))
1292 for i, key := range keys {
1295 cmd := NewStringSliceCmd(args...)
1300 func (c *cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
1301 args := make([]interface{}, 2+len(keys))
1302 args[0] = "sunionstore"
1303 args[1] = destination
1304 for i, key := range keys {
1307 cmd := NewIntCmd(args...)
1312 //------------------------------------------------------------------------------
1314 type XAddArgs struct {
1316 MaxLen int64 // MAXLEN N
1317 MaxLenApprox int64 // MAXLEN ~ N
1319 Values map[string]interface{}
1322 func (c *cmdable) XAdd(a *XAddArgs) *StringCmd {
1323 args := make([]interface{}, 0, 6+len(a.Values)*2)
1324 args = append(args, "xadd")
1325 args = append(args, a.Stream)
1327 args = append(args, "maxlen", a.MaxLen)
1328 } else if a.MaxLenApprox > 0 {
1329 args = append(args, "maxlen", "~", a.MaxLenApprox)
1332 args = append(args, a.ID)
1334 args = append(args, "*")
1336 for k, v := range a.Values {
1337 args = append(args, k)
1338 args = append(args, v)
1341 cmd := NewStringCmd(args...)
1346 func (c *cmdable) XDel(stream string, ids ...string) *IntCmd {
1347 args := []interface{}{"xdel", stream}
1348 for _, id := range ids {
1349 args = append(args, id)
1351 cmd := NewIntCmd(args...)
1356 func (c *cmdable) XLen(stream string) *IntCmd {
1357 cmd := NewIntCmd("xlen", stream)
1362 func (c *cmdable) XRange(stream, start, stop string) *XMessageSliceCmd {
1363 cmd := NewXMessageSliceCmd("xrange", stream, start, stop)
1368 func (c *cmdable) XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
1369 cmd := NewXMessageSliceCmd("xrange", stream, start, stop, "count", count)
1374 func (c *cmdable) XRevRange(stream, start, stop string) *XMessageSliceCmd {
1375 cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop)
1380 func (c *cmdable) XRevRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
1381 cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop, "count", count)
1386 type XReadArgs struct {
1392 func (c *cmdable) XRead(a *XReadArgs) *XStreamSliceCmd {
1393 args := make([]interface{}, 0, 5+len(a.Streams))
1394 args = append(args, "xread")
1396 args = append(args, "count")
1397 args = append(args, a.Count)
1400 args = append(args, "block")
1401 args = append(args, int64(a.Block/time.Millisecond))
1403 args = append(args, "streams")
1404 for _, s := range a.Streams {
1405 args = append(args, s)
1408 cmd := NewXStreamSliceCmd(args...)
1410 cmd.setReadTimeout(a.Block)
1416 func (c *cmdable) XReadStreams(streams ...string) *XStreamSliceCmd {
1417 return c.XRead(&XReadArgs{
1423 func (c *cmdable) XGroupCreate(stream, group, start string) *StatusCmd {
1424 cmd := NewStatusCmd("xgroup", "create", stream, group, start)
1429 func (c *cmdable) XGroupCreateMkStream(stream, group, start string) *StatusCmd {
1430 cmd := NewStatusCmd("xgroup", "create", stream, group, start, "mkstream")
1435 func (c *cmdable) XGroupSetID(stream, group, start string) *StatusCmd {
1436 cmd := NewStatusCmd("xgroup", "setid", stream, group, start)
1441 func (c *cmdable) XGroupDestroy(stream, group string) *IntCmd {
1442 cmd := NewIntCmd("xgroup", "destroy", stream, group)
1447 func (c *cmdable) XGroupDelConsumer(stream, group, consumer string) *IntCmd {
1448 cmd := NewIntCmd("xgroup", "delconsumer", stream, group, consumer)
1453 type XReadGroupArgs struct {
1456 // List of streams and ids.
1463 func (c *cmdable) XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd {
1464 args := make([]interface{}, 0, 8+len(a.Streams))
1465 args = append(args, "xreadgroup", "group", a.Group, a.Consumer)
1467 args = append(args, "count", a.Count)
1470 args = append(args, "block", int64(a.Block/time.Millisecond))
1473 args = append(args, "noack")
1475 args = append(args, "streams")
1476 for _, s := range a.Streams {
1477 args = append(args, s)
1480 cmd := NewXStreamSliceCmd(args...)
1482 cmd.setReadTimeout(a.Block)
1488 func (c *cmdable) XAck(stream, group string, ids ...string) *IntCmd {
1489 args := []interface{}{"xack", stream, group}
1490 for _, id := range ids {
1491 args = append(args, id)
1493 cmd := NewIntCmd(args...)
1498 func (c *cmdable) XPending(stream, group string) *XPendingCmd {
1499 cmd := NewXPendingCmd("xpending", stream, group)
1504 type XPendingExtArgs struct {
1513 func (c *cmdable) XPendingExt(a *XPendingExtArgs) *XPendingExtCmd {
1514 args := make([]interface{}, 0, 7)
1515 args = append(args, "xpending", a.Stream, a.Group, a.Start, a.End, a.Count)
1516 if a.Consumer != "" {
1517 args = append(args, a.Consumer)
1519 cmd := NewXPendingExtCmd(args...)
1524 type XClaimArgs struct {
1528 MinIdle time.Duration
1532 func (c *cmdable) XClaim(a *XClaimArgs) *XMessageSliceCmd {
1533 args := xClaimArgs(a)
1534 cmd := NewXMessageSliceCmd(args...)
1539 func (c *cmdable) XClaimJustID(a *XClaimArgs) *StringSliceCmd {
1540 args := xClaimArgs(a)
1541 args = append(args, "justid")
1542 cmd := NewStringSliceCmd(args...)
1547 func xClaimArgs(a *XClaimArgs) []interface{} {
1548 args := make([]interface{}, 0, 4+len(a.Messages))
1552 a.Group, a.Consumer,
1553 int64(a.MinIdle/time.Millisecond))
1554 for _, id := range a.Messages {
1555 args = append(args, id)
1560 func (c *cmdable) XTrim(key string, maxLen int64) *IntCmd {
1561 cmd := NewIntCmd("xtrim", key, "maxlen", maxLen)
1566 func (c *cmdable) XTrimApprox(key string, maxLen int64) *IntCmd {
1567 cmd := NewIntCmd("xtrim", key, "maxlen", "~", maxLen)
1572 //------------------------------------------------------------------------------
1574 // Z represents sorted set member.
1580 // ZWithKey represents sorted set member including the name of the key where it was popped.
1581 type ZWithKey struct {
1586 // ZStore is used as an arg to ZInterStore and ZUnionStore.
1587 type ZStore struct {
1589 // Can be SUM, MIN or MAX.
1593 // Redis `BZPOPMAX key [key ...] timeout` command.
1594 func (c *cmdable) BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd {
1595 args := make([]interface{}, 1+len(keys)+1)
1596 args[0] = "bzpopmax"
1597 for i, key := range keys {
1600 args[len(args)-1] = formatSec(timeout)
1601 cmd := NewZWithKeyCmd(args...)
1602 cmd.setReadTimeout(timeout)
1607 // Redis `BZPOPMIN key [key ...] timeout` command.
1608 func (c *cmdable) BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd {
1609 args := make([]interface{}, 1+len(keys)+1)
1610 args[0] = "bzpopmin"
1611 for i, key := range keys {
1614 args[len(args)-1] = formatSec(timeout)
1615 cmd := NewZWithKeyCmd(args...)
1616 cmd.setReadTimeout(timeout)
1621 func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
1622 for i, m := range members {
1624 a[n+2*i+1] = m.Member
1626 cmd := NewIntCmd(a...)
1631 // Redis `ZADD key score member [score member ...]` command.
1632 func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
1634 a := make([]interface{}, n+2*len(members))
1635 a[0], a[1] = "zadd", key
1636 return c.zAdd(a, n, members...)
1639 // Redis `ZADD key NX score member [score member ...]` command.
1640 func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
1642 a := make([]interface{}, n+2*len(members))
1643 a[0], a[1], a[2] = "zadd", key, "nx"
1644 return c.zAdd(a, n, members...)
1647 // Redis `ZADD key XX score member [score member ...]` command.
1648 func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
1650 a := make([]interface{}, n+2*len(members))
1651 a[0], a[1], a[2] = "zadd", key, "xx"
1652 return c.zAdd(a, n, members...)
1655 // Redis `ZADD key CH score member [score member ...]` command.
1656 func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
1658 a := make([]interface{}, n+2*len(members))
1659 a[0], a[1], a[2] = "zadd", key, "ch"
1660 return c.zAdd(a, n, members...)
1663 // Redis `ZADD key NX CH score member [score member ...]` command.
1664 func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
1666 a := make([]interface{}, n+2*len(members))
1667 a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
1668 return c.zAdd(a, n, members...)
1671 // Redis `ZADD key XX CH score member [score member ...]` command.
1672 func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
1674 a := make([]interface{}, n+2*len(members))
1675 a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
1676 return c.zAdd(a, n, members...)
1679 func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
1680 for i, m := range members {
1682 a[n+2*i+1] = m.Member
1684 cmd := NewFloatCmd(a...)
1689 // Redis `ZADD key INCR score member` command.
1690 func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
1692 a := make([]interface{}, n+2)
1693 a[0], a[1], a[2] = "zadd", key, "incr"
1694 return c.zIncr(a, n, member)
1697 // Redis `ZADD key NX INCR score member` command.
1698 func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
1700 a := make([]interface{}, n+2)
1701 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
1702 return c.zIncr(a, n, member)
1705 // Redis `ZADD key XX INCR score member` command.
1706 func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
1708 a := make([]interface{}, n+2)
1709 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
1710 return c.zIncr(a, n, member)
1713 func (c *cmdable) ZCard(key string) *IntCmd {
1714 cmd := NewIntCmd("zcard", key)
1719 func (c *cmdable) ZCount(key, min, max string) *IntCmd {
1720 cmd := NewIntCmd("zcount", key, min, max)
1725 func (c *cmdable) ZLexCount(key, min, max string) *IntCmd {
1726 cmd := NewIntCmd("zlexcount", key, min, max)
1731 func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
1732 cmd := NewFloatCmd("zincrby", key, increment, member)
1737 func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
1738 args := make([]interface{}, 3+len(keys))
1739 args[0] = "zinterstore"
1740 args[1] = destination
1742 for i, key := range keys {
1745 if len(store.Weights) > 0 {
1746 args = append(args, "weights")
1747 for _, weight := range store.Weights {
1748 args = append(args, weight)
1751 if store.Aggregate != "" {
1752 args = append(args, "aggregate", store.Aggregate)
1754 cmd := NewIntCmd(args...)
1759 func (c *cmdable) ZPopMax(key string, count ...int64) *ZSliceCmd {
1760 args := []interface{}{
1769 args = append(args, count[0])
1771 panic("too many arguments")
1774 cmd := NewZSliceCmd(args...)
1779 func (c *cmdable) ZPopMin(key string, count ...int64) *ZSliceCmd {
1780 args := []interface{}{
1789 args = append(args, count[0])
1791 panic("too many arguments")
1794 cmd := NewZSliceCmd(args...)
1799 func (c *cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
1800 args := []interface{}{
1807 args = append(args, "withscores")
1809 cmd := NewStringSliceCmd(args...)
1814 func (c *cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
1815 return c.zRange(key, start, stop, false)
1818 func (c *cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1819 cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
1824 type ZRangeBy struct {
1829 func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
1830 args := []interface{}{zcmd, key, opt.Min, opt.Max}
1832 args = append(args, "withscores")
1834 if opt.Offset != 0 || opt.Count != 0 {
1842 cmd := NewStringSliceCmd(args...)
1847 func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1848 return c.zRangeBy("zrangebyscore", key, opt, false)
1851 func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1852 return c.zRangeBy("zrangebylex", key, opt, false)
1855 func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1856 args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
1857 if opt.Offset != 0 || opt.Count != 0 {
1865 cmd := NewZSliceCmd(args...)
1870 func (c *cmdable) ZRank(key, member string) *IntCmd {
1871 cmd := NewIntCmd("zrank", key, member)
1876 func (c *cmdable) ZRem(key string, members ...interface{}) *IntCmd {
1877 args := make([]interface{}, 2, 2+len(members))
1880 args = appendArgs(args, members)
1881 cmd := NewIntCmd(args...)
1886 func (c *cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
1897 func (c *cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
1898 cmd := NewIntCmd("zremrangebyscore", key, min, max)
1903 func (c *cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
1904 cmd := NewIntCmd("zremrangebylex", key, min, max)
1909 func (c *cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
1910 cmd := NewStringSliceCmd("zrevrange", key, start, stop)
1915 func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1916 cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
1921 func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
1922 args := []interface{}{zcmd, key, opt.Max, opt.Min}
1923 if opt.Offset != 0 || opt.Count != 0 {
1931 cmd := NewStringSliceCmd(args...)
1936 func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1937 return c.zRevRangeBy("zrevrangebyscore", key, opt)
1940 func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1941 return c.zRevRangeBy("zrevrangebylex", key, opt)
1944 func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1945 args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
1946 if opt.Offset != 0 || opt.Count != 0 {
1954 cmd := NewZSliceCmd(args...)
1959 func (c *cmdable) ZRevRank(key, member string) *IntCmd {
1960 cmd := NewIntCmd("zrevrank", key, member)
1965 func (c *cmdable) ZScore(key, member string) *FloatCmd {
1966 cmd := NewFloatCmd("zscore", key, member)
1971 func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
1972 args := make([]interface{}, 3+len(keys))
1973 args[0] = "zunionstore"
1976 for i, key := range keys {
1979 if len(store.Weights) > 0 {
1980 args = append(args, "weights")
1981 for _, weight := range store.Weights {
1982 args = append(args, weight)
1985 if store.Aggregate != "" {
1986 args = append(args, "aggregate", store.Aggregate)
1988 cmd := NewIntCmd(args...)
1993 //------------------------------------------------------------------------------
1995 func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
1996 args := make([]interface{}, 2, 2+len(els))
1999 args = appendArgs(args, els)
2000 cmd := NewIntCmd(args...)
2005 func (c *cmdable) PFCount(keys ...string) *IntCmd {
2006 args := make([]interface{}, 1+len(keys))
2008 for i, key := range keys {
2011 cmd := NewIntCmd(args...)
2016 func (c *cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
2017 args := make([]interface{}, 2+len(keys))
2020 for i, key := range keys {
2023 cmd := NewStatusCmd(args...)
2028 //------------------------------------------------------------------------------
2030 func (c *cmdable) BgRewriteAOF() *StatusCmd {
2031 cmd := NewStatusCmd("bgrewriteaof")
2036 func (c *cmdable) BgSave() *StatusCmd {
2037 cmd := NewStatusCmd("bgsave")
2042 func (c *cmdable) ClientKill(ipPort string) *StatusCmd {
2043 cmd := NewStatusCmd("client", "kill", ipPort)
2048 // ClientKillByFilter is new style synx, while the ClientKill is old
2049 // CLIENT KILL <option> [value] ... <option> [value]
2050 func (c *cmdable) ClientKillByFilter(keys ...string) *IntCmd {
2051 args := make([]interface{}, 2+len(keys))
2054 for i, key := range keys {
2057 cmd := NewIntCmd(args...)
2062 func (c *cmdable) ClientList() *StringCmd {
2063 cmd := NewStringCmd("client", "list")
2068 func (c *cmdable) ClientPause(dur time.Duration) *BoolCmd {
2069 cmd := NewBoolCmd("client", "pause", formatMs(dur))
2074 func (c *cmdable) ClientID() *IntCmd {
2075 cmd := NewIntCmd("client", "id")
2080 func (c *cmdable) ClientUnblock(id int64) *IntCmd {
2081 cmd := NewIntCmd("client", "unblock", id)
2086 func (c *cmdable) ClientUnblockWithError(id int64) *IntCmd {
2087 cmd := NewIntCmd("client", "unblock", id, "error")
2092 // ClientSetName assigns a name to the connection.
2093 func (c *statefulCmdable) ClientSetName(name string) *BoolCmd {
2094 cmd := NewBoolCmd("client", "setname", name)
2099 // ClientGetName returns the name of the connection.
2100 func (c *cmdable) ClientGetName() *StringCmd {
2101 cmd := NewStringCmd("client", "getname")
2106 func (c *cmdable) ConfigGet(parameter string) *SliceCmd {
2107 cmd := NewSliceCmd("config", "get", parameter)
2112 func (c *cmdable) ConfigResetStat() *StatusCmd {
2113 cmd := NewStatusCmd("config", "resetstat")
2118 func (c *cmdable) ConfigSet(parameter, value string) *StatusCmd {
2119 cmd := NewStatusCmd("config", "set", parameter, value)
2124 func (c *cmdable) ConfigRewrite() *StatusCmd {
2125 cmd := NewStatusCmd("config", "rewrite")
2130 // Deperecated. Use DBSize instead.
2131 func (c *cmdable) DbSize() *IntCmd {
2135 func (c *cmdable) DBSize() *IntCmd {
2136 cmd := NewIntCmd("dbsize")
2141 func (c *cmdable) FlushAll() *StatusCmd {
2142 cmd := NewStatusCmd("flushall")
2147 func (c *cmdable) FlushAllAsync() *StatusCmd {
2148 cmd := NewStatusCmd("flushall", "async")
2153 // Deprecated. Use FlushDB instead.
2154 func (c *cmdable) FlushDb() *StatusCmd {
2158 func (c *cmdable) FlushDB() *StatusCmd {
2159 cmd := NewStatusCmd("flushdb")
2164 func (c *cmdable) FlushDBAsync() *StatusCmd {
2165 cmd := NewStatusCmd("flushdb", "async")
2170 func (c *cmdable) Info(section ...string) *StringCmd {
2171 args := []interface{}{"info"}
2172 if len(section) > 0 {
2173 args = append(args, section[0])
2175 cmd := NewStringCmd(args...)
2180 func (c *cmdable) LastSave() *IntCmd {
2181 cmd := NewIntCmd("lastsave")
2186 func (c *cmdable) Save() *StatusCmd {
2187 cmd := NewStatusCmd("save")
2192 func (c *cmdable) shutdown(modifier string) *StatusCmd {
2193 var args []interface{}
2195 args = []interface{}{"shutdown"}
2197 args = []interface{}{"shutdown", modifier}
2199 cmd := NewStatusCmd(args...)
2201 if err := cmd.Err(); err != nil {
2203 // Server quit as expected.
2207 // Server did not quit. String reply contains the reason.
2208 cmd.err = errors.New(cmd.val)
2214 func (c *cmdable) Shutdown() *StatusCmd {
2215 return c.shutdown("")
2218 func (c *cmdable) ShutdownSave() *StatusCmd {
2219 return c.shutdown("save")
2222 func (c *cmdable) ShutdownNoSave() *StatusCmd {
2223 return c.shutdown("nosave")
2226 func (c *cmdable) SlaveOf(host, port string) *StatusCmd {
2227 cmd := NewStatusCmd("slaveof", host, port)
2232 func (c *cmdable) SlowLog() {
2233 panic("not implemented")
2236 func (c *cmdable) Sync() {
2237 panic("not implemented")
2240 func (c *cmdable) Time() *TimeCmd {
2241 cmd := NewTimeCmd("time")
2246 //------------------------------------------------------------------------------
2248 func (c *cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
2249 cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
2252 cmdArgs[2] = len(keys)
2253 for i, key := range keys {
2256 cmdArgs = appendArgs(cmdArgs, args)
2257 cmd := NewCmd(cmdArgs...)
2262 func (c *cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
2263 cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
2264 cmdArgs[0] = "evalsha"
2266 cmdArgs[2] = len(keys)
2267 for i, key := range keys {
2270 cmdArgs = appendArgs(cmdArgs, args)
2271 cmd := NewCmd(cmdArgs...)
2276 func (c *cmdable) ScriptExists(hashes ...string) *BoolSliceCmd {
2277 args := make([]interface{}, 2+len(hashes))
2280 for i, hash := range hashes {
2283 cmd := NewBoolSliceCmd(args...)
2288 func (c *cmdable) ScriptFlush() *StatusCmd {
2289 cmd := NewStatusCmd("script", "flush")
2294 func (c *cmdable) ScriptKill() *StatusCmd {
2295 cmd := NewStatusCmd("script", "kill")
2300 func (c *cmdable) ScriptLoad(script string) *StringCmd {
2301 cmd := NewStringCmd("script", "load", script)
2306 //------------------------------------------------------------------------------
2308 func (c *cmdable) DebugObject(key string) *StringCmd {
2309 cmd := NewStringCmd("debug", "object", key)
2314 //------------------------------------------------------------------------------
2316 // Publish posts the message to the channel.
2317 func (c *cmdable) Publish(channel string, message interface{}) *IntCmd {
2318 cmd := NewIntCmd("publish", channel, message)
2323 func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd {
2324 args := []interface{}{"pubsub", "channels"}
2326 args = append(args, pattern)
2328 cmd := NewStringSliceCmd(args...)
2333 func (c *cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
2334 args := make([]interface{}, 2+len(channels))
2337 for i, channel := range channels {
2340 cmd := NewStringIntMapCmd(args...)
2345 func (c *cmdable) PubSubNumPat() *IntCmd {
2346 cmd := NewIntCmd("pubsub", "numpat")
2351 //------------------------------------------------------------------------------
2353 func (c *cmdable) ClusterSlots() *ClusterSlotsCmd {
2354 cmd := NewClusterSlotsCmd("cluster", "slots")
2359 func (c *cmdable) ClusterNodes() *StringCmd {
2360 cmd := NewStringCmd("cluster", "nodes")
2365 func (c *cmdable) ClusterMeet(host, port string) *StatusCmd {
2366 cmd := NewStatusCmd("cluster", "meet", host, port)
2371 func (c *cmdable) ClusterForget(nodeID string) *StatusCmd {
2372 cmd := NewStatusCmd("cluster", "forget", nodeID)
2377 func (c *cmdable) ClusterReplicate(nodeID string) *StatusCmd {
2378 cmd := NewStatusCmd("cluster", "replicate", nodeID)
2383 func (c *cmdable) ClusterResetSoft() *StatusCmd {
2384 cmd := NewStatusCmd("cluster", "reset", "soft")
2389 func (c *cmdable) ClusterResetHard() *StatusCmd {
2390 cmd := NewStatusCmd("cluster", "reset", "hard")
2395 func (c *cmdable) ClusterInfo() *StringCmd {
2396 cmd := NewStringCmd("cluster", "info")
2401 func (c *cmdable) ClusterKeySlot(key string) *IntCmd {
2402 cmd := NewIntCmd("cluster", "keyslot", key)
2407 func (c *cmdable) ClusterGetKeysInSlot(slot int, count int) *StringSliceCmd {
2408 cmd := NewStringSliceCmd("cluster", "getkeysinslot", slot, count)
2413 func (c *cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
2414 cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
2419 func (c *cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
2420 cmd := NewIntCmd("cluster", "countkeysinslot", slot)
2425 func (c *cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
2426 args := make([]interface{}, 2+len(slots))
2428 args[1] = "delslots"
2429 for i, slot := range slots {
2432 cmd := NewStatusCmd(args...)
2437 func (c *cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
2438 size := max - min + 1
2439 slots := make([]int, size)
2440 for i := 0; i < size; i++ {
2443 return c.ClusterDelSlots(slots...)
2446 func (c *cmdable) ClusterSaveConfig() *StatusCmd {
2447 cmd := NewStatusCmd("cluster", "saveconfig")
2452 func (c *cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
2453 cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
2458 func (c *cmdable) ReadOnly() *StatusCmd {
2459 cmd := NewStatusCmd("readonly")
2464 func (c *cmdable) ReadWrite() *StatusCmd {
2465 cmd := NewStatusCmd("readwrite")
2470 func (c *cmdable) ClusterFailover() *StatusCmd {
2471 cmd := NewStatusCmd("cluster", "failover")
2476 func (c *cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
2477 args := make([]interface{}, 2+len(slots))
2479 args[1] = "addslots"
2480 for i, num := range slots {
2483 cmd := NewStatusCmd(args...)
2488 func (c *cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
2489 size := max - min + 1
2490 slots := make([]int, size)
2491 for i := 0; i < size; i++ {
2494 return c.ClusterAddSlots(slots...)
2497 //------------------------------------------------------------------------------
2499 func (c *cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
2500 args := make([]interface{}, 2+3*len(geoLocation))
2503 for i, eachLoc := range geoLocation {
2504 args[2+3*i] = eachLoc.Longitude
2505 args[2+3*i+1] = eachLoc.Latitude
2506 args[2+3*i+2] = eachLoc.Name
2508 cmd := NewIntCmd(args...)
2513 func (c *cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
2514 cmd := NewGeoLocationCmd(query, "georadius", key, longitude, latitude)
2519 func (c *cmdable) GeoRadiusRO(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
2520 cmd := NewGeoLocationCmd(query, "georadius_ro", key, longitude, latitude)
2525 func (c *cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
2526 cmd := NewGeoLocationCmd(query, "georadiusbymember", key, member)
2531 func (c *cmdable) GeoRadiusByMemberRO(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
2532 cmd := NewGeoLocationCmd(query, "georadiusbymember_ro", key, member)
2537 func (c *cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
2541 cmd := NewFloatCmd("geodist", key, member1, member2, unit)
2546 func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
2547 args := make([]interface{}, 2+len(members))
2550 for i, member := range members {
2553 cmd := NewStringSliceCmd(args...)
2558 func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
2559 args := make([]interface{}, 2+len(members))
2562 for i, member := range members {
2565 cmd := NewGeoPosCmd(args...)
2570 //------------------------------------------------------------------------------
2572 func (c *cmdable) MemoryUsage(key string, samples ...int) *IntCmd {
2573 args := []interface{}{"memory", "usage", key}
2574 if len(samples) > 0 {
2575 if len(samples) != 1 {
2576 panic("MemoryUsage expects single sample count")
2578 args = append(args, "SAMPLES", samples[0])
2580 cmd := NewIntCmd(args...)