mirror of
https://github.com/redis/go-redis.git
synced 2025-06-12 14:21:52 +03:00
Allow setting and scaning interface{} values.
This commit is contained in:
411
commands.go
411
commands.go
@ -7,14 +7,18 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func formatFloat(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func formatInt(i int64) string {
|
||||
return strconv.FormatInt(i, 10)
|
||||
}
|
||||
|
||||
func formatUint(i uint64) string {
|
||||
return strconv.FormatUint(i, 10)
|
||||
}
|
||||
|
||||
func formatFloat(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func readTimeout(timeout time.Duration) time.Duration {
|
||||
if timeout == 0 {
|
||||
return 0
|
||||
@ -22,14 +26,6 @@ func readTimeout(timeout time.Duration) time.Duration {
|
||||
return timeout + time.Second
|
||||
}
|
||||
|
||||
type commandable struct {
|
||||
process func(cmd Cmder)
|
||||
}
|
||||
|
||||
func (c *commandable) Process(cmd Cmder) {
|
||||
c.process(cmd)
|
||||
}
|
||||
|
||||
func usePrecise(dur time.Duration) bool {
|
||||
return dur < time.Second || dur%time.Second != 0
|
||||
}
|
||||
@ -41,7 +37,7 @@ func formatMs(dur time.Duration) string {
|
||||
dur, time.Millisecond,
|
||||
)
|
||||
}
|
||||
return strconv.FormatInt(int64(dur/time.Millisecond), 10)
|
||||
return formatInt(int64(dur / time.Millisecond))
|
||||
}
|
||||
|
||||
func formatSec(dur time.Duration) string {
|
||||
@ -51,7 +47,15 @@ func formatSec(dur time.Duration) string {
|
||||
dur, time.Second,
|
||||
)
|
||||
}
|
||||
return strconv.FormatInt(int64(dur/time.Second), 10)
|
||||
return formatInt(int64(dur / time.Second))
|
||||
}
|
||||
|
||||
type commandable struct {
|
||||
process func(cmd Cmder)
|
||||
}
|
||||
|
||||
func (c *commandable) Process(cmd Cmder) {
|
||||
c.process(cmd)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -80,7 +84,7 @@ func (c *commandable) Quit() *StatusCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) Select(index int64) *StatusCmd {
|
||||
cmd := newKeylessStatusCmd("SELECT", strconv.FormatInt(index, 10))
|
||||
cmd := newKeylessStatusCmd("SELECT", formatInt(index))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -88,7 +92,11 @@ func (c *commandable) Select(index int64) *StatusCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) Del(keys ...string) *IntCmd {
|
||||
args := append([]string{"DEL"}, keys...)
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "DEL"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -113,7 +121,7 @@ func (c *commandable) Expire(key string, expiration time.Duration) *BoolCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ExpireAt(key string, tm time.Time) *BoolCmd {
|
||||
cmd := NewBoolCmd("EXPIREAT", key, strconv.FormatInt(tm.Unix(), 10))
|
||||
cmd := NewBoolCmd("EXPIREAT", key, formatInt(tm.Unix()))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -130,7 +138,7 @@ func (c *commandable) Migrate(host, port, key string, db int64, timeout time.Dur
|
||||
host,
|
||||
port,
|
||||
key,
|
||||
strconv.FormatInt(db, 10),
|
||||
formatInt(db),
|
||||
formatMs(timeout),
|
||||
)
|
||||
cmd._clusterKeyPos = 3
|
||||
@ -140,13 +148,18 @@ func (c *commandable) Migrate(host, port, key string, db int64, timeout time.Dur
|
||||
}
|
||||
|
||||
func (c *commandable) Move(key string, db int64) *BoolCmd {
|
||||
cmd := NewBoolCmd("MOVE", key, strconv.FormatInt(db, 10))
|
||||
cmd := NewBoolCmd("MOVE", key, formatInt(db))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) ObjectRefCount(keys ...string) *IntCmd {
|
||||
args := append([]string{"OBJECT", "REFCOUNT"}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "OBJECT"
|
||||
args[1] = "REFCOUNT"
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
cmd._clusterKeyPos = 2
|
||||
c.Process(cmd)
|
||||
@ -154,7 +167,12 @@ func (c *commandable) ObjectRefCount(keys ...string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ObjectEncoding(keys ...string) *StringCmd {
|
||||
args := append([]string{"OBJECT", "ENCODING"}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "OBJECT"
|
||||
args[1] = "ENCODING"
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewStringCmd(args...)
|
||||
cmd._clusterKeyPos = 2
|
||||
c.Process(cmd)
|
||||
@ -162,7 +180,12 @@ func (c *commandable) ObjectEncoding(keys ...string) *StringCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ObjectIdleTime(keys ...string) *DurationCmd {
|
||||
args := append([]string{"OBJECT", "IDLETIME"}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "OBJECT"
|
||||
args[1] = "IDLETIME"
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewDurationCmd(time.Second, args...)
|
||||
cmd._clusterKeyPos = 2
|
||||
c.Process(cmd)
|
||||
@ -185,7 +208,7 @@ func (c *commandable) PExpireAt(key string, tm time.Time) *BoolCmd {
|
||||
cmd := NewBoolCmd(
|
||||
"PEXPIREAT",
|
||||
key,
|
||||
strconv.FormatInt(tm.UnixNano()/int64(time.Millisecond), 10),
|
||||
formatInt(tm.UnixNano()/int64(time.Millisecond)),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -219,7 +242,7 @@ func (c *commandable) Restore(key string, ttl int64, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd(
|
||||
"RESTORE",
|
||||
key,
|
||||
strconv.FormatInt(ttl, 10),
|
||||
formatInt(ttl),
|
||||
value,
|
||||
)
|
||||
c.Process(cmd)
|
||||
@ -236,7 +259,7 @@ type Sort struct {
|
||||
}
|
||||
|
||||
func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd {
|
||||
args := []string{"SORT", key}
|
||||
args := []interface{}{"SORT", key}
|
||||
if sort.By != "" {
|
||||
args = append(args, "BY", sort.By)
|
||||
}
|
||||
@ -273,12 +296,12 @@ func (c *commandable) Type(key string) *StatusCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) Scan(cursor int64, match string, count int64) *ScanCmd {
|
||||
args := []string{"SCAN", strconv.FormatInt(cursor, 10)}
|
||||
args := []interface{}{"SCAN", formatInt(cursor)}
|
||||
if match != "" {
|
||||
args = append(args, "MATCH", match)
|
||||
}
|
||||
if count > 0 {
|
||||
args = append(args, "COUNT", strconv.FormatInt(count, 10))
|
||||
args = append(args, "COUNT", formatInt(count))
|
||||
}
|
||||
cmd := NewScanCmd(args...)
|
||||
c.Process(cmd)
|
||||
@ -286,12 +309,12 @@ func (c *commandable) Scan(cursor int64, match string, count int64) *ScanCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) SScan(key string, cursor int64, match string, count int64) *ScanCmd {
|
||||
args := []string{"SSCAN", key, strconv.FormatInt(cursor, 10)}
|
||||
args := []interface{}{"SSCAN", key, formatInt(cursor)}
|
||||
if match != "" {
|
||||
args = append(args, "MATCH", match)
|
||||
}
|
||||
if count > 0 {
|
||||
args = append(args, "COUNT", strconv.FormatInt(count, 10))
|
||||
args = append(args, "COUNT", formatInt(count))
|
||||
}
|
||||
cmd := NewScanCmd(args...)
|
||||
c.Process(cmd)
|
||||
@ -299,12 +322,12 @@ func (c *commandable) SScan(key string, cursor int64, match string, count int64)
|
||||
}
|
||||
|
||||
func (c *commandable) HScan(key string, cursor int64, match string, count int64) *ScanCmd {
|
||||
args := []string{"HSCAN", key, strconv.FormatInt(cursor, 10)}
|
||||
args := []interface{}{"HSCAN", key, formatInt(cursor)}
|
||||
if match != "" {
|
||||
args = append(args, "MATCH", match)
|
||||
}
|
||||
if count > 0 {
|
||||
args = append(args, "COUNT", strconv.FormatInt(count, 10))
|
||||
args = append(args, "COUNT", formatInt(count))
|
||||
}
|
||||
cmd := NewScanCmd(args...)
|
||||
c.Process(cmd)
|
||||
@ -312,12 +335,12 @@ func (c *commandable) HScan(key string, cursor int64, match string, count int64)
|
||||
}
|
||||
|
||||
func (c *commandable) ZScan(key string, cursor int64, match string, count int64) *ScanCmd {
|
||||
args := []string{"ZSCAN", key, strconv.FormatInt(cursor, 10)}
|
||||
args := []interface{}{"ZSCAN", key, formatInt(cursor)}
|
||||
if match != "" {
|
||||
args = append(args, "MATCH", match)
|
||||
}
|
||||
if count > 0 {
|
||||
args = append(args, "COUNT", strconv.FormatInt(count, 10))
|
||||
args = append(args, "COUNT", formatInt(count))
|
||||
}
|
||||
cmd := NewScanCmd(args...)
|
||||
c.Process(cmd)
|
||||
@ -337,12 +360,12 @@ type BitCount struct {
|
||||
}
|
||||
|
||||
func (c *commandable) BitCount(key string, bitCount *BitCount) *IntCmd {
|
||||
args := []string{"BITCOUNT", key}
|
||||
args := []interface{}{"BITCOUNT", key}
|
||||
if bitCount != nil {
|
||||
args = append(
|
||||
args,
|
||||
strconv.FormatInt(bitCount.Start, 10),
|
||||
strconv.FormatInt(bitCount.End, 10),
|
||||
formatInt(bitCount.Start),
|
||||
formatInt(bitCount.End),
|
||||
)
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
@ -351,8 +374,13 @@ func (c *commandable) BitCount(key string, bitCount *BitCount) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) bitOp(op, destKey string, keys ...string) *IntCmd {
|
||||
args := []string{"BITOP", op, destKey}
|
||||
args = append(args, keys...)
|
||||
args := make([]interface{}, 3+len(keys))
|
||||
args[0] = "BITOP"
|
||||
args[1] = op
|
||||
args[2] = destKey
|
||||
for i, key := range keys {
|
||||
args[3+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -375,13 +403,17 @@ func (c *commandable) BitOpNot(destKey string, key string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
|
||||
args := []string{"BITPOS", key, formatInt(bit)}
|
||||
args := make([]interface{}, 3+len(pos))
|
||||
args[0] = "BITPOS"
|
||||
args[1] = key
|
||||
args[2] = formatInt(bit)
|
||||
switch len(pos) {
|
||||
case 0:
|
||||
case 1:
|
||||
args = append(args, formatInt(pos[0]))
|
||||
args[3] = formatInt(pos[0])
|
||||
case 2:
|
||||
args = append(args, formatInt(pos[0]), formatInt(pos[1]))
|
||||
args[3] = formatInt(pos[0])
|
||||
args[4] = formatInt(pos[1])
|
||||
default:
|
||||
panic("too many arguments")
|
||||
}
|
||||
@ -397,7 +429,7 @@ func (c *commandable) Decr(key string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) DecrBy(key string, decrement int64) *IntCmd {
|
||||
cmd := NewIntCmd("DECRBY", key, strconv.FormatInt(decrement, 10))
|
||||
cmd := NewIntCmd("DECRBY", key, formatInt(decrement))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -409,7 +441,7 @@ func (c *commandable) Get(key string) *StringCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) GetBit(key string, offset int64) *IntCmd {
|
||||
cmd := NewIntCmd("GETBIT", key, strconv.FormatInt(offset, 10))
|
||||
cmd := NewIntCmd("GETBIT", key, formatInt(offset))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -418,14 +450,14 @@ func (c *commandable) GetRange(key string, start, end int64) *StringCmd {
|
||||
cmd := NewStringCmd(
|
||||
"GETRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(end, 10),
|
||||
formatInt(start),
|
||||
formatInt(end),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) GetSet(key, value string) *StringCmd {
|
||||
func (c *commandable) GetSet(key string, value interface{}) *StringCmd {
|
||||
cmd := NewStringCmd("GETSET", key, value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -438,7 +470,7 @@ func (c *commandable) Incr(key string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) IncrBy(key string, value int64) *IntCmd {
|
||||
cmd := NewIntCmd("INCRBY", key, strconv.FormatInt(value, 10))
|
||||
cmd := NewIntCmd("INCRBY", key, formatInt(value))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -450,28 +482,43 @@ func (c *commandable) IncrByFloat(key string, value float64) *FloatCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) MGet(keys ...string) *SliceCmd {
|
||||
args := append([]string{"MGET"}, keys...)
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "MGET"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewSliceCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) MSet(pairs ...string) *StatusCmd {
|
||||
args := append([]string{"MSET"}, pairs...)
|
||||
args := make([]interface{}, 1+len(pairs))
|
||||
args[0] = "MSET"
|
||||
for i, pair := range pairs {
|
||||
args[1+i] = pair
|
||||
}
|
||||
cmd := NewStatusCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) MSetNX(pairs ...string) *BoolCmd {
|
||||
args := append([]string{"MSETNX"}, pairs...)
|
||||
args := make([]interface{}, 1+len(pairs))
|
||||
args[0] = "MSETNX"
|
||||
for i, pair := range pairs {
|
||||
args[1+i] = pair
|
||||
}
|
||||
cmd := NewBoolCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) Set(key, value string, expiration time.Duration) *StatusCmd {
|
||||
args := []string{"SET", key, value}
|
||||
func (c *commandable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
|
||||
args := make([]interface{}, 3, 5)
|
||||
args[0] = "SET"
|
||||
args[1] = key
|
||||
args[2] = value
|
||||
if expiration > 0 {
|
||||
if usePrecise(expiration) {
|
||||
args = append(args, "PX", formatMs(expiration))
|
||||
@ -488,14 +535,14 @@ func (c *commandable) SetBit(key string, offset int64, value int) *IntCmd {
|
||||
cmd := NewIntCmd(
|
||||
"SETBIT",
|
||||
key,
|
||||
strconv.FormatInt(offset, 10),
|
||||
strconv.FormatInt(int64(value), 10),
|
||||
formatInt(offset),
|
||||
formatInt(int64(value)),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SetNX(key, value string, expiration time.Duration) *BoolCmd {
|
||||
func (c *commandable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
if expiration == 0 {
|
||||
// Use old `SETNX` to support old Redis versions.
|
||||
@ -511,7 +558,7 @@ func (c *commandable) SetNX(key, value string, expiration time.Duration) *BoolCm
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *Client) SetXX(key, value string, expiration time.Duration) *BoolCmd {
|
||||
func (c *Client) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
if usePrecise(expiration) {
|
||||
cmd = NewBoolCmd("SET", key, value, "PX", formatMs(expiration), "XX")
|
||||
@ -523,7 +570,7 @@ func (c *Client) SetXX(key, value string, expiration time.Duration) *BoolCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) SetRange(key string, offset int64, value string) *IntCmd {
|
||||
cmd := NewIntCmd("SETRANGE", key, strconv.FormatInt(offset, 10), value)
|
||||
cmd := NewIntCmd("SETRANGE", key, formatInt(offset), value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -537,7 +584,12 @@ func (c *commandable) StrLen(key string) *IntCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) HDel(key string, fields ...string) *IntCmd {
|
||||
args := append([]string{"HDEL", key}, fields...)
|
||||
args := make([]interface{}, 2+len(fields))
|
||||
args[0] = "HDEL"
|
||||
args[1] = key
|
||||
for i, field := range fields {
|
||||
args[2+i] = field
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -568,7 +620,7 @@ func (c *commandable) HGetAllMap(key string) *StringStringMapCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) HIncrBy(key, field string, incr int64) *IntCmd {
|
||||
cmd := NewIntCmd("HINCRBY", key, field, strconv.FormatInt(incr, 10))
|
||||
cmd := NewIntCmd("HINCRBY", key, field, formatInt(incr))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -592,14 +644,26 @@ func (c *commandable) HLen(key string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) HMGet(key string, fields ...string) *SliceCmd {
|
||||
args := append([]string{"HMGET", key}, fields...)
|
||||
args := make([]interface{}, 2+len(fields))
|
||||
args[0] = "HMGET"
|
||||
args[1] = key
|
||||
for i, field := range fields {
|
||||
args[2+i] = field
|
||||
}
|
||||
cmd := NewSliceCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) HMSet(key, field, value string, pairs ...string) *StatusCmd {
|
||||
args := append([]string{"HMSET", key, field, value}, pairs...)
|
||||
args := make([]interface{}, 4+len(pairs))
|
||||
args[0] = "HMSET"
|
||||
args[1] = key
|
||||
args[2] = field
|
||||
args[3] = value
|
||||
for i, pair := range pairs {
|
||||
args[4+i] = pair
|
||||
}
|
||||
cmd := NewStatusCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -626,8 +690,12 @@ func (c *commandable) HVals(key string) *StringSliceCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
|
||||
args := append([]string{"BLPOP"}, keys...)
|
||||
args = append(args, formatSec(timeout))
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "BLPOP"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
args[len(args)-1] = formatSec(timeout)
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
cmd.setReadTimeout(readTimeout(timeout))
|
||||
c.Process(cmd)
|
||||
@ -635,8 +703,12 @@ func (c *commandable) BLPop(timeout time.Duration, keys ...string) *StringSliceC
|
||||
}
|
||||
|
||||
func (c *commandable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
|
||||
args := append([]string{"BRPOP"}, keys...)
|
||||
args = append(args, formatSec(timeout))
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "BRPOP"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
args[len(args)-1] = formatSec(timeout)
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
cmd.setReadTimeout(readTimeout(timeout))
|
||||
c.Process(cmd)
|
||||
@ -656,7 +728,7 @@ func (c *commandable) BRPopLPush(source, destination string, timeout time.Durati
|
||||
}
|
||||
|
||||
func (c *commandable) LIndex(key string, index int64) *StringCmd {
|
||||
cmd := NewStringCmd("LINDEX", key, strconv.FormatInt(index, 10))
|
||||
cmd := NewStringCmd("LINDEX", key, formatInt(index))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -680,7 +752,12 @@ func (c *commandable) LPop(key string) *StringCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) LPush(key string, values ...string) *IntCmd {
|
||||
args := append([]string{"LPUSH", key}, values...)
|
||||
args := make([]interface{}, 2+len(values))
|
||||
args[0] = "LPUSH"
|
||||
args[1] = key
|
||||
for i, value := range values {
|
||||
args[2+i] = value
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -696,21 +773,21 @@ func (c *commandable) LRange(key string, start, stop int64) *StringSliceCmd {
|
||||
cmd := NewStringSliceCmd(
|
||||
"LRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(stop, 10),
|
||||
formatInt(start),
|
||||
formatInt(stop),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) LRem(key string, count int64, value string) *IntCmd {
|
||||
cmd := NewIntCmd("LREM", key, strconv.FormatInt(count, 10), value)
|
||||
cmd := NewIntCmd("LREM", key, formatInt(count), value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) LSet(key string, index int64, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd("LSET", key, strconv.FormatInt(index, 10), value)
|
||||
cmd := NewStatusCmd("LSET", key, formatInt(index), value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -719,8 +796,8 @@ func (c *commandable) LTrim(key string, start, stop int64) *StatusCmd {
|
||||
cmd := NewStatusCmd(
|
||||
"LTRIM",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(stop, 10),
|
||||
formatInt(start),
|
||||
formatInt(stop),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -739,7 +816,12 @@ func (c *commandable) RPopLPush(source, destination string) *StringCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) RPush(key string, values ...string) *IntCmd {
|
||||
args := append([]string{"RPUSH", key}, values...)
|
||||
args := make([]interface{}, 2+len(values))
|
||||
args[0] = "RPUSH"
|
||||
args[1] = key
|
||||
for i, value := range values {
|
||||
args[2+i] = value
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -754,7 +836,12 @@ func (c *commandable) RPushX(key string, value string) *IntCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) SAdd(key string, members ...string) *IntCmd {
|
||||
args := append([]string{"SADD", key}, members...)
|
||||
args := make([]interface{}, 2+len(members))
|
||||
args[0] = "SADD"
|
||||
args[1] = key
|
||||
for i, member := range members {
|
||||
args[2+i] = member
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -767,28 +854,46 @@ func (c *commandable) SCard(key string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) SDiff(keys ...string) *StringSliceCmd {
|
||||
args := append([]string{"SDIFF"}, keys...)
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "SDIFF"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SDiffStore(destination string, keys ...string) *IntCmd {
|
||||
args := append([]string{"SDIFFSTORE", destination}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "SDIFFSTORE"
|
||||
args[1] = destination
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SInter(keys ...string) *StringSliceCmd {
|
||||
args := append([]string{"SINTER"}, keys...)
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "SINTER"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SInterStore(destination string, keys ...string) *IntCmd {
|
||||
args := append([]string{"SINTERSTORE", destination}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "SINTERSTORE"
|
||||
args[1] = destination
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -825,21 +930,35 @@ func (c *commandable) SRandMember(key string) *StringCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) SRem(key string, members ...string) *IntCmd {
|
||||
args := append([]string{"SREM", key}, members...)
|
||||
args := make([]interface{}, 2+len(members))
|
||||
args[0] = "SREM"
|
||||
args[1] = key
|
||||
for i, member := range members {
|
||||
args[2+i] = member
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SUnion(keys ...string) *StringSliceCmd {
|
||||
args := append([]string{"SUNION"}, keys...)
|
||||
args := make([]interface{}, 1+len(keys))
|
||||
args[0] = "SUNION"
|
||||
for i, key := range keys {
|
||||
args[1+i] = key
|
||||
}
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SUnionStore(destination string, keys ...string) *IntCmd {
|
||||
args := append([]string{"SUNIONSTORE", destination}, keys...)
|
||||
args := make([]interface{}, 2+len(keys))
|
||||
args[0] = "SUNIONSTORE"
|
||||
args[1] = destination
|
||||
for i, key := range keys {
|
||||
args[2+i] = key
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -858,7 +977,7 @@ type ZStore struct {
|
||||
}
|
||||
|
||||
func (c *commandable) ZAdd(key string, members ...Z) *IntCmd {
|
||||
args := make([]string, 2+2*len(members))
|
||||
args := make([]interface{}, 2+2*len(members))
|
||||
args[0] = "ZADD"
|
||||
args[1] = key
|
||||
for i, m := range members {
|
||||
@ -893,12 +1012,17 @@ func (c *commandable) ZInterStore(
|
||||
store ZStore,
|
||||
keys ...string,
|
||||
) *IntCmd {
|
||||
args := []string{"ZINTERSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
|
||||
args = append(args, keys...)
|
||||
args := make([]interface{}, 3+len(keys))
|
||||
args[0] = "ZINTERSTORE"
|
||||
args[1] = destination
|
||||
args[2] = strconv.Itoa(len(keys))
|
||||
for i, key := range keys {
|
||||
args[3+i] = key
|
||||
}
|
||||
if len(store.Weights) > 0 {
|
||||
args = append(args, "WEIGHTS")
|
||||
for _, weight := range store.Weights {
|
||||
args = append(args, strconv.FormatInt(weight, 10))
|
||||
args = append(args, formatInt(weight))
|
||||
}
|
||||
}
|
||||
if store.Aggregate != "" {
|
||||
@ -910,11 +1034,11 @@ func (c *commandable) ZInterStore(
|
||||
}
|
||||
|
||||
func (c *commandable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
|
||||
args := []string{
|
||||
args := []interface{}{
|
||||
"ZRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(stop, 10),
|
||||
formatInt(start),
|
||||
formatInt(stop),
|
||||
}
|
||||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
@ -929,11 +1053,11 @@ func (c *commandable) ZRange(key string, start, stop int64) *StringSliceCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
|
||||
args := []string{
|
||||
args := []interface{}{
|
||||
"ZRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(stop, 10),
|
||||
formatInt(start),
|
||||
formatInt(stop),
|
||||
"WITHSCORES",
|
||||
}
|
||||
cmd := NewZSliceCmd(args...)
|
||||
@ -947,7 +1071,7 @@ type ZRangeByScore struct {
|
||||
}
|
||||
|
||||
func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
|
||||
args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
|
||||
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
|
||||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
}
|
||||
@ -955,8 +1079,8 @@ func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bo
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(opt.Offset, 10),
|
||||
strconv.FormatInt(opt.Count, 10),
|
||||
formatInt(opt.Offset),
|
||||
formatInt(opt.Count),
|
||||
)
|
||||
}
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
@ -969,13 +1093,13 @@ func (c *commandable) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceC
|
||||
}
|
||||
|
||||
func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
|
||||
args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
|
||||
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
|
||||
if opt.Offset != 0 || opt.Count != 0 {
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(opt.Offset, 10),
|
||||
strconv.FormatInt(opt.Count, 10),
|
||||
formatInt(opt.Offset),
|
||||
formatInt(opt.Count),
|
||||
)
|
||||
}
|
||||
cmd := NewZSliceCmd(args...)
|
||||
@ -990,7 +1114,12 @@ func (c *commandable) ZRank(key, member string) *IntCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ZRem(key string, members ...string) *IntCmd {
|
||||
args := append([]string{"ZREM", key}, members...)
|
||||
args := make([]interface{}, 2+len(members))
|
||||
args[0] = "ZREM"
|
||||
args[1] = key
|
||||
for i, member := range members {
|
||||
args[2+i] = member
|
||||
}
|
||||
cmd := NewIntCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -1000,8 +1129,8 @@ func (c *commandable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
|
||||
cmd := NewIntCmd(
|
||||
"ZREMRANGEBYRANK",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
strconv.FormatInt(stop, 10),
|
||||
formatInt(start),
|
||||
formatInt(stop),
|
||||
)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
@ -1026,13 +1155,13 @@ func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSlice
|
||||
}
|
||||
|
||||
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
|
||||
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
|
||||
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
|
||||
if opt.Offset != 0 || opt.Count != 0 {
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(opt.Offset, 10),
|
||||
strconv.FormatInt(opt.Count, 10),
|
||||
formatInt(opt.Offset),
|
||||
formatInt(opt.Count),
|
||||
)
|
||||
}
|
||||
cmd := NewStringSliceCmd(args...)
|
||||
@ -1041,13 +1170,13 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSli
|
||||
}
|
||||
|
||||
func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
|
||||
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
|
||||
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
|
||||
if opt.Offset != 0 || opt.Count != 0 {
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(opt.Offset, 10),
|
||||
strconv.FormatInt(opt.Count, 10),
|
||||
formatInt(opt.Offset),
|
||||
formatInt(opt.Count),
|
||||
)
|
||||
}
|
||||
cmd := NewZSliceCmd(args...)
|
||||
@ -1068,12 +1197,17 @@ func (c *commandable) ZScore(key, member string) *FloatCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
|
||||
args := []string{"ZUNIONSTORE", dest, strconv.FormatInt(int64(len(keys)), 10)}
|
||||
args = append(args, keys...)
|
||||
args := make([]interface{}, 3+len(keys))
|
||||
args[0] = "ZUNIONSTORE"
|
||||
args[1] = dest
|
||||
args[2] = strconv.Itoa(len(keys))
|
||||
for i, key := range keys {
|
||||
args[3+i] = key
|
||||
}
|
||||
if len(store.Weights) > 0 {
|
||||
args = append(args, "WEIGHTS")
|
||||
for _, weight := range store.Weights {
|
||||
args = append(args, strconv.FormatInt(weight, 10))
|
||||
args = append(args, formatInt(weight))
|
||||
}
|
||||
}
|
||||
if store.Aggregate != "" {
|
||||
@ -1182,11 +1316,11 @@ func (c *commandable) Save() *StatusCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) shutdown(modifier string) *StatusCmd {
|
||||
var args []string
|
||||
var args []interface{}
|
||||
if modifier == "" {
|
||||
args = []string{"SHUTDOWN"}
|
||||
args = []interface{}{"SHUTDOWN"}
|
||||
} else {
|
||||
args = []string{"SHUTDOWN", modifier}
|
||||
args = []interface{}{"SHUTDOWN", modifier}
|
||||
}
|
||||
cmd := newKeylessStatusCmd(args...)
|
||||
c.Process(cmd)
|
||||
@ -1239,9 +1373,17 @@ func (c *commandable) Time() *StringSliceCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) Eval(script string, keys []string, args []string) *Cmd {
|
||||
cmdArgs := []string{"EVAL", script, strconv.FormatInt(int64(len(keys)), 10)}
|
||||
cmdArgs = append(cmdArgs, keys...)
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
||||
cmdArgs[0] = "EVAL"
|
||||
cmdArgs[1] = script
|
||||
cmdArgs[2] = strconv.Itoa(len(keys))
|
||||
for i, key := range keys {
|
||||
cmdArgs[3+i] = key
|
||||
}
|
||||
pos := 3 + len(keys)
|
||||
for i, arg := range args {
|
||||
cmdArgs[pos+i] = arg
|
||||
}
|
||||
cmd := NewCmd(cmdArgs...)
|
||||
if len(keys) > 0 {
|
||||
cmd._clusterKeyPos = 3
|
||||
@ -1251,9 +1393,17 @@ func (c *commandable) Eval(script string, keys []string, args []string) *Cmd {
|
||||
}
|
||||
|
||||
func (c *commandable) EvalSha(sha1 string, keys []string, args []string) *Cmd {
|
||||
cmdArgs := []string{"EVALSHA", sha1, strconv.FormatInt(int64(len(keys)), 10)}
|
||||
cmdArgs = append(cmdArgs, keys...)
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
cmdArgs := make([]interface{}, 3+len(keys)+len(args))
|
||||
cmdArgs[0] = "EVALSHA"
|
||||
cmdArgs[1] = sha1
|
||||
cmdArgs[2] = strconv.Itoa(len(keys))
|
||||
for i, key := range keys {
|
||||
cmdArgs[3+i] = key
|
||||
}
|
||||
pos := 3 + len(keys)
|
||||
for i, arg := range args {
|
||||
cmdArgs[pos+i] = arg
|
||||
}
|
||||
cmd := NewCmd(cmdArgs...)
|
||||
if len(keys) > 0 {
|
||||
cmd._clusterKeyPos = 3
|
||||
@ -1263,7 +1413,12 @@ func (c *commandable) EvalSha(sha1 string, keys []string, args []string) *Cmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ScriptExists(scripts ...string) *BoolSliceCmd {
|
||||
args := append([]string{"SCRIPT", "EXISTS"}, scripts...)
|
||||
args := make([]interface{}, 2+len(scripts))
|
||||
args[0] = "SCRIPT"
|
||||
args[1] = "EXISTS"
|
||||
for i, script := range scripts {
|
||||
args[2+i] = script
|
||||
}
|
||||
cmd := NewBoolSliceCmd(args...)
|
||||
cmd._clusterKeyPos = 0
|
||||
c.Process(cmd)
|
||||
@ -1301,7 +1456,7 @@ func (c *commandable) DebugObject(key string) *StringCmd {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) PubSubChannels(pattern string) *StringSliceCmd {
|
||||
args := []string{"PUBSUB", "CHANNELS"}
|
||||
args := []interface{}{"PUBSUB", "CHANNELS"}
|
||||
if pattern != "*" {
|
||||
args = append(args, pattern)
|
||||
}
|
||||
@ -1312,8 +1467,12 @@ func (c *commandable) PubSubChannels(pattern string) *StringSliceCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) PubSubNumSub(channels ...string) *StringIntMapCmd {
|
||||
args := []string{"PUBSUB", "NUMSUB"}
|
||||
args = append(args, channels...)
|
||||
args := make([]interface{}, 2+len(channels))
|
||||
args[0] = "PUBSUB"
|
||||
args[1] = "NUMSUB"
|
||||
for i, channel := range channels {
|
||||
args[2+i] = channel
|
||||
}
|
||||
cmd := NewStringIntMapCmd(args...)
|
||||
cmd._clusterKeyPos = 0
|
||||
c.Process(cmd)
|
||||
@ -1369,11 +1528,11 @@ func (c *commandable) ClusterFailover() *StatusCmd {
|
||||
}
|
||||
|
||||
func (c *commandable) ClusterAddSlots(slots ...int) *StatusCmd {
|
||||
args := make([]string, len(slots)+2)
|
||||
args := make([]interface{}, 2+len(slots))
|
||||
args[0] = "CLUSTER"
|
||||
args[1] = "addslots"
|
||||
args[1] = "ADDSLOTS"
|
||||
for i, num := range slots {
|
||||
args[i+2] = strconv.Itoa(num)
|
||||
args[2+i] = strconv.Itoa(num)
|
||||
}
|
||||
cmd := newKeylessStatusCmd(args...)
|
||||
c.Process(cmd)
|
||||
|
Reference in New Issue
Block a user