8 "github.com/go-redis/redis/internal"
11 func readTimeout(timeout time.Duration) time.Duration {
15 return timeout + 10*time.Second
18 func usePrecise(dur time.Duration) bool {
19 return dur < time.Second || dur%time.Second != 0
22 func formatMs(dur time.Duration) int64 {
23 if dur > 0 && dur < time.Millisecond {
25 "specified duration is %s, but minimal supported value is %s",
26 dur, time.Millisecond,
29 return int64(dur / time.Millisecond)
32 func formatSec(dur time.Duration) int64 {
33 if dur > 0 && dur < time.Second {
35 "specified duration is %s, but minimal supported value is %s",
39 return int64(dur / time.Second)
42 func appendArgs(dst, src []interface{}) []interface{} {
44 if ss, ok := src[0].([]string); ok {
45 for _, s := range ss {
52 for _, v := range src {
58 type Cmdable interface {
60 Pipelined(fn func(Pipeliner) error) ([]Cmder, error)
62 TxPipelined(fn func(Pipeliner) error) ([]Cmder, error)
63 TxPipeline() Pipeliner
65 Command() *CommandsInfoCmd
66 ClientGetName() *StringCmd
67 Echo(message interface{}) *StringCmd
70 Del(keys ...string) *IntCmd
71 Unlink(keys ...string) *IntCmd
72 Dump(key string) *StringCmd
73 Exists(keys ...string) *IntCmd
74 Expire(key string, expiration time.Duration) *BoolCmd
75 ExpireAt(key string, tm time.Time) *BoolCmd
76 Keys(pattern string) *StringSliceCmd
77 Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd
78 Move(key string, db int64) *BoolCmd
79 ObjectRefCount(key string) *IntCmd
80 ObjectEncoding(key string) *StringCmd
81 ObjectIdleTime(key string) *DurationCmd
82 Persist(key string) *BoolCmd
83 PExpire(key string, expiration time.Duration) *BoolCmd
84 PExpireAt(key string, tm time.Time) *BoolCmd
85 PTTL(key string) *DurationCmd
86 RandomKey() *StringCmd
87 Rename(key, newkey string) *StatusCmd
88 RenameNX(key, newkey string) *BoolCmd
89 Restore(key string, ttl time.Duration, value string) *StatusCmd
90 RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
91 Sort(key string, sort *Sort) *StringSliceCmd
92 SortStore(key, store string, sort *Sort) *IntCmd
93 SortInterfaces(key string, sort *Sort) *SliceCmd
94 Touch(keys ...string) *IntCmd
95 TTL(key string) *DurationCmd
96 Type(key string) *StatusCmd
97 Scan(cursor uint64, match string, count int64) *ScanCmd
98 SScan(key string, cursor uint64, match string, count int64) *ScanCmd
99 HScan(key string, cursor uint64, match string, count int64) *ScanCmd
100 ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
101 Append(key, value string) *IntCmd
102 BitCount(key string, bitCount *BitCount) *IntCmd
103 BitOpAnd(destKey string, keys ...string) *IntCmd
104 BitOpOr(destKey string, keys ...string) *IntCmd
105 BitOpXor(destKey string, keys ...string) *IntCmd
106 BitOpNot(destKey string, key string) *IntCmd
107 BitPos(key string, bit int64, pos ...int64) *IntCmd
108 Decr(key string) *IntCmd
109 DecrBy(key string, decrement int64) *IntCmd
110 Get(key string) *StringCmd
111 GetBit(key string, offset int64) *IntCmd
112 GetRange(key string, start, end int64) *StringCmd
113 GetSet(key string, value interface{}) *StringCmd
114 Incr(key string) *IntCmd
115 IncrBy(key string, value int64) *IntCmd
116 IncrByFloat(key string, value float64) *FloatCmd
117 MGet(keys ...string) *SliceCmd
118 MSet(pairs ...interface{}) *StatusCmd
119 MSetNX(pairs ...interface{}) *BoolCmd
120 Set(key string, value interface{}, expiration time.Duration) *StatusCmd
121 SetBit(key string, offset int64, value int) *IntCmd
122 SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
123 SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd
124 SetRange(key string, offset int64, value string) *IntCmd
125 StrLen(key string) *IntCmd
126 HDel(key string, fields ...string) *IntCmd
127 HExists(key, field string) *BoolCmd
128 HGet(key, field string) *StringCmd
129 HGetAll(key string) *StringStringMapCmd
130 HIncrBy(key, field string, incr int64) *IntCmd
131 HIncrByFloat(key, field string, incr float64) *FloatCmd
132 HKeys(key string) *StringSliceCmd
133 HLen(key string) *IntCmd
134 HMGet(key string, fields ...string) *SliceCmd
135 HMSet(key string, fields map[string]interface{}) *StatusCmd
136 HSet(key, field string, value interface{}) *BoolCmd
137 HSetNX(key, field string, value interface{}) *BoolCmd
138 HVals(key string) *StringSliceCmd
139 BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
140 BRPop(timeout time.Duration, keys ...string) *StringSliceCmd
141 BRPopLPush(source, destination string, timeout time.Duration) *StringCmd
142 LIndex(key string, index int64) *StringCmd
143 LInsert(key, op string, pivot, value interface{}) *IntCmd
144 LInsertBefore(key string, pivot, value interface{}) *IntCmd
145 LInsertAfter(key string, pivot, value interface{}) *IntCmd
146 LLen(key string) *IntCmd
147 LPop(key string) *StringCmd
148 LPush(key string, values ...interface{}) *IntCmd
149 LPushX(key string, value interface{}) *IntCmd
150 LRange(key string, start, stop int64) *StringSliceCmd
151 LRem(key string, count int64, value interface{}) *IntCmd
152 LSet(key string, index int64, value interface{}) *StatusCmd
153 LTrim(key string, start, stop int64) *StatusCmd
154 RPop(key string) *StringCmd
155 RPopLPush(source, destination string) *StringCmd
156 RPush(key string, values ...interface{}) *IntCmd
157 RPushX(key string, value interface{}) *IntCmd
158 SAdd(key string, members ...interface{}) *IntCmd
159 SCard(key string) *IntCmd
160 SDiff(keys ...string) *StringSliceCmd
161 SDiffStore(destination string, keys ...string) *IntCmd
162 SInter(keys ...string) *StringSliceCmd
163 SInterStore(destination string, keys ...string) *IntCmd
164 SIsMember(key string, member interface{}) *BoolCmd
165 SMembers(key string) *StringSliceCmd
166 SMembersMap(key string) *StringStructMapCmd
167 SMove(source, destination string, member interface{}) *BoolCmd
168 SPop(key string) *StringCmd
169 SPopN(key string, count int64) *StringSliceCmd
170 SRandMember(key string) *StringCmd
171 SRandMemberN(key string, count int64) *StringSliceCmd
172 SRem(key string, members ...interface{}) *IntCmd
173 SUnion(keys ...string) *StringSliceCmd
174 SUnionStore(destination string, keys ...string) *IntCmd
175 XAdd(stream, id string, els map[string]interface{}) *StringCmd
176 XAddExt(opt *XAddExt) *StringCmd
177 XLen(key string) *IntCmd
178 XRange(stream, start, stop string) *XMessageSliceCmd
179 XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd
180 XRevRange(stream string, start, stop string) *XMessageSliceCmd
181 XRevRangeN(stream string, start, stop string, count int64) *XMessageSliceCmd
182 XRead(streams ...string) *XStreamSliceCmd
183 XReadN(count int64, streams ...string) *XStreamSliceCmd
184 XReadExt(opt *XReadExt) *XStreamSliceCmd
185 ZAdd(key string, members ...Z) *IntCmd
186 ZAddNX(key string, members ...Z) *IntCmd
187 ZAddXX(key string, members ...Z) *IntCmd
188 ZAddCh(key string, members ...Z) *IntCmd
189 ZAddNXCh(key string, members ...Z) *IntCmd
190 ZAddXXCh(key string, members ...Z) *IntCmd
191 ZIncr(key string, member Z) *FloatCmd
192 ZIncrNX(key string, member Z) *FloatCmd
193 ZIncrXX(key string, member Z) *FloatCmd
194 ZCard(key string) *IntCmd
195 ZCount(key, min, max string) *IntCmd
196 ZLexCount(key, min, max string) *IntCmd
197 ZIncrBy(key string, increment float64, member string) *FloatCmd
198 ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
199 ZRange(key string, start, stop int64) *StringSliceCmd
200 ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
201 ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
202 ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
203 ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
204 ZRank(key, member string) *IntCmd
205 ZRem(key string, members ...interface{}) *IntCmd
206 ZRemRangeByRank(key string, start, stop int64) *IntCmd
207 ZRemRangeByScore(key, min, max string) *IntCmd
208 ZRemRangeByLex(key, min, max string) *IntCmd
209 ZRevRange(key string, start, stop int64) *StringSliceCmd
210 ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
211 ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
212 ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
213 ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
214 ZRevRank(key, member string) *IntCmd
215 ZScore(key, member string) *FloatCmd
216 ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
217 PFAdd(key string, els ...interface{}) *IntCmd
218 PFCount(keys ...string) *IntCmd
219 PFMerge(dest string, keys ...string) *StatusCmd
220 BgRewriteAOF() *StatusCmd
222 ClientKill(ipPort string) *StatusCmd
223 ClientKillByFilter(keys ...string) *IntCmd
224 ClientList() *StringCmd
225 ClientPause(dur time.Duration) *BoolCmd
226 ConfigGet(parameter string) *SliceCmd
227 ConfigResetStat() *StatusCmd
228 ConfigSet(parameter, value string) *StatusCmd
229 ConfigRewrite() *StatusCmd
231 FlushAll() *StatusCmd
232 FlushAllAsync() *StatusCmd
234 FlushDBAsync() *StatusCmd
235 Info(section ...string) *StringCmd
238 Shutdown() *StatusCmd
239 ShutdownSave() *StatusCmd
240 ShutdownNoSave() *StatusCmd
241 SlaveOf(host, port string) *StatusCmd
243 Eval(script string, keys []string, args ...interface{}) *Cmd
244 EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
245 ScriptExists(hashes ...string) *BoolSliceCmd
246 ScriptFlush() *StatusCmd
247 ScriptKill() *StatusCmd
248 ScriptLoad(script string) *StringCmd
249 DebugObject(key string) *StringCmd
250 Publish(channel string, message interface{}) *IntCmd
251 PubSubChannels(pattern string) *StringSliceCmd
252 PubSubNumSub(channels ...string) *StringIntMapCmd
253 PubSubNumPat() *IntCmd
254 ClusterSlots() *ClusterSlotsCmd
255 ClusterNodes() *StringCmd
256 ClusterMeet(host, port string) *StatusCmd
257 ClusterForget(nodeID string) *StatusCmd
258 ClusterReplicate(nodeID string) *StatusCmd
259 ClusterResetSoft() *StatusCmd
260 ClusterResetHard() *StatusCmd
261 ClusterInfo() *StringCmd
262 ClusterKeySlot(key string) *IntCmd
263 ClusterCountFailureReports(nodeID string) *IntCmd
264 ClusterCountKeysInSlot(slot int) *IntCmd
265 ClusterDelSlots(slots ...int) *StatusCmd
266 ClusterDelSlotsRange(min, max int) *StatusCmd
267 ClusterSaveConfig() *StatusCmd
268 ClusterSlaves(nodeID string) *StringSliceCmd
269 ClusterFailover() *StatusCmd
270 ClusterAddSlots(slots ...int) *StatusCmd
271 ClusterAddSlotsRange(min, max int) *StatusCmd
272 GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
273 GeoPos(key string, members ...string) *GeoPosCmd
274 GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
275 GeoRadiusRO(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
276 GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
277 GeoRadiusByMemberRO(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
278 GeoDist(key string, member1, member2, unit string) *FloatCmd
279 GeoHash(key string, members ...string) *StringSliceCmd
280 ReadOnly() *StatusCmd
281 ReadWrite() *StatusCmd
282 MemoryUsage(key string, samples ...int) *IntCmd
285 type StatefulCmdable interface {
287 Auth(password string) *StatusCmd
288 Select(index int) *StatusCmd
289 SwapDB(index1, index2 int) *StatusCmd
290 ClientSetName(name string) *BoolCmd
293 var _ Cmdable = (*Client)(nil)
294 var _ Cmdable = (*Tx)(nil)
295 var _ Cmdable = (*Ring)(nil)
296 var _ Cmdable = (*ClusterClient)(nil)
298 type cmdable struct {
299 process func(cmd Cmder) error
302 func (c *cmdable) setProcessor(fn func(Cmder) error) {
306 type statefulCmdable struct {
308 process func(cmd Cmder) error
311 func (c *statefulCmdable) setProcessor(fn func(Cmder) error) {
313 c.cmdable.setProcessor(fn)
316 //------------------------------------------------------------------------------
318 func (c *statefulCmdable) Auth(password string) *StatusCmd {
319 cmd := NewStatusCmd("auth", password)
324 func (c *cmdable) Echo(message interface{}) *StringCmd {
325 cmd := NewStringCmd("echo", message)
330 func (c *cmdable) Ping() *StatusCmd {
331 cmd := NewStatusCmd("ping")
336 func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
337 cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
342 func (c *cmdable) Quit() *StatusCmd {
343 panic("not implemented")
346 func (c *statefulCmdable) Select(index int) *StatusCmd {
347 cmd := NewStatusCmd("select", index)
352 func (c *statefulCmdable) SwapDB(index1, index2 int) *StatusCmd {
353 cmd := NewStatusCmd("swapdb", index1, index2)
358 //------------------------------------------------------------------------------
360 func (c *cmdable) Command() *CommandsInfoCmd {
361 cmd := NewCommandsInfoCmd("command")
366 func (c *cmdable) Del(keys ...string) *IntCmd {
367 args := make([]interface{}, 1+len(keys))
369 for i, key := range keys {
372 cmd := NewIntCmd(args...)
377 func (c *cmdable) Unlink(keys ...string) *IntCmd {
378 args := make([]interface{}, 1+len(keys))
380 for i, key := range keys {
383 cmd := NewIntCmd(args...)
388 func (c *cmdable) Dump(key string) *StringCmd {
389 cmd := NewStringCmd("dump", key)
394 func (c *cmdable) Exists(keys ...string) *IntCmd {
395 args := make([]interface{}, 1+len(keys))
397 for i, key := range keys {
400 cmd := NewIntCmd(args...)
405 func (c *cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
406 cmd := NewBoolCmd("expire", key, formatSec(expiration))
411 func (c *cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
412 cmd := NewBoolCmd("expireat", key, tm.Unix())
417 func (c *cmdable) Keys(pattern string) *StringSliceCmd {
418 cmd := NewStringSliceCmd("keys", pattern)
423 func (c *cmdable) Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd {
432 cmd.setReadTimeout(timeout)
437 func (c *cmdable) Move(key string, db int64) *BoolCmd {
438 cmd := NewBoolCmd("move", key, db)
443 func (c *cmdable) ObjectRefCount(key string) *IntCmd {
444 cmd := NewIntCmd("object", "refcount", key)
449 func (c *cmdable) ObjectEncoding(key string) *StringCmd {
450 cmd := NewStringCmd("object", "encoding", key)
455 func (c *cmdable) ObjectIdleTime(key string) *DurationCmd {
456 cmd := NewDurationCmd(time.Second, "object", "idletime", key)
461 func (c *cmdable) Persist(key string) *BoolCmd {
462 cmd := NewBoolCmd("persist", key)
467 func (c *cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
468 cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
473 func (c *cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
477 tm.UnixNano()/int64(time.Millisecond),
483 func (c *cmdable) PTTL(key string) *DurationCmd {
484 cmd := NewDurationCmd(time.Millisecond, "pttl", key)
489 func (c *cmdable) RandomKey() *StringCmd {
490 cmd := NewStringCmd("randomkey")
495 func (c *cmdable) Rename(key, newkey string) *StatusCmd {
496 cmd := NewStatusCmd("rename", key, newkey)
501 func (c *cmdable) RenameNX(key, newkey string) *BoolCmd {
502 cmd := NewBoolCmd("renamenx", key, newkey)
507 func (c *cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
518 func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
538 func (sort *Sort) args(key string) []interface{} {
539 args := []interface{}{"sort", key}
541 args = append(args, "by", sort.By)
543 if sort.Offset != 0 || sort.Count != 0 {
544 args = append(args, "limit", sort.Offset, sort.Count)
546 for _, get := range sort.Get {
547 args = append(args, "get", get)
549 if sort.Order != "" {
550 args = append(args, sort.Order)
553 args = append(args, "alpha")
558 func (c *cmdable) Sort(key string, sort *Sort) *StringSliceCmd {
559 cmd := NewStringSliceCmd(sort.args(key)...)
564 func (c *cmdable) SortStore(key, store string, sort *Sort) *IntCmd {
565 args := sort.args(key)
567 args = append(args, "store", store)
569 cmd := NewIntCmd(args...)
574 func (c *cmdable) SortInterfaces(key string, sort *Sort) *SliceCmd {
575 cmd := NewSliceCmd(sort.args(key)...)
580 func (c *cmdable) Touch(keys ...string) *IntCmd {
581 args := make([]interface{}, len(keys)+1)
583 for i, key := range keys {
586 cmd := NewIntCmd(args...)
591 func (c *cmdable) TTL(key string) *DurationCmd {
592 cmd := NewDurationCmd(time.Second, "ttl", key)
597 func (c *cmdable) Type(key string) *StatusCmd {
598 cmd := NewStatusCmd("type", key)
603 func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
604 args := []interface{}{"scan", cursor}
606 args = append(args, "match", match)
609 args = append(args, "count", count)
611 cmd := NewScanCmd(c.process, args...)
616 func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
617 args := []interface{}{"sscan", key, cursor}
619 args = append(args, "match", match)
622 args = append(args, "count", count)
624 cmd := NewScanCmd(c.process, args...)
629 func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
630 args := []interface{}{"hscan", key, cursor}
632 args = append(args, "match", match)
635 args = append(args, "count", count)
637 cmd := NewScanCmd(c.process, args...)
642 func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
643 args := []interface{}{"zscan", key, cursor}
645 args = append(args, "match", match)
648 args = append(args, "count", count)
650 cmd := NewScanCmd(c.process, args...)
655 //------------------------------------------------------------------------------
657 func (c *cmdable) Append(key, value string) *IntCmd {
658 cmd := NewIntCmd("append", key, value)
663 type BitCount struct {
667 func (c *cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
668 args := []interface{}{"bitcount", key}
676 cmd := NewIntCmd(args...)
681 func (c *cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
682 args := make([]interface{}, 3+len(keys))
686 for i, key := range keys {
689 cmd := NewIntCmd(args...)
694 func (c *cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
695 return c.bitOp("and", destKey, keys...)
698 func (c *cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
699 return c.bitOp("or", destKey, keys...)
702 func (c *cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
703 return c.bitOp("xor", destKey, keys...)
706 func (c *cmdable) BitOpNot(destKey string, key string) *IntCmd {
707 return c.bitOp("not", destKey, key)
710 func (c *cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
711 args := make([]interface{}, 3+len(pos))
723 panic("too many arguments")
725 cmd := NewIntCmd(args...)
730 func (c *cmdable) Decr(key string) *IntCmd {
731 cmd := NewIntCmd("decr", key)
736 func (c *cmdable) DecrBy(key string, decrement int64) *IntCmd {
737 cmd := NewIntCmd("decrby", key, decrement)
742 // Redis `GET key` command. It returns redis.Nil error when key does not exist.
743 func (c *cmdable) Get(key string) *StringCmd {
744 cmd := NewStringCmd("get", key)
749 func (c *cmdable) GetBit(key string, offset int64) *IntCmd {
750 cmd := NewIntCmd("getbit", key, offset)
755 func (c *cmdable) GetRange(key string, start, end int64) *StringCmd {
756 cmd := NewStringCmd("getrange", key, start, end)
761 func (c *cmdable) GetSet(key string, value interface{}) *StringCmd {
762 cmd := NewStringCmd("getset", key, value)
767 func (c *cmdable) Incr(key string) *IntCmd {
768 cmd := NewIntCmd("incr", key)
773 func (c *cmdable) IncrBy(key string, value int64) *IntCmd {
774 cmd := NewIntCmd("incrby", key, value)
779 func (c *cmdable) IncrByFloat(key string, value float64) *FloatCmd {
780 cmd := NewFloatCmd("incrbyfloat", key, value)
785 func (c *cmdable) MGet(keys ...string) *SliceCmd {
786 args := make([]interface{}, 1+len(keys))
788 for i, key := range keys {
791 cmd := NewSliceCmd(args...)
796 func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
797 args := make([]interface{}, 1, 1+len(pairs))
799 args = appendArgs(args, pairs)
800 cmd := NewStatusCmd(args...)
805 func (c *cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
806 args := make([]interface{}, 1, 1+len(pairs))
808 args = appendArgs(args, pairs)
809 cmd := NewBoolCmd(args...)
814 // Redis `SET key value [expiration]` command.
816 // Use expiration for `SETEX`-like behavior.
817 // Zero expiration means the key has no expiration time.
818 func (c *cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
819 args := make([]interface{}, 3, 4)
824 if usePrecise(expiration) {
825 args = append(args, "px", formatMs(expiration))
827 args = append(args, "ex", formatSec(expiration))
830 cmd := NewStatusCmd(args...)
835 func (c *cmdable) SetBit(key string, offset int64, value int) *IntCmd {
846 // Redis `SET key value [expiration] NX` command.
848 // Zero expiration means the key has no expiration time.
849 func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
852 // Use old `SETNX` to support old Redis versions.
853 cmd = NewBoolCmd("setnx", key, value)
855 if usePrecise(expiration) {
856 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
858 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
865 // Redis `SET key value [expiration] XX` command.
867 // Zero expiration means the key has no expiration time.
868 func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
871 cmd = NewBoolCmd("set", key, value, "xx")
873 if usePrecise(expiration) {
874 cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
876 cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
883 func (c *cmdable) SetRange(key string, offset int64, value string) *IntCmd {
884 cmd := NewIntCmd("setrange", key, offset, value)
889 func (c *cmdable) StrLen(key string) *IntCmd {
890 cmd := NewIntCmd("strlen", key)
895 //------------------------------------------------------------------------------
897 func (c *cmdable) HDel(key string, fields ...string) *IntCmd {
898 args := make([]interface{}, 2+len(fields))
901 for i, field := range fields {
904 cmd := NewIntCmd(args...)
909 func (c *cmdable) HExists(key, field string) *BoolCmd {
910 cmd := NewBoolCmd("hexists", key, field)
915 func (c *cmdable) HGet(key, field string) *StringCmd {
916 cmd := NewStringCmd("hget", key, field)
921 func (c *cmdable) HGetAll(key string) *StringStringMapCmd {
922 cmd := NewStringStringMapCmd("hgetall", key)
927 func (c *cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
928 cmd := NewIntCmd("hincrby", key, field, incr)
933 func (c *cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
934 cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
939 func (c *cmdable) HKeys(key string) *StringSliceCmd {
940 cmd := NewStringSliceCmd("hkeys", key)
945 func (c *cmdable) HLen(key string) *IntCmd {
946 cmd := NewIntCmd("hlen", key)
951 func (c *cmdable) HMGet(key string, fields ...string) *SliceCmd {
952 args := make([]interface{}, 2+len(fields))
955 for i, field := range fields {
958 cmd := NewSliceCmd(args...)
963 func (c *cmdable) HMSet(key string, fields map[string]interface{}) *StatusCmd {
964 args := make([]interface{}, 2+len(fields)*2)
968 for k, v := range fields {
973 cmd := NewStatusCmd(args...)
978 func (c *cmdable) HSet(key, field string, value interface{}) *BoolCmd {
979 cmd := NewBoolCmd("hset", key, field, value)
984 func (c *cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
985 cmd := NewBoolCmd("hsetnx", key, field, value)
990 func (c *cmdable) HVals(key string) *StringSliceCmd {
991 cmd := NewStringSliceCmd("hvals", key)
996 //------------------------------------------------------------------------------
998 func (c *cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
999 args := make([]interface{}, 1+len(keys)+1)
1001 for i, key := range keys {
1004 args[len(args)-1] = formatSec(timeout)
1005 cmd := NewStringSliceCmd(args...)
1006 cmd.setReadTimeout(timeout)
1011 func (c *cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
1012 args := make([]interface{}, 1+len(keys)+1)
1014 for i, key := range keys {
1017 args[len(keys)+1] = formatSec(timeout)
1018 cmd := NewStringSliceCmd(args...)
1019 cmd.setReadTimeout(timeout)
1024 func (c *cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
1025 cmd := NewStringCmd(
1031 cmd.setReadTimeout(timeout)
1036 func (c *cmdable) LIndex(key string, index int64) *StringCmd {
1037 cmd := NewStringCmd("lindex", key, index)
1042 func (c *cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
1043 cmd := NewIntCmd("linsert", key, op, pivot, value)
1048 func (c *cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
1049 cmd := NewIntCmd("linsert", key, "before", pivot, value)
1054 func (c *cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
1055 cmd := NewIntCmd("linsert", key, "after", pivot, value)
1060 func (c *cmdable) LLen(key string) *IntCmd {
1061 cmd := NewIntCmd("llen", key)
1066 func (c *cmdable) LPop(key string) *StringCmd {
1067 cmd := NewStringCmd("lpop", key)
1072 func (c *cmdable) LPush(key string, values ...interface{}) *IntCmd {
1073 args := make([]interface{}, 2, 2+len(values))
1076 args = appendArgs(args, values)
1077 cmd := NewIntCmd(args...)
1082 func (c *cmdable) LPushX(key string, value interface{}) *IntCmd {
1083 cmd := NewIntCmd("lpushx", key, value)
1088 func (c *cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
1089 cmd := NewStringSliceCmd(
1099 func (c *cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
1100 cmd := NewIntCmd("lrem", key, count, value)
1105 func (c *cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
1106 cmd := NewStatusCmd("lset", key, index, value)
1111 func (c *cmdable) LTrim(key string, start, stop int64) *StatusCmd {
1112 cmd := NewStatusCmd(
1122 func (c *cmdable) RPop(key string) *StringCmd {
1123 cmd := NewStringCmd("rpop", key)
1128 func (c *cmdable) RPopLPush(source, destination string) *StringCmd {
1129 cmd := NewStringCmd("rpoplpush", source, destination)
1134 func (c *cmdable) RPush(key string, values ...interface{}) *IntCmd {
1135 args := make([]interface{}, 2, 2+len(values))
1138 args = appendArgs(args, values)
1139 cmd := NewIntCmd(args...)
1144 func (c *cmdable) RPushX(key string, value interface{}) *IntCmd {
1145 cmd := NewIntCmd("rpushx", key, value)
1150 //------------------------------------------------------------------------------
1152 func (c *cmdable) SAdd(key string, members ...interface{}) *IntCmd {
1153 args := make([]interface{}, 2, 2+len(members))
1156 args = appendArgs(args, members)
1157 cmd := NewIntCmd(args...)
1162 func (c *cmdable) SCard(key string) *IntCmd {
1163 cmd := NewIntCmd("scard", key)
1168 func (c *cmdable) SDiff(keys ...string) *StringSliceCmd {
1169 args := make([]interface{}, 1+len(keys))
1171 for i, key := range keys {
1174 cmd := NewStringSliceCmd(args...)
1179 func (c *cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
1180 args := make([]interface{}, 2+len(keys))
1181 args[0] = "sdiffstore"
1182 args[1] = destination
1183 for i, key := range keys {
1186 cmd := NewIntCmd(args...)
1191 func (c *cmdable) SInter(keys ...string) *StringSliceCmd {
1192 args := make([]interface{}, 1+len(keys))
1194 for i, key := range keys {
1197 cmd := NewStringSliceCmd(args...)
1202 func (c *cmdable) SInterStore(destination string, keys ...string) *IntCmd {
1203 args := make([]interface{}, 2+len(keys))
1204 args[0] = "sinterstore"
1205 args[1] = destination
1206 for i, key := range keys {
1209 cmd := NewIntCmd(args...)
1214 func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
1215 cmd := NewBoolCmd("sismember", key, member)
1220 // Redis `SMEMBERS key` command output as a slice
1221 func (c *cmdable) SMembers(key string) *StringSliceCmd {
1222 cmd := NewStringSliceCmd("smembers", key)
1227 // Redis `SMEMBERS key` command output as a map
1228 func (c *cmdable) SMembersMap(key string) *StringStructMapCmd {
1229 cmd := NewStringStructMapCmd("smembers", key)
1234 func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
1235 cmd := NewBoolCmd("smove", source, destination, member)
1240 // Redis `SPOP key` command.
1241 func (c *cmdable) SPop(key string) *StringCmd {
1242 cmd := NewStringCmd("spop", key)
1247 // Redis `SPOP key count` command.
1248 func (c *cmdable) SPopN(key string, count int64) *StringSliceCmd {
1249 cmd := NewStringSliceCmd("spop", key, count)
1254 // Redis `SRANDMEMBER key` command.
1255 func (c *cmdable) SRandMember(key string) *StringCmd {
1256 cmd := NewStringCmd("srandmember", key)
1261 // Redis `SRANDMEMBER key count` command.
1262 func (c *cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
1263 cmd := NewStringSliceCmd("srandmember", key, count)
1268 func (c *cmdable) SRem(key string, members ...interface{}) *IntCmd {
1269 args := make([]interface{}, 2, 2+len(members))
1272 args = appendArgs(args, members)
1273 cmd := NewIntCmd(args...)
1278 func (c *cmdable) SUnion(keys ...string) *StringSliceCmd {
1279 args := make([]interface{}, 1+len(keys))
1281 for i, key := range keys {
1284 cmd := NewStringSliceCmd(args...)
1289 func (c *cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
1290 args := make([]interface{}, 2+len(keys))
1291 args[0] = "sunionstore"
1292 args[1] = destination
1293 for i, key := range keys {
1296 cmd := NewIntCmd(args...)
1301 //------------------------------------------------------------------------------
1303 type XAddExt struct {
1305 MaxLen int64 // MAXLEN N
1306 MaxLenApprox int64 // MAXLEN ~ N
1308 Values map[string]interface{}
1311 func (c *cmdable) XAddExt(opt *XAddExt) *StringCmd {
1312 a := make([]interface{}, 0, 6+len(opt.Values)*2)
1313 a = append(a, "xadd")
1314 a = append(a, opt.Stream)
1316 a = append(a, "maxlen", opt.MaxLen)
1317 } else if opt.MaxLenApprox > 0 {
1318 a = append(a, "maxlen", "~", opt.MaxLenApprox)
1321 a = append(a, opt.ID)
1325 for k, v := range opt.Values {
1330 cmd := NewStringCmd(a...)
1335 func (c *cmdable) XAdd(stream, id string, values map[string]interface{}) *StringCmd {
1336 return c.XAddExt(&XAddExt{
1343 func (c *cmdable) XLen(key string) *IntCmd {
1344 cmd := NewIntCmd("xlen", key)
1349 func (c *cmdable) XRange(stream, start, stop string) *XMessageSliceCmd {
1350 cmd := NewXMessageSliceCmd("xrange", stream, start, stop)
1355 func (c *cmdable) XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
1356 cmd := NewXMessageSliceCmd("xrange", stream, start, stop, "count", count)
1361 func (c *cmdable) XRevRange(stream, start, stop string) *XMessageSliceCmd {
1362 cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop)
1367 func (c *cmdable) XRevRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
1368 cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop, "count", count)
1373 type XReadExt struct {
1379 func (c *cmdable) XReadExt(opt *XReadExt) *XStreamSliceCmd {
1380 a := make([]interface{}, 0, 5+len(opt.Streams))
1381 a = append(a, "xread")
1384 a = append(a, "count")
1385 a = append(a, opt.Count)
1388 a = append(a, "block")
1389 a = append(a, int64(opt.Block/time.Millisecond))
1392 a = append(a, "streams")
1393 for _, s := range opt.Streams {
1397 cmd := NewXStreamSliceCmd(a...)
1402 func (c *cmdable) XRead(streams ...string) *XStreamSliceCmd {
1403 return c.XReadExt(&XReadExt{
1409 func (c *cmdable) XReadN(count int64, streams ...string) *XStreamSliceCmd {
1410 return c.XReadExt(&XReadExt{
1417 func (c *cmdable) XReadBlock(block time.Duration, streams ...string) *XStreamSliceCmd {
1418 return c.XReadExt(&XReadExt{
1424 //------------------------------------------------------------------------------
1426 // Z represents sorted set member.
1432 // ZStore is used as an arg to ZInterStore and ZUnionStore.
1433 type ZStore struct {
1435 // Can be SUM, MIN or MAX.
1439 func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
1440 for i, m := range members {
1442 a[n+2*i+1] = m.Member
1444 cmd := NewIntCmd(a...)
1449 // Redis `ZADD key score member [score member ...]` command.
1450 func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
1452 a := make([]interface{}, n+2*len(members))
1453 a[0], a[1] = "zadd", key
1454 return c.zAdd(a, n, members...)
1457 // Redis `ZADD key NX score member [score member ...]` command.
1458 func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
1460 a := make([]interface{}, n+2*len(members))
1461 a[0], a[1], a[2] = "zadd", key, "nx"
1462 return c.zAdd(a, n, members...)
1465 // Redis `ZADD key XX score member [score member ...]` command.
1466 func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
1468 a := make([]interface{}, n+2*len(members))
1469 a[0], a[1], a[2] = "zadd", key, "xx"
1470 return c.zAdd(a, n, members...)
1473 // Redis `ZADD key CH score member [score member ...]` command.
1474 func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
1476 a := make([]interface{}, n+2*len(members))
1477 a[0], a[1], a[2] = "zadd", key, "ch"
1478 return c.zAdd(a, n, members...)
1481 // Redis `ZADD key NX CH score member [score member ...]` command.
1482 func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
1484 a := make([]interface{}, n+2*len(members))
1485 a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
1486 return c.zAdd(a, n, members...)
1489 // Redis `ZADD key XX CH score member [score member ...]` command.
1490 func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
1492 a := make([]interface{}, n+2*len(members))
1493 a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
1494 return c.zAdd(a, n, members...)
1497 func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
1498 for i, m := range members {
1500 a[n+2*i+1] = m.Member
1502 cmd := NewFloatCmd(a...)
1507 // Redis `ZADD key INCR score member` command.
1508 func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
1510 a := make([]interface{}, n+2)
1511 a[0], a[1], a[2] = "zadd", key, "incr"
1512 return c.zIncr(a, n, member)
1515 // Redis `ZADD key NX INCR score member` command.
1516 func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
1518 a := make([]interface{}, n+2)
1519 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
1520 return c.zIncr(a, n, member)
1523 // Redis `ZADD key XX INCR score member` command.
1524 func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
1526 a := make([]interface{}, n+2)
1527 a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
1528 return c.zIncr(a, n, member)
1531 func (c *cmdable) ZCard(key string) *IntCmd {
1532 cmd := NewIntCmd("zcard", key)
1537 func (c *cmdable) ZCount(key, min, max string) *IntCmd {
1538 cmd := NewIntCmd("zcount", key, min, max)
1543 func (c *cmdable) ZLexCount(key, min, max string) *IntCmd {
1544 cmd := NewIntCmd("zlexcount", key, min, max)
1549 func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
1550 cmd := NewFloatCmd("zincrby", key, increment, member)
1555 func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
1556 args := make([]interface{}, 3+len(keys))
1557 args[0] = "zinterstore"
1558 args[1] = destination
1560 for i, key := range keys {
1563 if len(store.Weights) > 0 {
1564 args = append(args, "weights")
1565 for _, weight := range store.Weights {
1566 args = append(args, weight)
1569 if store.Aggregate != "" {
1570 args = append(args, "aggregate", store.Aggregate)
1572 cmd := NewIntCmd(args...)
1577 func (c *cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
1578 args := []interface{}{
1585 args = append(args, "withscores")
1587 cmd := NewStringSliceCmd(args...)
1592 func (c *cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
1593 return c.zRange(key, start, stop, false)
1596 func (c *cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1597 cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
1602 type ZRangeBy struct {
1607 func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
1608 args := []interface{}{zcmd, key, opt.Min, opt.Max}
1610 args = append(args, "withscores")
1612 if opt.Offset != 0 || opt.Count != 0 {
1620 cmd := NewStringSliceCmd(args...)
1625 func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1626 return c.zRangeBy("zrangebyscore", key, opt, false)
1629 func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1630 return c.zRangeBy("zrangebylex", key, opt, false)
1633 func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1634 args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
1635 if opt.Offset != 0 || opt.Count != 0 {
1643 cmd := NewZSliceCmd(args...)
1648 func (c *cmdable) ZRank(key, member string) *IntCmd {
1649 cmd := NewIntCmd("zrank", key, member)
1654 func (c *cmdable) ZRem(key string, members ...interface{}) *IntCmd {
1655 args := make([]interface{}, 2, 2+len(members))
1658 args = appendArgs(args, members)
1659 cmd := NewIntCmd(args...)
1664 func (c *cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
1675 func (c *cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
1676 cmd := NewIntCmd("zremrangebyscore", key, min, max)
1681 func (c *cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
1682 cmd := NewIntCmd("zremrangebylex", key, min, max)
1687 func (c *cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
1688 cmd := NewStringSliceCmd("zrevrange", key, start, stop)
1693 func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
1694 cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
1699 func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
1700 args := []interface{}{zcmd, key, opt.Max, opt.Min}
1701 if opt.Offset != 0 || opt.Count != 0 {
1709 cmd := NewStringSliceCmd(args...)
1714 func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
1715 return c.zRevRangeBy("zrevrangebyscore", key, opt)
1718 func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
1719 return c.zRevRangeBy("zrevrangebylex", key, opt)
1722 func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
1723 args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
1724 if opt.Offset != 0 || opt.Count != 0 {
1732 cmd := NewZSliceCmd(args...)
1737 func (c *cmdable) ZRevRank(key, member string) *IntCmd {
1738 cmd := NewIntCmd("zrevrank", key, member)
1743 func (c *cmdable) ZScore(key, member string) *FloatCmd {
1744 cmd := NewFloatCmd("zscore", key, member)
1749 func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
1750 args := make([]interface{}, 3+len(keys))
1751 args[0] = "zunionstore"
1754 for i, key := range keys {
1757 if len(store.Weights) > 0 {
1758 args = append(args, "weights")
1759 for _, weight := range store.Weights {
1760 args = append(args, weight)
1763 if store.Aggregate != "" {
1764 args = append(args, "aggregate", store.Aggregate)
1766 cmd := NewIntCmd(args...)
1771 //------------------------------------------------------------------------------
1773 func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
1774 args := make([]interface{}, 2, 2+len(els))
1777 args = appendArgs(args, els)
1778 cmd := NewIntCmd(args...)
1783 func (c *cmdable) PFCount(keys ...string) *IntCmd {
1784 args := make([]interface{}, 1+len(keys))
1786 for i, key := range keys {
1789 cmd := NewIntCmd(args...)
1794 func (c *cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
1795 args := make([]interface{}, 2+len(keys))
1798 for i, key := range keys {
1801 cmd := NewStatusCmd(args...)
1806 //------------------------------------------------------------------------------
1808 func (c *cmdable) BgRewriteAOF() *StatusCmd {
1809 cmd := NewStatusCmd("bgrewriteaof")
1814 func (c *cmdable) BgSave() *StatusCmd {
1815 cmd := NewStatusCmd("bgsave")
1820 func (c *cmdable) ClientKill(ipPort string) *StatusCmd {
1821 cmd := NewStatusCmd("client", "kill", ipPort)
1826 // ClientKillByFilter is new style synx, while the ClientKill is old
1827 // CLIENT KILL <option> [value] ... <option> [value]
1828 func (c *cmdable) ClientKillByFilter(keys ...string) *IntCmd {
1829 args := make([]interface{}, 2+len(keys))
1832 for i, key := range keys {
1835 cmd := NewIntCmd(args...)
1840 func (c *cmdable) ClientList() *StringCmd {
1841 cmd := NewStringCmd("client", "list")
1846 func (c *cmdable) ClientPause(dur time.Duration) *BoolCmd {
1847 cmd := NewBoolCmd("client", "pause", formatMs(dur))
1852 // ClientSetName assigns a name to the connection.
1853 func (c *statefulCmdable) ClientSetName(name string) *BoolCmd {
1854 cmd := NewBoolCmd("client", "setname", name)
1859 // ClientGetName returns the name of the connection.
1860 func (c *cmdable) ClientGetName() *StringCmd {
1861 cmd := NewStringCmd("client", "getname")
1866 func (c *cmdable) ConfigGet(parameter string) *SliceCmd {
1867 cmd := NewSliceCmd("config", "get", parameter)
1872 func (c *cmdable) ConfigResetStat() *StatusCmd {
1873 cmd := NewStatusCmd("config", "resetstat")
1878 func (c *cmdable) ConfigSet(parameter, value string) *StatusCmd {
1879 cmd := NewStatusCmd("config", "set", parameter, value)
1884 func (c *cmdable) ConfigRewrite() *StatusCmd {
1885 cmd := NewStatusCmd("config", "rewrite")
1890 // Deperecated. Use DBSize instead.
1891 func (c *cmdable) DbSize() *IntCmd {
1895 func (c *cmdable) DBSize() *IntCmd {
1896 cmd := NewIntCmd("dbsize")
1901 func (c *cmdable) FlushAll() *StatusCmd {
1902 cmd := NewStatusCmd("flushall")
1907 func (c *cmdable) FlushAllAsync() *StatusCmd {
1908 cmd := NewStatusCmd("flushall", "async")
1913 // Deprecated. Use FlushDB instead.
1914 func (c *cmdable) FlushDb() *StatusCmd {
1918 func (c *cmdable) FlushDB() *StatusCmd {
1919 cmd := NewStatusCmd("flushdb")
1924 func (c *cmdable) FlushDBAsync() *StatusCmd {
1925 cmd := NewStatusCmd("flushdb", "async")
1930 func (c *cmdable) Info(section ...string) *StringCmd {
1931 args := []interface{}{"info"}
1932 if len(section) > 0 {
1933 args = append(args, section[0])
1935 cmd := NewStringCmd(args...)
1940 func (c *cmdable) LastSave() *IntCmd {
1941 cmd := NewIntCmd("lastsave")
1946 func (c *cmdable) Save() *StatusCmd {
1947 cmd := NewStatusCmd("save")
1952 func (c *cmdable) shutdown(modifier string) *StatusCmd {
1953 var args []interface{}
1955 args = []interface{}{"shutdown"}
1957 args = []interface{}{"shutdown", modifier}
1959 cmd := NewStatusCmd(args...)
1961 if err := cmd.Err(); err != nil {
1963 // Server quit as expected.
1967 // Server did not quit. String reply contains the reason.
1968 cmd.err = errors.New(cmd.val)
1974 func (c *cmdable) Shutdown() *StatusCmd {
1975 return c.shutdown("")
1978 func (c *cmdable) ShutdownSave() *StatusCmd {
1979 return c.shutdown("save")
1982 func (c *cmdable) ShutdownNoSave() *StatusCmd {
1983 return c.shutdown("nosave")
1986 func (c *cmdable) SlaveOf(host, port string) *StatusCmd {
1987 cmd := NewStatusCmd("slaveof", host, port)
1992 func (c *cmdable) SlowLog() {
1993 panic("not implemented")
1996 func (c *cmdable) Sync() {
1997 panic("not implemented")
2000 func (c *cmdable) Time() *TimeCmd {
2001 cmd := NewTimeCmd("time")
2006 //------------------------------------------------------------------------------
2008 func (c *cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
2009 cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
2012 cmdArgs[2] = len(keys)
2013 for i, key := range keys {
2016 cmdArgs = appendArgs(cmdArgs, args)
2017 cmd := NewCmd(cmdArgs...)
2022 func (c *cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
2023 cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
2024 cmdArgs[0] = "evalsha"
2026 cmdArgs[2] = len(keys)
2027 for i, key := range keys {
2030 cmdArgs = appendArgs(cmdArgs, args)
2031 cmd := NewCmd(cmdArgs...)
2036 func (c *cmdable) ScriptExists(hashes ...string) *BoolSliceCmd {
2037 args := make([]interface{}, 2+len(hashes))
2040 for i, hash := range hashes {
2043 cmd := NewBoolSliceCmd(args...)
2048 func (c *cmdable) ScriptFlush() *StatusCmd {
2049 cmd := NewStatusCmd("script", "flush")
2054 func (c *cmdable) ScriptKill() *StatusCmd {
2055 cmd := NewStatusCmd("script", "kill")
2060 func (c *cmdable) ScriptLoad(script string) *StringCmd {
2061 cmd := NewStringCmd("script", "load", script)
2066 //------------------------------------------------------------------------------
2068 func (c *cmdable) DebugObject(key string) *StringCmd {
2069 cmd := NewStringCmd("debug", "object", key)
2074 //------------------------------------------------------------------------------
2076 // Publish posts the message to the channel.
2077 func (c *cmdable) Publish(channel string, message interface{}) *IntCmd {
2078 cmd := NewIntCmd("publish", channel, message)
2083 func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd {
2084 args := []interface{}{"pubsub", "channels"}
2086 args = append(args, pattern)
2088 cmd := NewStringSliceCmd(args...)
2093 func (c *cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
2094 args := make([]interface{}, 2+len(channels))
2097 for i, channel := range channels {
2100 cmd := NewStringIntMapCmd(args...)
2105 func (c *cmdable) PubSubNumPat() *IntCmd {
2106 cmd := NewIntCmd("pubsub", "numpat")
2111 //------------------------------------------------------------------------------
2113 func (c *cmdable) ClusterSlots() *ClusterSlotsCmd {
2114 cmd := NewClusterSlotsCmd("cluster", "slots")
2119 func (c *cmdable) ClusterNodes() *StringCmd {
2120 cmd := NewStringCmd("cluster", "nodes")
2125 func (c *cmdable) ClusterMeet(host, port string) *StatusCmd {
2126 cmd := NewStatusCmd("cluster", "meet", host, port)
2131 func (c *cmdable) ClusterForget(nodeID string) *StatusCmd {
2132 cmd := NewStatusCmd("cluster", "forget", nodeID)
2137 func (c *cmdable) ClusterReplicate(nodeID string) *StatusCmd {
2138 cmd := NewStatusCmd("cluster", "replicate", nodeID)
2143 func (c *cmdable) ClusterResetSoft() *StatusCmd {
2144 cmd := NewStatusCmd("cluster", "reset", "soft")
2149 func (c *cmdable) ClusterResetHard() *StatusCmd {
2150 cmd := NewStatusCmd("cluster", "reset", "hard")
2155 func (c *cmdable) ClusterInfo() *StringCmd {
2156 cmd := NewStringCmd("cluster", "info")
2161 func (c *cmdable) ClusterKeySlot(key string) *IntCmd {
2162 cmd := NewIntCmd("cluster", "keyslot", key)
2167 func (c *cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
2168 cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
2173 func (c *cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
2174 cmd := NewIntCmd("cluster", "countkeysinslot", slot)
2179 func (c *cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
2180 args := make([]interface{}, 2+len(slots))
2182 args[1] = "delslots"
2183 for i, slot := range slots {
2186 cmd := NewStatusCmd(args...)
2191 func (c *cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
2192 size := max - min + 1
2193 slots := make([]int, size)
2194 for i := 0; i < size; i++ {
2197 return c.ClusterDelSlots(slots...)
2200 func (c *cmdable) ClusterSaveConfig() *StatusCmd {
2201 cmd := NewStatusCmd("cluster", "saveconfig")
2206 func (c *cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
2207 cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
2212 func (c *cmdable) ReadOnly() *StatusCmd {
2213 cmd := NewStatusCmd("readonly")
2218 func (c *cmdable) ReadWrite() *StatusCmd {
2219 cmd := NewStatusCmd("readwrite")
2224 func (c *cmdable) ClusterFailover() *StatusCmd {
2225 cmd := NewStatusCmd("cluster", "failover")
2230 func (c *cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
2231 args := make([]interface{}, 2+len(slots))
2233 args[1] = "addslots"
2234 for i, num := range slots {
2237 cmd := NewStatusCmd(args...)
2242 func (c *cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
2243 size := max - min + 1
2244 slots := make([]int, size)
2245 for i := 0; i < size; i++ {
2248 return c.ClusterAddSlots(slots...)
2251 //------------------------------------------------------------------------------
2253 func (c *cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
2254 args := make([]interface{}, 2+3*len(geoLocation))
2257 for i, eachLoc := range geoLocation {
2258 args[2+3*i] = eachLoc.Longitude
2259 args[2+3*i+1] = eachLoc.Latitude
2260 args[2+3*i+2] = eachLoc.Name
2262 cmd := NewIntCmd(args...)
2267 func (c *cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
2268 cmd := NewGeoLocationCmd(query, "georadius", key, longitude, latitude)
2273 func (c *cmdable) GeoRadiusRO(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
2274 cmd := NewGeoLocationCmd(query, "georadius_ro", key, longitude, latitude)
2279 func (c *cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
2280 cmd := NewGeoLocationCmd(query, "georadiusbymember", key, member)
2285 func (c *cmdable) GeoRadiusByMemberRO(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
2286 cmd := NewGeoLocationCmd(query, "georadiusbymember_ro", key, member)
2291 func (c *cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
2295 cmd := NewFloatCmd("geodist", key, member1, member2, unit)
2300 func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
2301 args := make([]interface{}, 2+len(members))
2304 for i, member := range members {
2307 cmd := NewStringSliceCmd(args...)
2312 func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
2313 args := make([]interface{}, 2+len(members))
2316 for i, member := range members {
2319 cmd := NewGeoPosCmd(args...)
2324 //------------------------------------------------------------------------------
2326 func (c *cmdable) MemoryUsage(key string, samples ...int) *IntCmd {
2327 args := []interface{}{"memory", "usage", key}
2328 if len(samples) > 0 {
2329 if len(samples) != 1 {
2330 panic("MemoryUsage expects single sample count")
2332 args = append(args, "SAMPLES", samples[0])
2334 cmd := NewIntCmd(args...)