diff --git a/v2/command.go b/v2/command.go new file mode 100644 index 00000000..8ac0ee26 --- /dev/null +++ b/v2/command.go @@ -0,0 +1,333 @@ +package redis + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +type Cmder interface { + args() []string + parseReply(reader) (interface{}, error) + setErr(error) + setVal(interface{}) + + writeTimeout() *time.Duration + readTimeout() *time.Duration + + Err() error +} + +//------------------------------------------------------------------------------ + +type baseCmd struct { + _args []string + + val interface{} + err error + + _writeTimeout, _readTimeout *time.Duration +} + +func newBaseCmd(args ...string) *baseCmd { + return &baseCmd{ + _args: args, + } +} + +func (cmd *baseCmd) String() string { + args := strings.Join(cmd._args, " ") + if cmd.err != nil { + return args + ": " + cmd.err.Error() + } else if cmd.val != nil { + return args + ": " + fmt.Sprint(cmd.val) + } + return args +} + +func (cmd *baseCmd) Err() error { + if cmd.err != nil { + return cmd.err + } + if cmd.val == nil { + return errValNotSet + } + return nil +} + +func (cmd *baseCmd) args() []string { + return cmd._args +} + +func (cmd *baseCmd) setErr(err error) { + if err == nil { + panic("non-nil value expected") + } + cmd.err = err +} + +func (cmd *baseCmd) setVal(val interface{}) { + if val == nil { + panic("non-nil value expected") + } + cmd.val = val +} + +func (cmd *baseCmd) parseReply(rd reader) (interface{}, error) { + return parseReply(rd) +} + +func (cmd *baseCmd) readTimeout() *time.Duration { + return cmd._readTimeout +} + +func (cmd *baseCmd) setReadTimeout(d time.Duration) { + cmd._readTimeout = &d +} + +func (cmd *baseCmd) writeTimeout() *time.Duration { + return cmd._writeTimeout +} + +func (cmd *baseCmd) setWriteTimeout(d time.Duration) { + cmd._writeTimeout = &d +} + +//------------------------------------------------------------------------------ + +type Cmd struct { + *baseCmd +} + +func NewCmd(args ...string) *Cmd { + return &Cmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *Cmd) Val() interface{} { + return cmd.val +} + +//------------------------------------------------------------------------------ + +type StatusCmd struct { + *baseCmd +} + +func NewStatusCmd(args ...string) *StatusCmd { + return &StatusCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StatusCmd) Val() string { + if cmd.val == nil { + return "" + } + return cmd.val.(string) +} + +//------------------------------------------------------------------------------ + +type IntCmd struct { + *baseCmd +} + +func NewIntCmd(args ...string) *IntCmd { + return &IntCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *IntCmd) Val() int64 { + if cmd.val == nil { + return 0 + } + return cmd.val.(int64) +} + +//------------------------------------------------------------------------------ + +type BoolCmd struct { + *baseCmd +} + +func NewBoolCmd(args ...string) *BoolCmd { + return &BoolCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *BoolCmd) parseReply(rd reader) (interface{}, error) { + v, err := parseReply(rd) + if err != nil { + return nil, err + } + return v.(int64) == 1, nil +} + +func (cmd *BoolCmd) Val() bool { + if cmd.val == nil { + return false + } + return cmd.val.(bool) +} + +//------------------------------------------------------------------------------ + +type StringCmd struct { + *baseCmd +} + +func NewStringCmd(args ...string) *StringCmd { + return &StringCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringCmd) Val() string { + if cmd.val == nil { + return "" + } + return cmd.val.(string) +} + +//------------------------------------------------------------------------------ + +type FloatCmd struct { + *baseCmd +} + +func NewFloatCmd(args ...string) *FloatCmd { + return &FloatCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *FloatCmd) parseReply(rd reader) (interface{}, error) { + v, err := parseReply(rd) + if err != nil { + return nil, err + } + return strconv.ParseFloat(v.(string), 64) +} + +func (cmd *FloatCmd) Val() float64 { + if cmd.val == nil { + return 0 + } + return cmd.val.(float64) +} + +//------------------------------------------------------------------------------ + +type SliceCmd struct { + *baseCmd +} + +func NewSliceCmd(args ...string) *SliceCmd { + return &SliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *SliceCmd) Val() []interface{} { + if cmd.val == nil { + return nil + } + return cmd.val.([]interface{}) +} + +//------------------------------------------------------------------------------ + +type StringSliceCmd struct { + *baseCmd +} + +func NewStringSliceCmd(args ...string) *StringSliceCmd { + return &StringSliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringSliceCmd) parseReply(rd reader) (interface{}, error) { + return parseStringSliceReply(rd) +} + +func (cmd *StringSliceCmd) Val() []string { + if cmd.val == nil { + return nil + } + return cmd.val.([]string) +} + +//------------------------------------------------------------------------------ + +type BoolSliceCmd struct { + *baseCmd +} + +func NewBoolSliceCmd(args ...string) *BoolSliceCmd { + return &BoolSliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *BoolSliceCmd) parseReply(rd reader) (interface{}, error) { + return parseBoolSliceReply(rd) +} + +func (cmd *BoolSliceCmd) Val() []bool { + if cmd.val == nil { + return nil + } + return cmd.val.([]bool) +} + +//------------------------------------------------------------------------------ + +type StringStringMapCmd struct { + *baseCmd +} + +func NewStringStringMapCmd(args ...string) *StringStringMapCmd { + return &StringStringMapCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringStringMapCmd) parseReply(rd reader) (interface{}, error) { + return parseStringStringMapReply(rd) +} + +func (cmd *StringStringMapCmd) Val() map[string]string { + if cmd.val == nil { + return nil + } + return cmd.val.(map[string]string) +} + +//------------------------------------------------------------------------------ + +type StringFloatMapCmd struct { + *baseCmd +} + +func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd { + return &StringFloatMapCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringFloatMapCmd) parseReply(rd reader) (interface{}, error) { + return parseStringFloatMapReply(rd) +} + +func (cmd *StringFloatMapCmd) Val() map[string]float64 { + if cmd.val == nil { + return nil + } + return cmd.val.(map[string]float64) +} diff --git a/v2/commands.go b/v2/commands.go index dcf9804d..fc7ae8ec 100644 --- a/v2/commands.go +++ b/v2/commands.go @@ -18,75 +18,75 @@ func readTimeout(sec int64) time.Duration { //------------------------------------------------------------------------------ -func (c *Client) Auth(password string) *StatusReq { - req := NewStatusReq("AUTH", password) +func (c *Client) Auth(password string) *StatusCmd { + req := NewStatusCmd("AUTH", password) c.Process(req) return req } -func (c *Client) Echo(message string) *StringReq { - req := NewStringReq("ECHO", message) +func (c *Client) Echo(message string) *StringCmd { + req := NewStringCmd("ECHO", message) c.Process(req) return req } -func (c *Client) Ping() *StatusReq { - req := NewStatusReq("PING") +func (c *Client) Ping() *StatusCmd { + req := NewStatusCmd("PING") c.Process(req) return req } -func (c *Client) Quit() *StatusReq { +func (c *Client) Quit() *StatusCmd { panic("not implemented") } -func (c *Client) Select(index int64) *StatusReq { - req := NewStatusReq("SELECT", strconv.FormatInt(index, 10)) +func (c *Client) Select(index int64) *StatusCmd { + req := NewStatusCmd("SELECT", strconv.FormatInt(index, 10)) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) Del(keys ...string) *IntReq { +func (c *Client) Del(keys ...string) *IntCmd { args := append([]string{"DEL"}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) Dump(key string) *StringReq { - req := NewStringReq("DUMP", key) +func (c *Client) Dump(key string) *StringCmd { + req := NewStringCmd("DUMP", key) c.Process(req) return req } -func (c *Client) Exists(key string) *BoolReq { - req := NewBoolReq("EXISTS", key) +func (c *Client) Exists(key string) *BoolCmd { + req := NewBoolCmd("EXISTS", key) c.Process(req) return req } -func (c *Client) Expire(key string, seconds int64) *BoolReq { - req := NewBoolReq("EXPIRE", key, strconv.FormatInt(seconds, 10)) +func (c *Client) Expire(key string, seconds int64) *BoolCmd { + req := NewBoolCmd("EXPIRE", key, strconv.FormatInt(seconds, 10)) c.Process(req) return req } -func (c *Client) ExpireAt(key string, timestamp int64) *BoolReq { - req := NewBoolReq("EXPIREAT", key, strconv.FormatInt(timestamp, 10)) +func (c *Client) ExpireAt(key string, timestamp int64) *BoolCmd { + req := NewBoolCmd("EXPIREAT", key, strconv.FormatInt(timestamp, 10)) c.Process(req) return req } -func (c *Client) Keys(pattern string) *StringSliceReq { - req := NewStringSliceReq("KEYS", pattern) +func (c *Client) Keys(pattern string) *StringSliceCmd { + req := NewStringSliceCmd("KEYS", pattern) c.Process(req) return req } -func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusReq { - req := NewStatusReq( +func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusCmd { + req := NewStatusCmd( "MIGRATE", host, port, @@ -99,77 +99,77 @@ func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusReq { return req } -func (c *Client) Move(key string, db int64) *BoolReq { - req := NewBoolReq("MOVE", key, strconv.FormatInt(db, 10)) +func (c *Client) Move(key string, db int64) *BoolCmd { + req := NewBoolCmd("MOVE", key, strconv.FormatInt(db, 10)) c.Process(req) return req } -func (c *Client) ObjectRefCount(keys ...string) *IntReq { +func (c *Client) ObjectRefCount(keys ...string) *IntCmd { args := append([]string{"OBJECT", "REFCOUNT"}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) ObjectEncoding(keys ...string) *StringReq { +func (c *Client) ObjectEncoding(keys ...string) *StringCmd { args := append([]string{"OBJECT", "ENCODING"}, keys...) - req := NewStringReq(args...) + req := NewStringCmd(args...) c.Process(req) return req } -func (c *Client) ObjectIdleTime(keys ...string) *IntReq { +func (c *Client) ObjectIdleTime(keys ...string) *IntCmd { args := append([]string{"OBJECT", "IDLETIME"}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) Persist(key string) *BoolReq { - req := NewBoolReq("PERSIST", key) +func (c *Client) Persist(key string) *BoolCmd { + req := NewBoolCmd("PERSIST", key) c.Process(req) return req } -func (c *Client) PExpire(key string, milliseconds int64) *BoolReq { - req := NewBoolReq("PEXPIRE", key, strconv.FormatInt(milliseconds, 10)) +func (c *Client) PExpire(key string, milliseconds int64) *BoolCmd { + req := NewBoolCmd("PEXPIRE", key, strconv.FormatInt(milliseconds, 10)) c.Process(req) return req } -func (c *Client) PExpireAt(key string, milliseconds int64) *BoolReq { - req := NewBoolReq("PEXPIREAT", key, strconv.FormatInt(milliseconds, 10)) +func (c *Client) PExpireAt(key string, milliseconds int64) *BoolCmd { + req := NewBoolCmd("PEXPIREAT", key, strconv.FormatInt(milliseconds, 10)) c.Process(req) return req } -func (c *Client) PTTL(key string) *IntReq { - req := NewIntReq("PTTL", key) +func (c *Client) PTTL(key string) *IntCmd { + req := NewIntCmd("PTTL", key) c.Process(req) return req } -func (c *Client) RandomKey() *StringReq { - req := NewStringReq("RANDOMKEY") +func (c *Client) RandomKey() *StringCmd { + req := NewStringCmd("RANDOMKEY") c.Process(req) return req } -func (c *Client) Rename(key, newkey string) *StatusReq { - req := NewStatusReq("RENAME", key, newkey) +func (c *Client) Rename(key, newkey string) *StatusCmd { + req := NewStatusCmd("RENAME", key, newkey) c.Process(req) return req } -func (c *Client) RenameNX(key, newkey string) *BoolReq { - req := NewBoolReq("RENAMENX", key, newkey) +func (c *Client) RenameNX(key, newkey string) *BoolCmd { + req := NewBoolCmd("RENAMENX", key, newkey) c.Process(req) return req } -func (c *Client) Restore(key string, ttl int64, value string) *StatusReq { - req := NewStatusReq( +func (c *Client) Restore(key string, ttl int64, value string) *StatusCmd { + req := NewStatusCmd( "RESTORE", key, strconv.FormatInt(ttl, 10), @@ -188,7 +188,7 @@ type Sort struct { Store string } -func (c *Client) Sort(key string, sort Sort) *StringSliceReq { +func (c *Client) Sort(key string, sort Sort) *StringSliceCmd { args := []string{"SORT", key} if sort.By != "" { args = append(args, sort.By) @@ -208,27 +208,27 @@ func (c *Client) Sort(key string, sort Sort) *StringSliceReq { if sort.Store != "" { args = append(args, "STORE", sort.Store) } - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) TTL(key string) *IntReq { - req := NewIntReq("TTL", key) +func (c *Client) TTL(key string) *IntCmd { + req := NewIntCmd("TTL", key) c.Process(req) return req } -func (c *Client) Type(key string) *StatusReq { - req := NewStatusReq("TYPE", key) +func (c *Client) Type(key string) *StatusCmd { + req := NewStatusCmd("TYPE", key) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) Append(key, value string) *IntReq { - req := NewIntReq("APPEND", key, value) +func (c *Client) Append(key, value string) *IntCmd { + req := NewIntCmd("APPEND", key, value) c.Process(req) return req } @@ -237,7 +237,7 @@ type BitCount struct { Start, End int64 } -func (c *Client) BitCount(key string, bitCount *BitCount) *IntReq { +func (c *Client) BitCount(key string, bitCount *BitCount) *IntCmd { args := []string{"BITCOUNT", key} if bitCount != nil { args = append( @@ -246,61 +246,61 @@ func (c *Client) BitCount(key string, bitCount *BitCount) *IntReq { strconv.FormatInt(bitCount.End, 10), ) } - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) bitOp(op, destKey string, keys ...string) *IntReq { +func (c *Client) bitOp(op, destKey string, keys ...string) *IntCmd { args := []string{"BITOP", op, destKey} args = append(args, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) BitOpAnd(destKey string, keys ...string) *IntReq { +func (c *Client) BitOpAnd(destKey string, keys ...string) *IntCmd { return c.bitOp("AND", destKey, keys...) } -func (c *Client) BitOpOr(destKey string, keys ...string) *IntReq { +func (c *Client) BitOpOr(destKey string, keys ...string) *IntCmd { return c.bitOp("OR", destKey, keys...) } -func (c *Client) BitOpXor(destKey string, keys ...string) *IntReq { +func (c *Client) BitOpXor(destKey string, keys ...string) *IntCmd { return c.bitOp("XOR", destKey, keys...) } -func (c *Client) BitOpNot(destKey string, key string) *IntReq { +func (c *Client) BitOpNot(destKey string, key string) *IntCmd { return c.bitOp("NOT", destKey, key) } -func (c *Client) Decr(key string) *IntReq { - req := NewIntReq("DECR", key) +func (c *Client) Decr(key string) *IntCmd { + req := NewIntCmd("DECR", key) c.Process(req) return req } -func (c *Client) DecrBy(key string, decrement int64) *IntReq { - req := NewIntReq("DECRBY", key, strconv.FormatInt(decrement, 10)) +func (c *Client) DecrBy(key string, decrement int64) *IntCmd { + req := NewIntCmd("DECRBY", key, strconv.FormatInt(decrement, 10)) c.Process(req) return req } -func (c *Client) Get(key string) *StringReq { - req := NewStringReq("GET", key) +func (c *Client) Get(key string) *StringCmd { + req := NewStringCmd("GET", key) c.Process(req) return req } -func (c *Client) GetBit(key string, offset int64) *IntReq { - req := NewIntReq("GETBIT", key, strconv.FormatInt(offset, 10)) +func (c *Client) GetBit(key string, offset int64) *IntCmd { + req := NewIntCmd("GETBIT", key, strconv.FormatInt(offset, 10)) c.Process(req) return req } -func (c *Client) GetRange(key string, start, end int64) *StringReq { - req := NewStringReq( +func (c *Client) GetRange(key string, start, end int64) *StringCmd { + req := NewStringCmd( "GETRANGE", key, strconv.FormatInt(start, 10), @@ -310,53 +310,53 @@ func (c *Client) GetRange(key string, start, end int64) *StringReq { return req } -func (c *Client) GetSet(key, value string) *StringReq { - req := NewStringReq("GETSET", key, value) +func (c *Client) GetSet(key, value string) *StringCmd { + req := NewStringCmd("GETSET", key, value) c.Process(req) return req } -func (c *Client) Incr(key string) *IntReq { - req := NewIntReq("INCR", key) +func (c *Client) Incr(key string) *IntCmd { + req := NewIntCmd("INCR", key) c.Process(req) return req } -func (c *Client) IncrBy(key string, value int64) *IntReq { - req := NewIntReq("INCRBY", key, strconv.FormatInt(value, 10)) +func (c *Client) IncrBy(key string, value int64) *IntCmd { + req := NewIntCmd("INCRBY", key, strconv.FormatInt(value, 10)) c.Process(req) return req } -func (c *Client) IncrByFloat(key string, value float64) *FloatReq { - req := NewFloatReq("INCRBYFLOAT", key, formatFloat(value)) +func (c *Client) IncrByFloat(key string, value float64) *FloatCmd { + req := NewFloatCmd("INCRBYFLOAT", key, formatFloat(value)) c.Process(req) return req } -func (c *Client) MGet(keys ...string) *IfaceSliceReq { +func (c *Client) MGet(keys ...string) *SliceCmd { args := append([]string{"MGET"}, keys...) - req := NewIfaceSliceReq(args...) + req := NewSliceCmd(args...) c.Process(req) return req } -func (c *Client) MSet(pairs ...string) *StatusReq { +func (c *Client) MSet(pairs ...string) *StatusCmd { args := append([]string{"MSET"}, pairs...) - req := NewStatusReq(args...) + req := NewStatusCmd(args...) c.Process(req) return req } -func (c *Client) MSetNX(pairs ...string) *BoolReq { +func (c *Client) MSetNX(pairs ...string) *BoolCmd { args := append([]string{"MSETNX"}, pairs...) - req := NewBoolReq(args...) + req := NewBoolCmd(args...) c.Process(req) return req } -func (c *Client) PSetEx(key string, milliseconds int64, value string) *StatusReq { - req := NewStatusReq( +func (c *Client) PSetEx(key string, milliseconds int64, value string) *StatusCmd { + req := NewStatusCmd( "PSETEX", key, strconv.FormatInt(milliseconds, 10), @@ -366,14 +366,14 @@ func (c *Client) PSetEx(key string, milliseconds int64, value string) *StatusReq return req } -func (c *Client) Set(key, value string) *StatusReq { - req := NewStatusReq("SET", key, value) +func (c *Client) Set(key, value string) *StatusCmd { + req := NewStatusCmd("SET", key, value) c.Process(req) return req } -func (c *Client) SetBit(key string, offset int64, value int) *IntReq { - req := NewIntReq( +func (c *Client) SetBit(key string, offset int64, value int) *IntCmd { + req := NewIntCmd( "SETBIT", key, strconv.FormatInt(offset, 10), @@ -383,141 +383,141 @@ func (c *Client) SetBit(key string, offset int64, value int) *IntReq { return req } -func (c *Client) SetEx(key string, seconds int64, value string) *StatusReq { - req := NewStatusReq("SETEX", key, strconv.FormatInt(seconds, 10), value) +func (c *Client) SetEx(key string, seconds int64, value string) *StatusCmd { + req := NewStatusCmd("SETEX", key, strconv.FormatInt(seconds, 10), value) c.Process(req) return req } -func (c *Client) SetNX(key, value string) *BoolReq { - req := NewBoolReq("SETNX", key, value) +func (c *Client) SetNX(key, value string) *BoolCmd { + req := NewBoolCmd("SETNX", key, value) c.Process(req) return req } -func (c *Client) SetRange(key string, offset int64, value string) *IntReq { - req := NewIntReq("SETRANGE", key, strconv.FormatInt(offset, 10), value) +func (c *Client) SetRange(key string, offset int64, value string) *IntCmd { + req := NewIntCmd("SETRANGE", key, strconv.FormatInt(offset, 10), value) c.Process(req) return req } -func (c *Client) StrLen(key string) *IntReq { - req := NewIntReq("STRLEN", key) +func (c *Client) StrLen(key string) *IntCmd { + req := NewIntCmd("STRLEN", key) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) HDel(key string, fields ...string) *IntReq { +func (c *Client) HDel(key string, fields ...string) *IntCmd { args := append([]string{"HDEL", key}, fields...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) HExists(key, field string) *BoolReq { - req := NewBoolReq("HEXISTS", key, field) +func (c *Client) HExists(key, field string) *BoolCmd { + req := NewBoolCmd("HEXISTS", key, field) c.Process(req) return req } -func (c *Client) HGet(key, field string) *StringReq { - req := NewStringReq("HGET", key, field) +func (c *Client) HGet(key, field string) *StringCmd { + req := NewStringCmd("HGET", key, field) c.Process(req) return req } -func (c *Client) HGetAll(key string) *StringSliceReq { - req := NewStringSliceReq("HGETALL", key) +func (c *Client) HGetAll(key string) *StringSliceCmd { + req := NewStringSliceCmd("HGETALL", key) c.Process(req) return req } -func (c *Client) HGetAllMap(key string) *StringStringMapReq { - req := NewStringStringMapReq("HGETALL", key) +func (c *Client) HGetAllMap(key string) *StringStringMapCmd { + req := NewStringStringMapCmd("HGETALL", key) c.Process(req) return req } -func (c *Client) HIncrBy(key, field string, incr int64) *IntReq { - req := NewIntReq("HINCRBY", key, field, strconv.FormatInt(incr, 10)) +func (c *Client) HIncrBy(key, field string, incr int64) *IntCmd { + req := NewIntCmd("HINCRBY", key, field, strconv.FormatInt(incr, 10)) c.Process(req) return req } -func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatReq { - req := NewFloatReq("HINCRBYFLOAT", key, field, formatFloat(incr)) +func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatCmd { + req := NewFloatCmd("HINCRBYFLOAT", key, field, formatFloat(incr)) c.Process(req) return req } -func (c *Client) HKeys(key string) *StringSliceReq { - req := NewStringSliceReq("HKEYS", key) +func (c *Client) HKeys(key string) *StringSliceCmd { + req := NewStringSliceCmd("HKEYS", key) c.Process(req) return req } -func (c *Client) HLen(key string) *IntReq { - req := NewIntReq("HLEN", key) +func (c *Client) HLen(key string) *IntCmd { + req := NewIntCmd("HLEN", key) c.Process(req) return req } -func (c *Client) HMGet(key string, fields ...string) *IfaceSliceReq { +func (c *Client) HMGet(key string, fields ...string) *SliceCmd { args := append([]string{"HMGET", key}, fields...) - req := NewIfaceSliceReq(args...) + req := NewSliceCmd(args...) c.Process(req) return req } -func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusReq { +func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusCmd { args := append([]string{"HMSET", key, field, value}, pairs...) - req := NewStatusReq(args...) + req := NewStatusCmd(args...) c.Process(req) return req } -func (c *Client) HSet(key, field, value string) *BoolReq { - req := NewBoolReq("HSET", key, field, value) +func (c *Client) HSet(key, field, value string) *BoolCmd { + req := NewBoolCmd("HSET", key, field, value) c.Process(req) return req } -func (c *Client) HSetNX(key, field, value string) *BoolReq { - req := NewBoolReq("HSETNX", key, field, value) +func (c *Client) HSetNX(key, field, value string) *BoolCmd { + req := NewBoolCmd("HSETNX", key, field, value) c.Process(req) return req } -func (c *Client) HVals(key string) *StringSliceReq { - req := NewStringSliceReq("HVALS", key) +func (c *Client) HVals(key string) *StringSliceCmd { + req := NewStringSliceCmd("HVALS", key) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceReq { +func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceCmd { args := append([]string{"BLPOP"}, keys...) args = append(args, strconv.FormatInt(timeout, 10)) - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) req.setReadTimeout(readTimeout(timeout)) c.Process(req) return req } -func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceReq { +func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceCmd { args := append([]string{"BRPOP"}, keys...) args = append(args, strconv.FormatInt(timeout, 10)) - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) req.setReadTimeout(readTimeout(timeout)) c.Process(req) return req } -func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringReq { - req := NewStringReq( +func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringCmd { + req := NewStringCmd( "BRPOPLPUSH", source, destination, @@ -528,45 +528,45 @@ func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringRe return req } -func (c *Client) LIndex(key string, index int64) *StringReq { - req := NewStringReq("LINDEX", key, strconv.FormatInt(index, 10)) +func (c *Client) LIndex(key string, index int64) *StringCmd { + req := NewStringCmd("LINDEX", key, strconv.FormatInt(index, 10)) c.Process(req) return req } -func (c *Client) LInsert(key, op, pivot, value string) *IntReq { - req := NewIntReq("LINSERT", key, op, pivot, value) +func (c *Client) LInsert(key, op, pivot, value string) *IntCmd { + req := NewIntCmd("LINSERT", key, op, pivot, value) c.Process(req) return req } -func (c *Client) LLen(key string) *IntReq { - req := NewIntReq("LLEN", key) +func (c *Client) LLen(key string) *IntCmd { + req := NewIntCmd("LLEN", key) c.Process(req) return req } -func (c *Client) LPop(key string) *StringReq { - req := NewStringReq("LPOP", key) +func (c *Client) LPop(key string) *StringCmd { + req := NewStringCmd("LPOP", key) c.Process(req) return req } -func (c *Client) LPush(key string, values ...string) *IntReq { +func (c *Client) LPush(key string, values ...string) *IntCmd { args := append([]string{"LPUSH", key}, values...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) LPushX(key, value string) *IntReq { - req := NewIntReq("LPUSHX", key, value) +func (c *Client) LPushX(key, value string) *IntCmd { + req := NewIntCmd("LPUSHX", key, value) c.Process(req) return req } -func (c *Client) LRange(key string, start, stop int64) *StringSliceReq { - req := NewStringSliceReq( +func (c *Client) LRange(key string, start, stop int64) *StringSliceCmd { + req := NewStringSliceCmd( "LRANGE", key, strconv.FormatInt(start, 10), @@ -576,20 +576,20 @@ func (c *Client) LRange(key string, start, stop int64) *StringSliceReq { return req } -func (c *Client) LRem(key string, count int64, value string) *IntReq { - req := NewIntReq("LREM", key, strconv.FormatInt(count, 10), value) +func (c *Client) LRem(key string, count int64, value string) *IntCmd { + req := NewIntCmd("LREM", key, strconv.FormatInt(count, 10), value) c.Process(req) return req } -func (c *Client) LSet(key string, index int64, value string) *StatusReq { - req := NewStatusReq("LSET", key, strconv.FormatInt(index, 10), value) +func (c *Client) LSet(key string, index int64, value string) *StatusCmd { + req := NewStatusCmd("LSET", key, strconv.FormatInt(index, 10), value) c.Process(req) return req } -func (c *Client) LTrim(key string, start, stop int64) *StatusReq { - req := NewStatusReq( +func (c *Client) LTrim(key string, start, stop int64) *StatusCmd { + req := NewStatusCmd( "LTRIM", key, strconv.FormatInt(start, 10), @@ -599,121 +599,121 @@ func (c *Client) LTrim(key string, start, stop int64) *StatusReq { return req } -func (c *Client) RPop(key string) *StringReq { - req := NewStringReq("RPOP", key) +func (c *Client) RPop(key string) *StringCmd { + req := NewStringCmd("RPOP", key) c.Process(req) return req } -func (c *Client) RPopLPush(source, destination string) *StringReq { - req := NewStringReq("RPOPLPUSH", source, destination) +func (c *Client) RPopLPush(source, destination string) *StringCmd { + req := NewStringCmd("RPOPLPUSH", source, destination) c.Process(req) return req } -func (c *Client) RPush(key string, values ...string) *IntReq { +func (c *Client) RPush(key string, values ...string) *IntCmd { args := append([]string{"RPUSH", key}, values...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) RPushX(key string, value string) *IntReq { - req := NewIntReq("RPUSHX", key, value) +func (c *Client) RPushX(key string, value string) *IntCmd { + req := NewIntCmd("RPUSHX", key, value) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) SAdd(key string, members ...string) *IntReq { +func (c *Client) SAdd(key string, members ...string) *IntCmd { args := append([]string{"SADD", key}, members...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) SCard(key string) *IntReq { - req := NewIntReq("SCARD", key) +func (c *Client) SCard(key string) *IntCmd { + req := NewIntCmd("SCARD", key) c.Process(req) return req } -func (c *Client) SDiff(keys ...string) *StringSliceReq { +func (c *Client) SDiff(keys ...string) *StringSliceCmd { args := append([]string{"SDIFF"}, keys...) - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) SDiffStore(destination string, keys ...string) *IntReq { +func (c *Client) SDiffStore(destination string, keys ...string) *IntCmd { args := append([]string{"SDIFFSTORE", destination}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) SInter(keys ...string) *StringSliceReq { +func (c *Client) SInter(keys ...string) *StringSliceCmd { args := append([]string{"SINTER"}, keys...) - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) SInterStore(destination string, keys ...string) *IntReq { +func (c *Client) SInterStore(destination string, keys ...string) *IntCmd { args := append([]string{"SINTERSTORE", destination}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) SIsMember(key, member string) *BoolReq { - req := NewBoolReq("SISMEMBER", key, member) +func (c *Client) SIsMember(key, member string) *BoolCmd { + req := NewBoolCmd("SISMEMBER", key, member) c.Process(req) return req } -func (c *Client) SMembers(key string) *StringSliceReq { - req := NewStringSliceReq("SMEMBERS", key) +func (c *Client) SMembers(key string) *StringSliceCmd { + req := NewStringSliceCmd("SMEMBERS", key) c.Process(req) return req } -func (c *Client) SMove(source, destination, member string) *BoolReq { - req := NewBoolReq("SMOVE", source, destination, member) +func (c *Client) SMove(source, destination, member string) *BoolCmd { + req := NewBoolCmd("SMOVE", source, destination, member) c.Process(req) return req } -func (c *Client) SPop(key string) *StringReq { - req := NewStringReq("SPOP", key) +func (c *Client) SPop(key string) *StringCmd { + req := NewStringCmd("SPOP", key) c.Process(req) return req } -func (c *Client) SRandMember(key string) *StringReq { - req := NewStringReq("SRANDMEMBER", key) +func (c *Client) SRandMember(key string) *StringCmd { + req := NewStringCmd("SRANDMEMBER", key) c.Process(req) return req } -func (c *Client) SRem(key string, members ...string) *IntReq { +func (c *Client) SRem(key string, members ...string) *IntCmd { args := append([]string{"SREM", key}, members...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) SUnion(keys ...string) *StringSliceReq { +func (c *Client) SUnion(keys ...string) *StringSliceCmd { args := append([]string{"SUNION"}, keys...) - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) SUnionStore(destination string, keys ...string) *IntReq { +func (c *Client) SUnionStore(destination string, keys ...string) *IntCmd { args := append([]string{"SUNIONSTORE", destination}, keys...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } @@ -730,30 +730,30 @@ type ZStore struct { Aggregate string } -func (c *Client) ZAdd(key string, members ...Z) *IntReq { +func (c *Client) ZAdd(key string, members ...Z) *IntCmd { args := []string{"ZADD", key} for _, m := range members { args = append(args, formatFloat(m.Score), m.Member) } - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) ZCard(key string) *IntReq { - req := NewIntReq("ZCARD", key) +func (c *Client) ZCard(key string) *IntCmd { + req := NewIntCmd("ZCARD", key) c.Process(req) return req } -func (c *Client) ZCount(key, min, max string) *IntReq { - req := NewIntReq("ZCOUNT", key, min, max) +func (c *Client) ZCount(key, min, max string) *IntCmd { + req := NewIntCmd("ZCOUNT", key, min, max) c.Process(req) return req } -func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatReq { - req := NewFloatReq("ZINCRBY", key, formatFloat(increment), member) +func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatCmd { + req := NewFloatCmd("ZINCRBY", key, formatFloat(increment), member) c.Process(req) return req } @@ -762,7 +762,7 @@ func (c *Client) ZInterStore( destination string, store ZStore, keys ...string, -) *IntReq { +) *IntCmd { args := []string{"ZINTERSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)} args = append(args, keys...) if len(store.Weights) > 0 { @@ -774,12 +774,12 @@ func (c *Client) ZInterStore( if store.Aggregate != "" { args = append(args, "AGGREGATE", store.Aggregate) } - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceReq { +func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd { args := []string{ "ZRANGE", key, @@ -789,20 +789,20 @@ func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringS if withScores { args = append(args, "WITHSCORES") } - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) ZRange(key string, start, stop int64) *StringSliceReq { +func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd { return c.zRange(key, start, stop, false) } -func (c *Client) ZRangeWithScores(key string, start, stop int64) *StringSliceReq { +func (c *Client) ZRangeWithScores(key string, start, stop int64) *StringSliceCmd { return c.zRange(key, start, stop, true) } -func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloatMapReq { +func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloatMapCmd { args := []string{ "ZRANGE", key, @@ -810,7 +810,7 @@ func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloat strconv.FormatInt(stop, 10), "WITHSCORES", } - req := NewStringFloatMapReq(args...) + req := NewStringFloatMapCmd(args...) c.Process(req) return req } @@ -820,7 +820,7 @@ func (c *Client) zRangeByScore( min, max string, withScores bool, offset, count int64, -) *StringSliceReq { +) *StringSliceCmd { args := []string{"ZRANGEBYSCORE", key, min, max} if withScores { args = append(args, "WITHSCORES") @@ -833,21 +833,21 @@ func (c *Client) zRangeByScore( strconv.FormatInt(count, 10), ) } - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) ZRangeByScore(key string, min, max string, offset, count int64) *StringSliceReq { +func (c *Client) ZRangeByScore(key string, min, max string, offset, count int64) *StringSliceCmd { return c.zRangeByScore(key, min, max, false, offset, count) } -func (c *Client) ZRangeByScoreWithScores(key, min, max string, offset, count int64) *StringSliceReq { +func (c *Client) ZRangeByScoreWithScores(key, min, max string, offset, count int64) *StringSliceCmd { return c.zRangeByScore(key, min, max, true, offset, count) } func (c *Client) ZRangeByScoreWithScoresMap( - key string, min, max string, offset, count int64) *StringFloatMapReq { + key string, min, max string, offset, count int64) *StringFloatMapCmd { args := []string{"ZRANGEBYSCORE", key, min, max, "WITHSCORES"} if offset != 0 || count != 0 { args = append( @@ -857,26 +857,26 @@ func (c *Client) ZRangeByScoreWithScoresMap( strconv.FormatInt(count, 10), ) } - req := NewStringFloatMapReq(args...) + req := NewStringFloatMapCmd(args...) c.Process(req) return req } -func (c *Client) ZRank(key, member string) *IntReq { - req := NewIntReq("ZRANK", key, member) +func (c *Client) ZRank(key, member string) *IntCmd { + req := NewIntCmd("ZRANK", key, member) c.Process(req) return req } -func (c *Client) ZRem(key string, members ...string) *IntReq { +func (c *Client) ZRem(key string, members ...string) *IntCmd { args := append([]string{"ZREM", key}, members...) - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } -func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntReq { - req := NewIntReq( +func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntCmd { + req := NewIntCmd( "ZREMRANGEBYRANK", key, strconv.FormatInt(start, 10), @@ -886,38 +886,38 @@ func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntReq { return req } -func (c *Client) ZRemRangeByScore(key, min, max string) *IntReq { - req := NewIntReq("ZREMRANGEBYSCORE", key, min, max) +func (c *Client) ZRemRangeByScore(key, min, max string) *IntCmd { + req := NewIntCmd("ZREMRANGEBYSCORE", key, min, max) c.Process(req) return req } -func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceReq { +func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd { args := []string{"ZREVRANGE", key, start, stop} if withScores { args = append(args, "WITHSCORES") } - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) ZRevRange(key, start, stop string) *StringSliceReq { +func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd { return c.zRevRange(key, start, stop, false) } -func (c *Client) ZRevRangeWithScores(key, start, stop string) *StringSliceReq { +func (c *Client) ZRevRangeWithScores(key, start, stop string) *StringSliceCmd { return c.zRevRange(key, start, stop, true) } -func (c *Client) ZRevRangeWithScoresMap(key, start, stop string) *StringFloatMapReq { +func (c *Client) ZRevRangeWithScoresMap(key, start, stop string) *StringFloatMapCmd { args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"} - req := NewStringFloatMapReq(args...) + req := NewStringFloatMapCmd(args...) c.Process(req) return req } -func (c *Client) zRevRangeByScore(key, start, stop string, withScores bool, offset, count int64) *StringSliceReq { +func (c *Client) zRevRangeByScore(key, start, stop string, withScores bool, offset, count int64) *StringSliceCmd { args := []string{"ZREVRANGEBYSCORE", key, start, stop} if withScores { args = append(args, "WITHSCORES") @@ -930,21 +930,21 @@ func (c *Client) zRevRangeByScore(key, start, stop string, withScores bool, offs strconv.FormatInt(count, 10), ) } - req := NewStringSliceReq(args...) + req := NewStringSliceCmd(args...) c.Process(req) return req } -func (c *Client) ZRevRangeByScore(key, start, stop string, offset, count int64) *StringSliceReq { +func (c *Client) ZRevRangeByScore(key, start, stop string, offset, count int64) *StringSliceCmd { return c.zRevRangeByScore(key, start, stop, false, offset, count) } -func (c *Client) ZRevRangeByScoreWithScores(key, start, stop string, offset, count int64) *StringSliceReq { +func (c *Client) ZRevRangeByScoreWithScores(key, start, stop string, offset, count int64) *StringSliceCmd { return c.zRevRangeByScore(key, start, stop, false, offset, count) } func (c *Client) ZRevRangeByScoreWithScoresMap( - key, start, stop string, offset, count int64) *StringFloatMapReq { + key, start, stop string, offset, count int64) *StringFloatMapCmd { args := []string{"ZREVRANGEBYSCORE", key, start, stop, "WITHSCORES"} if offset != 0 || count != 0 { args = append( @@ -954,19 +954,19 @@ func (c *Client) ZRevRangeByScoreWithScoresMap( strconv.FormatInt(count, 10), ) } - req := NewStringFloatMapReq(args...) + req := NewStringFloatMapCmd(args...) c.Process(req) return req } -func (c *Client) ZRevRank(key, member string) *IntReq { - req := NewIntReq("ZREVRANK", key, member) +func (c *Client) ZRevRank(key, member string) *IntCmd { + req := NewIntCmd("ZREVRANK", key, member) c.Process(req) return req } -func (c *Client) ZScore(key, member string) *FloatReq { - req := NewFloatReq("ZSCORE", key, member) +func (c *Client) ZScore(key, member string) *FloatCmd { + req := NewFloatCmd("ZSCORE", key, member) c.Process(req) return req } @@ -975,7 +975,7 @@ func (c *Client) ZUnionStore( destination string, store ZStore, keys ...string, -) *IntReq { +) *IntCmd { args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)} args = append(args, keys...) if len(store.Weights) > 0 { @@ -987,81 +987,81 @@ func (c *Client) ZUnionStore( if store.Aggregate != "" { args = append(args, "AGGREGATE", store.Aggregate) } - req := NewIntReq(args...) + req := NewIntCmd(args...) c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) BgRewriteAOF() *StatusReq { - req := NewStatusReq("BGREWRITEAOF") +func (c *Client) BgRewriteAOF() *StatusCmd { + req := NewStatusCmd("BGREWRITEAOF") c.Process(req) return req } -func (c *Client) BgSave() *StatusReq { - req := NewStatusReq("BGSAVE") +func (c *Client) BgSave() *StatusCmd { + req := NewStatusCmd("BGSAVE") c.Process(req) return req } -func (c *Client) ClientKill(ipPort string) *StatusReq { - req := NewStatusReq("CLIENT", "KILL", ipPort) +func (c *Client) ClientKill(ipPort string) *StatusCmd { + req := NewStatusCmd("CLIENT", "KILL", ipPort) c.Process(req) return req } -func (c *Client) ClientList() *StringReq { - req := NewStringReq("CLIENT", "LIST") +func (c *Client) ClientList() *StringCmd { + req := NewStringCmd("CLIENT", "LIST") c.Process(req) return req } -func (c *Client) ConfigGet(parameter string) *IfaceSliceReq { - req := NewIfaceSliceReq("CONFIG", "GET", parameter) +func (c *Client) ConfigGet(parameter string) *SliceCmd { + req := NewSliceCmd("CONFIG", "GET", parameter) c.Process(req) return req } -func (c *Client) ConfigResetStat() *StatusReq { - req := NewStatusReq("CONFIG", "RESETSTAT") +func (c *Client) ConfigResetStat() *StatusCmd { + req := NewStatusCmd("CONFIG", "RESETSTAT") c.Process(req) return req } -func (c *Client) ConfigSet(parameter, value string) *StatusReq { - req := NewStatusReq("CONFIG", "SET", parameter, value) +func (c *Client) ConfigSet(parameter, value string) *StatusCmd { + req := NewStatusCmd("CONFIG", "SET", parameter, value) c.Process(req) return req } -func (c *Client) DbSize() *IntReq { - req := NewIntReq("DBSIZE") +func (c *Client) DbSize() *IntCmd { + req := NewIntCmd("DBSIZE") c.Process(req) return req } -func (c *Client) FlushAll() *StatusReq { - req := NewStatusReq("FLUSHALL") +func (c *Client) FlushAll() *StatusCmd { + req := NewStatusCmd("FLUSHALL") c.Process(req) return req } -func (c *Client) FlushDb() *StatusReq { - req := NewStatusReq("FLUSHDB") +func (c *Client) FlushDb() *StatusCmd { + req := NewStatusCmd("FLUSHDB") c.Process(req) return req } -func (c *Client) Info() *StringReq { - req := NewStringReq("INFO") +func (c *Client) Info() *StringCmd { + req := NewStringCmd("INFO") c.Process(req) return req } -func (c *Client) LastSave() *IntReq { - req := NewIntReq("LASTSAVE") +func (c *Client) LastSave() *IntCmd { + req := NewIntCmd("LASTSAVE") c.Process(req) return req } @@ -1070,39 +1070,39 @@ func (c *Client) Monitor() { panic("not implemented") } -func (c *Client) Save() *StatusReq { - req := NewStatusReq("SAVE") +func (c *Client) Save() *StatusCmd { + req := NewStatusCmd("SAVE") c.Process(req) return req } -func (c *Client) shutdown(modifier string) *StatusReq { +func (c *Client) shutdown(modifier string) *StatusCmd { var args []string if modifier == "" { args = []string{"SHUTDOWN"} } else { args = []string{"SHUTDOWN", modifier} } - req := NewStatusReq(args...) + req := NewStatusCmd(args...) c.Process(req) c.Close() return req } -func (c *Client) Shutdown() *StatusReq { +func (c *Client) Shutdown() *StatusCmd { return c.shutdown("") } -func (c *Client) ShutdownSave() *StatusReq { +func (c *Client) ShutdownSave() *StatusCmd { return c.shutdown("SAVE") } -func (c *Client) ShutdownNoSave() *StatusReq { +func (c *Client) ShutdownNoSave() *StatusCmd { return c.shutdown("NOSAVE") } -func (c *Client) SlaveOf(host, port string) *StatusReq { - req := NewStatusReq("SLAVEOF", host, port) +func (c *Client) SlaveOf(host, port string) *StatusCmd { + req := NewStatusCmd("SLAVEOF", host, port) c.Process(req) return req } @@ -1115,53 +1115,53 @@ func (c *Client) Sync() { panic("not implemented") } -func (c *Client) Time() *StringSliceReq { - req := NewStringSliceReq("TIME") +func (c *Client) Time() *StringSliceCmd { + req := NewStringSliceCmd("TIME") c.Process(req) return req } //------------------------------------------------------------------------------ -func (c *Client) Eval(script string, keys []string, args []string) *IfaceReq { +func (c *Client) Eval(script string, keys []string, args []string) *Cmd { reqArgs := []string{"EVAL", script, strconv.FormatInt(int64(len(keys)), 10)} reqArgs = append(reqArgs, keys...) reqArgs = append(reqArgs, args...) - req := NewIfaceReq(reqArgs...) + req := NewCmd(reqArgs...) c.Process(req) return req } -func (c *Client) EvalSha(sha1 string, keys []string, args []string) *IfaceReq { +func (c *Client) EvalSha(sha1 string, keys []string, args []string) *Cmd { reqArgs := []string{"EVALSHA", sha1, strconv.FormatInt(int64(len(keys)), 10)} reqArgs = append(reqArgs, keys...) reqArgs = append(reqArgs, args...) - req := NewIfaceReq(reqArgs...) + req := NewCmd(reqArgs...) c.Process(req) return req } -func (c *Client) ScriptExists(scripts ...string) *BoolSliceReq { +func (c *Client) ScriptExists(scripts ...string) *BoolSliceCmd { args := append([]string{"SCRIPT", "EXISTS"}, scripts...) - req := NewBoolSliceReq(args...) + req := NewBoolSliceCmd(args...) c.Process(req) return req } -func (c *Client) ScriptFlush() *StatusReq { - req := NewStatusReq("SCRIPT", "FLUSH") +func (c *Client) ScriptFlush() *StatusCmd { + req := NewStatusCmd("SCRIPT", "FLUSH") c.Process(req) return req } -func (c *Client) ScriptKill() *StatusReq { - req := NewStatusReq("SCRIPT", "KILL") +func (c *Client) ScriptKill() *StatusCmd { + req := NewStatusCmd("SCRIPT", "KILL") c.Process(req) return req } -func (c *Client) ScriptLoad(script string) *StringReq { - req := NewStringReq("SCRIPT", "LOAD", script) +func (c *Client) ScriptLoad(script string) *StringCmd { + req := NewStringCmd("SCRIPT", "LOAD", script) c.Process(req) return req } diff --git a/v2/example_test.go b/v2/example_test.go index e2a36588..0ac075dd 100644 --- a/v2/example_test.go +++ b/v2/example_test.go @@ -53,13 +53,13 @@ func ExamplePipeline() { }) defer client.Close() - var set *redis.StatusReq - var get *redis.StringReq - reqs, err := client.Pipelined(func(c *redis.Pipeline) { + var set *redis.StatusCmd + var get *redis.StringCmd + cmds, err := client.Pipelined(func(c *redis.Pipeline) { set = c.Set("key1", "hello1") get = c.Get("key2") }) - fmt.Println(err, reqs) + fmt.Println(err, cmds) fmt.Println(set) fmt.Println(get) // Output: (nil) [SET key1 hello1: OK GET key2: (nil)] @@ -67,7 +67,7 @@ func ExamplePipeline() { // GET key2: (nil) } -func incr(tx *redis.Multi) ([]redis.Req, error) { +func incr(tx *redis.Multi) ([]redis.Cmder, error) { get := tx.Get("key") if err := get.Err(); err != nil && err != redis.Nil { return nil, err @@ -75,14 +75,14 @@ func incr(tx *redis.Multi) ([]redis.Req, error) { val, _ := strconv.ParseInt(get.Val(), 10, 64) - reqs, err := tx.Exec(func() { + cmds, err := tx.Exec(func() { tx.Set("key", strconv.FormatInt(val+1, 10)) }) // Transaction failed. Repeat. if err == redis.Nil { return incr(tx) } - return reqs, err + return cmds, err } func ExampleTransaction() { @@ -99,8 +99,8 @@ func ExampleTransaction() { watch := tx.Watch("key") _ = watch.Err() - reqs, err := incr(tx) - fmt.Println(err, reqs) + cmds, err := incr(tx) + fmt.Println(err, cmds) // Output: [SET key 1: OK] } @@ -130,10 +130,10 @@ func ExamplePubSub() { // &{mychannel hello} } -func Get(client *redis.Client, key string) *redis.StringReq { - req := redis.NewStringReq("GET", key) - client.Process(req) - return req +func Get(client *redis.Client, key string) *redis.StringCmd { + cmd := redis.NewStringCmd("GET", key) + client.Process(cmd) + return cmd } func ExampleCustomCommand() { diff --git a/v2/multi.go b/v2/multi.go index 737a3a93..926fd8fb 100644 --- a/v2/multi.go +++ b/v2/multi.go @@ -28,38 +28,38 @@ func (c *Multi) Close() error { return c.Client.Close() } -func (c *Multi) Watch(keys ...string) *StatusReq { +func (c *Multi) Watch(keys ...string) *StatusCmd { args := append([]string{"WATCH"}, keys...) - req := NewStatusReq(args...) - c.Process(req) - return req + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd } -func (c *Multi) Unwatch(keys ...string) *StatusReq { +func (c *Multi) Unwatch(keys ...string) *StatusCmd { args := append([]string{"UNWATCH"}, keys...) - req := NewStatusReq(args...) - c.Process(req) - return req + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd } func (c *Multi) Discard() error { - if c.reqs == nil { + if c.cmds == nil { return errDiscard } - c.reqs = c.reqs[:1] + c.cmds = c.cmds[:1] return nil } -func (c *Multi) Exec(f func()) ([]Req, error) { - c.reqs = []Req{NewStatusReq("MULTI")} +func (c *Multi) Exec(f func()) ([]Cmder, error) { + c.cmds = []Cmder{NewStatusCmd("MULTI")} f() - c.reqs = append(c.reqs, NewIfaceSliceReq("EXEC")) + c.cmds = append(c.cmds, NewSliceCmd("EXEC")) - reqs := c.reqs - c.reqs = nil + cmds := c.cmds + c.cmds = nil - if len(reqs) == 2 { - return []Req{}, nil + if len(cmds) == 2 { + return []Cmder{}, nil } cn, err := c.conn() @@ -68,30 +68,30 @@ func (c *Multi) Exec(f func()) ([]Req, error) { } // Synchronize writes and reads to the connection using mutex. - err = c.execReqs(reqs, cn) + err = c.execCmds(cmds, cn) if err != nil { c.removeConn(cn) return nil, err } c.putConn(cn) - return reqs[1 : len(reqs)-1], nil + return cmds[1 : len(cmds)-1], nil } -func (c *Multi) execReqs(reqs []Req, cn *conn) error { - err := c.writeReq(cn, reqs...) +func (c *Multi) execCmds(cmds []Cmder, cn *conn) error { + err := c.writeCmd(cn, cmds...) if err != nil { return err } - statusReq := NewStatusReq() + statusCmd := NewStatusCmd() - // Omit last request (EXEC). - reqsLen := len(reqs) - 1 + // Omit last cmduest (EXEC). + cmdsLen := len(cmds) - 1 // Parse queued replies. - for i := 0; i < reqsLen; i++ { - _, err = statusReq.ParseReply(cn.Rd) + for i := 0; i < cmdsLen; i++ { + _, err = statusCmd.parseReply(cn.Rd) if err != nil { return err } @@ -110,14 +110,14 @@ func (c *Multi) execReqs(reqs []Req, cn *conn) error { } // Parse replies. - // Loop starts from 1 to omit first request (MULTI). - for i := 1; i < reqsLen; i++ { - req := reqs[i] - val, err := req.ParseReply(cn.Rd) + // Loop starts from 1 to omit first cmduest (MULTI). + for i := 1; i < cmdsLen; i++ { + cmd := cmds[i] + val, err := cmd.parseReply(cn.Rd) if err != nil { - req.SetErr(err) + cmd.setErr(err) } else { - req.SetVal(val) + cmd.setVal(val) } } diff --git a/v2/parser.go b/v2/parser.go index 802380b7..43095d25 100644 --- a/v2/parser.go +++ b/v2/parser.go @@ -29,7 +29,7 @@ var ( //------------------------------------------------------------------------------ -func appendReq(buf []byte, args []string) []byte { +func appendCmd(buf []byte, args []string) []byte { buf = append(buf, '*') buf = strconv.AppendUint(buf, uint64(len(args)), 10) buf = append(buf, '\r', '\n') diff --git a/v2/pipeline.go b/v2/pipeline.go index 6acec649..4ae0c080 100644 --- a/v2/pipeline.go +++ b/v2/pipeline.go @@ -12,18 +12,18 @@ func (c *Client) Pipeline() *Pipeline { opt: c.opt, connPool: c.connPool, - reqs: make([]Req, 0), + cmds: make([]Cmder, 0), }, }, } } -func (c *Client) Pipelined(f func(*Pipeline)) ([]Req, error) { +func (c *Client) Pipelined(f func(*Pipeline)) ([]Cmder, error) { pc := c.Pipeline() f(pc) - reqs, err := pc.Exec() + cmds, err := pc.Exec() pc.Close() - return reqs, err + return cmds, err } func (c *Pipeline) Close() error { @@ -31,55 +31,55 @@ func (c *Pipeline) Close() error { } func (c *Pipeline) Discard() error { - c.reqs = c.reqs[:0] + c.cmds = c.cmds[:0] return nil } // Always returns list of commands and error of the first failed // command if any. -func (c *Pipeline) Exec() ([]Req, error) { - reqs := c.reqs - c.reqs = make([]Req, 0) +func (c *Pipeline) Exec() ([]Cmder, error) { + cmds := c.cmds + c.cmds = make([]Cmder, 0) - if len(reqs) == 0 { - return []Req{}, nil + if len(cmds) == 0 { + return []Cmder{}, nil } cn, err := c.conn() if err != nil { - return reqs, err + return cmds, err } - if err := c.execReqs(reqs, cn); err != nil { + if err := c.execCmds(cmds, cn); err != nil { c.freeConn(cn, err) - return reqs, err + return cmds, err } c.putConn(cn) - return reqs, nil + return cmds, nil } -func (c *Pipeline) execReqs(reqs []Req, cn *conn) error { - err := c.writeReq(cn, reqs...) +func (c *Pipeline) execCmds(cmds []Cmder, cn *conn) error { + err := c.writeCmd(cn, cmds...) if err != nil { - for _, req := range reqs { - req.SetErr(err) + for _, cmd := range cmds { + cmd.setErr(err) } return err } - var firstReqErr error - for _, req := range reqs { - val, err := req.ParseReply(cn.Rd) + var firstCmdErr error + for _, cmd := range cmds { + val, err := cmd.parseReply(cn.Rd) if err != nil { - req.SetErr(err) - if err != nil { - firstReqErr = err + cmd.setErr(err) + if firstCmdErr == nil { + firstCmdErr = err } } else { - req.SetVal(val) + cmd.setVal(val) } } - return firstReqErr + return firstCmdErr } diff --git a/v2/pubsub.go b/v2/pubsub.go index 290fe99b..e886d19d 100644 --- a/v2/pubsub.go +++ b/v2/pubsub.go @@ -18,8 +18,8 @@ func (c *Client) PubSub() *PubSub { } } -func (c *Client) Publish(channel, message string) *IntReq { - req := NewIntReq("PUBLISH", channel, message) +func (c *Client) Publish(channel, message string) *IntCmd { + req := NewIntCmd("PUBLISH", channel, message) c.Process(req) return req } @@ -52,7 +52,7 @@ func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) { } cn.readTimeout = timeout - replyIface, err := NewIfaceSliceReq().ParseReply(cn.Rd) + replyIface, err := NewSliceCmd().parseReply(cn.Rd) if err != nil { return nil, err } @@ -91,8 +91,8 @@ func (c *PubSub) subscribe(cmd string, channels ...string) error { } args := append([]string{cmd}, channels...) - req := NewIfaceSliceReq(args...) - return c.writeReq(cn, req) + req := NewSliceCmd(args...) + return c.writeCmd(cn, req) } func (c *PubSub) Subscribe(channels ...string) error { @@ -110,8 +110,8 @@ func (c *PubSub) unsubscribe(cmd string, channels ...string) error { } args := append([]string{cmd}, channels...) - req := NewIfaceSliceReq(args...) - return c.writeReq(cn, req) + req := NewSliceCmd(args...) + return c.writeCmd(cn, req) } func (c *PubSub) Unsubscribe(channels ...string) error { diff --git a/v2/redis.go b/v2/redis.go index f259b1cd..072070dd 100644 --- a/v2/redis.go +++ b/v2/redis.go @@ -18,13 +18,13 @@ type baseClient struct { opt *Options - reqs []Req + cmds []Cmder } -func (c *baseClient) writeReq(cn *conn, reqs ...Req) error { +func (c *baseClient) writeCmd(cn *conn, cmds ...Cmder) error { buf := make([]byte, 0, 1000) - for _, req := range reqs { - buf = appendReq(buf, req.Args()) + for _, cmd := range cmds { + buf = appendCmd(buf, cmd.args()) } _, err := cn.Write(buf) @@ -93,46 +93,46 @@ func (c *baseClient) putConn(cn *conn) { } } -func (c *baseClient) Process(req Req) { - if c.reqs == nil { - c.run(req) +func (c *baseClient) Process(cmd Cmder) { + if c.cmds == nil { + c.run(cmd) } else { - c.reqs = append(c.reqs, req) + c.cmds = append(c.cmds, cmd) } } -func (c *baseClient) run(req Req) { +func (c *baseClient) run(cmd Cmder) { cn, err := c.conn() if err != nil { - req.SetErr(err) + cmd.setErr(err) return } cn.writeTimeout = c.opt.WriteTimeout - if timeout := req.writeTimeout(); timeout != nil { + if timeout := cmd.writeTimeout(); timeout != nil { cn.writeTimeout = *timeout } cn.readTimeout = c.opt.ReadTimeout - if timeout := req.readTimeout(); timeout != nil { + if timeout := cmd.readTimeout(); timeout != nil { cn.readTimeout = *timeout } - if err := c.writeReq(cn, req); err != nil { + if err := c.writeCmd(cn, cmd); err != nil { c.removeConn(cn) - req.SetErr(err) + cmd.setErr(err) return } - val, err := req.ParseReply(cn.Rd) + val, err := cmd.parseReply(cn.Rd) if err != nil { c.freeConn(cn, err) - req.SetErr(err) + cmd.setErr(err) return } c.putConn(cn) - req.SetVal(val) + cmd.setVal(val) } func (c *baseClient) Close() error { diff --git a/v2/redis_test.go b/v2/redis_test.go index d70e70d6..4bc83c2c 100644 --- a/v2/redis_test.go +++ b/v2/redis_test.go @@ -155,9 +155,9 @@ func (t *RedisConnectorTest) TestUnixConnector(c *C) { // c.Assert(err, IsNil) // ping := pipeline.Ping() -// reqs, err := pipeline.RunQueued() +// cmds, err := pipeline.RunQueued() // c.Assert(err, IsNil) -// c.Assert(reqs, HasLen, 1) +// c.Assert(cmds, HasLen, 1) // c.Assert(ping.Err(), IsNil) // c.Assert(ping.Val(), Equals, "PONG") @@ -181,12 +181,12 @@ func (t *RedisConnectorTest) TestUnixConnector(c *C) { // multi, err := t.client.MultiClient() // c.Assert(err, IsNil) -// var ping *redis.StatusReq -// reqs, err := multi.Exec(func() { +// var ping *redis.StatusCmd +// cmds, err := multi.Exec(func() { // ping = multi.Ping() // }) // c.Assert(err, IsNil) -// c.Assert(reqs, HasLen, 1) +// c.Assert(cmds, HasLen, 1) // c.Assert(ping.Err(), IsNil) // c.Assert(ping.Val(), Equals, "PONG") @@ -257,7 +257,7 @@ func (t *RedisTest) resetRedis(c *C) { //------------------------------------------------------------------------------ -func (t *RedisTest) TestReqStringMethod(c *C) { +func (t *RedisTest) TestCmdStringMethod(c *C) { set := t.client.Set("foo", "bar") c.Assert(set.String(), Equals, "SET foo bar: OK") @@ -265,7 +265,7 @@ func (t *RedisTest) TestReqStringMethod(c *C) { c.Assert(get.String(), Equals, "GET foo: bar") } -func (t *RedisTest) TestReqStringMethodError(c *C) { +func (t *RedisTest) TestCmdStringMethodError(c *C) { get2 := t.client.Get("key_does_not_exists") c.Assert(get2.String(), Equals, "GET key_does_not_exists: (nil)") } @@ -2403,9 +2403,9 @@ func (t *RedisTest) TestPipeline(c *C) { incr := pipeline.Incr("key3") getNil := pipeline.Get("key4") - reqs, err := pipeline.Exec() + cmds, err := pipeline.Exec() c.Assert(err, Equals, redis.Nil) - c.Assert(reqs, HasLen, 4) + c.Assert(cmds, HasLen, 4) c.Assert(set.Err(), IsNil) c.Assert(set.Val(), Equals, "OK") @@ -2428,18 +2428,18 @@ func (t *RedisTest) TestPipelineDiscardQueued(c *C) { pipeline.Get("key") pipeline.Discard() - reqs, err := pipeline.Exec() + cmds, err := pipeline.Exec() c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 0) + c.Assert(cmds, HasLen, 0) } func (t *RedisTest) TestPipelineFunc(c *C) { - var get *redis.StringReq - reqs, err := t.client.Pipelined(func(c *redis.Pipeline) { + var get *redis.StringCmd + cmds, err := t.client.Pipelined(func(c *redis.Pipeline) { get = c.Get("foo") }) c.Assert(err, Equals, redis.Nil) - c.Assert(reqs, HasLen, 1) + c.Assert(cmds, HasLen, 1) c.Assert(get.Err(), Equals, redis.Nil) c.Assert(get.Val(), Equals, "") } @@ -2460,9 +2460,9 @@ func (t *RedisTest) TestPipelineRunQueuedOnEmptyQueue(c *C) { c.Assert(pipeline.Close(), IsNil) }() - reqs, err := pipeline.Exec() + cmds, err := pipeline.Exec() c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 0) + c.Assert(cmds, HasLen, 0) } func (t *RedisTest) TestPipelineIncrFromGoroutines(c *C) { @@ -2481,12 +2481,12 @@ func (t *RedisTest) TestPipelineIncrFromGoroutines(c *C) { } wg.Wait() - reqs, err := pipeline.Exec() + cmds, err := pipeline.Exec() c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 20000) - for _, req := range reqs { - if req.Err() != nil { - c.Errorf("got %v, expected nil", req.Err()) + c.Assert(cmds, HasLen, 20000) + for _, cmd := range cmds { + if cmd.Err() != nil { + c.Errorf("got %v, expected nil", cmd.Err()) } } @@ -2511,9 +2511,9 @@ func (t *RedisTest) TestPipelineEchoFromGoroutines(c *C) { echo1 := pipeline.Echo(msg1) echo2 := pipeline.Echo(msg2) - reqs, err := pipeline.Exec() + cmds, err := pipeline.Exec() c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 2) + c.Assert(cmds, HasLen, 2) c.Assert(echo1.Err(), IsNil) c.Assert(echo1.Val(), Equals, msg1) @@ -2536,15 +2536,15 @@ func (t *RedisTest) TestMultiExec(c *C) { }() var ( - set *redis.StatusReq - get *redis.StringReq + set *redis.StatusCmd + get *redis.StringCmd ) - reqs, err := multi.Exec(func() { + cmds, err := multi.Exec(func() { set = multi.Set("key", "hello") get = multi.Get("key") }) c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 2) + c.Assert(cmds, HasLen, 2) c.Assert(set.Err(), IsNil) c.Assert(set.Val(), Equals, "OK") @@ -2559,13 +2559,13 @@ func (t *RedisTest) TestMultiExecDiscard(c *C) { c.Assert(multi.Close(), IsNil) }() - reqs, err := multi.Exec(func() { + cmds, err := multi.Exec(func() { multi.Set("key1", "hello1") multi.Discard() multi.Set("key2", "hello2") }) c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 1) + c.Assert(cmds, HasLen, 1) get := t.client.Get("key1") c.Assert(get.Err(), Equals, redis.Nil) @@ -2582,9 +2582,9 @@ func (t *RedisTest) TestMultiExecEmpty(c *C) { c.Assert(multi.Close(), IsNil) }() - reqs, err := multi.Exec(func() {}) + cmds, err := multi.Exec(func() {}) c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 0) + c.Assert(cmds, HasLen, 0) ping := multi.Ping() c.Check(ping.Err(), IsNil) @@ -2597,9 +2597,9 @@ func (t *RedisTest) TestMultiExecOnEmptyQueue(c *C) { c.Assert(multi.Close(), IsNil) }() - reqs, err := multi.Exec(func() {}) + cmds, err := multi.Exec(func() {}) c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 0) + c.Assert(cmds, HasLen, 0) } func (t *RedisTest) TestMultiExecIncr(c *C) { @@ -2608,16 +2608,16 @@ func (t *RedisTest) TestMultiExecIncr(c *C) { c.Assert(multi.Close(), IsNil) }() - reqs, err := multi.Exec(func() { + cmds, err := multi.Exec(func() { for i := int64(0); i < 20000; i++ { multi.Incr("key") } }) c.Assert(err, IsNil) - c.Assert(reqs, HasLen, 20000) - for _, req := range reqs { - if req.Err() != nil { - c.Errorf("got %v, expected nil", req.Err()) + c.Assert(cmds, HasLen, 20000) + for _, cmd := range cmds { + if cmd.Err() != nil { + c.Errorf("got %v, expected nil", cmd.Err()) } } @@ -2626,7 +2626,7 @@ func (t *RedisTest) TestMultiExecIncr(c *C) { c.Assert(get.Val(), Equals, "20000") } -func (t *RedisTest) transactionalIncr(c *C) ([]redis.Req, error) { +func (t *RedisTest) transactionalIncr(c *C) ([]redis.Cmder, error) { multi := t.client.Multi() defer func() { c.Assert(multi.Close(), IsNil) @@ -2643,13 +2643,13 @@ func (t *RedisTest) transactionalIncr(c *C) ([]redis.Req, error) { v, err := strconv.ParseInt(get.Val(), 10, 64) c.Assert(err, IsNil) - reqs, err := multi.Exec(func() { + cmds, err := multi.Exec(func() { multi.Set("key", strconv.FormatInt(v+1, 10)) }) if err == redis.Nil { return t.transactionalIncr(c) } - return reqs, err + return cmds, err } func (t *RedisTest) TestWatchUnwatch(c *C) { @@ -2661,10 +2661,10 @@ func (t *RedisTest) TestWatchUnwatch(c *C) { for i := 0; i < 1000; i++ { wg.Add(1) go func() { - reqs, err := t.transactionalIncr(c) - c.Assert(reqs, HasLen, 1) + cmds, err := t.transactionalIncr(c) + c.Assert(cmds, HasLen, 1) c.Assert(err, IsNil) - c.Assert(reqs[0].Err(), IsNil) + c.Assert(cmds[0].Err(), IsNil) wg.Done() }() } diff --git a/v2/req.go b/v2/req.go deleted file mode 100644 index c9229e30..00000000 --- a/v2/req.go +++ /dev/null @@ -1,337 +0,0 @@ -package redis - -import ( - "fmt" - "strconv" - "strings" - "time" -) - -type Req interface { - Args() []string - ParseReply(reader) (interface{}, error) - SetErr(error) - Err() error - SetVal(interface{}) - IfaceVal() interface{} - - writeTimeout() *time.Duration - readTimeout() *time.Duration -} - -//------------------------------------------------------------------------------ - -type baseReq struct { - args []string - - val interface{} - err error - - _writeTimeout, _readTimeout *time.Duration -} - -func newBaseReq(args ...string) *baseReq { - return &baseReq{ - args: args, - } -} - -func (r *baseReq) Args() []string { - return r.args -} - -func (r *baseReq) SetErr(err error) { - if err == nil { - panic("non-nil value expected") - } - r.err = err -} - -func (r *baseReq) Err() error { - if r.err != nil { - return r.err - } - if r.val == nil { - return errValNotSet - } - return nil -} - -func (r *baseReq) SetVal(val interface{}) { - if val == nil { - panic("non-nil value expected") - } - r.val = val -} - -func (r *baseReq) IfaceVal() interface{} { - return r.val -} - -func (r *baseReq) ParseReply(rd reader) (interface{}, error) { - return parseReply(rd) -} - -func (r *baseReq) String() string { - args := strings.Join(r.args, " ") - if r.err != nil { - return args + ": " + r.err.Error() - } else if r.val != nil { - return args + ": " + fmt.Sprint(r.val) - } - return args -} - -func (r *baseReq) readTimeout() *time.Duration { - return r._readTimeout -} - -func (r *baseReq) setReadTimeout(d time.Duration) { - r._readTimeout = &d -} - -func (r *baseReq) writeTimeout() *time.Duration { - return r._writeTimeout -} - -func (r *baseReq) setWriteTimeout(d time.Duration) { - r._writeTimeout = &d -} - -//------------------------------------------------------------------------------ - -type IfaceReq struct { - *baseReq -} - -func NewIfaceReq(args ...string) *IfaceReq { - return &IfaceReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *IfaceReq) Val() interface{} { - return r.val -} - -//------------------------------------------------------------------------------ - -type StatusReq struct { - *baseReq -} - -func NewStatusReq(args ...string) *StatusReq { - return &StatusReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *StatusReq) Val() string { - if r.val == nil { - return "" - } - return r.val.(string) -} - -//------------------------------------------------------------------------------ - -type IntReq struct { - *baseReq -} - -func NewIntReq(args ...string) *IntReq { - return &IntReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *IntReq) Val() int64 { - if r.val == nil { - return 0 - } - return r.val.(int64) -} - -//------------------------------------------------------------------------------ - -type BoolReq struct { - *baseReq -} - -func NewBoolReq(args ...string) *BoolReq { - return &BoolReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *BoolReq) ParseReply(rd reader) (interface{}, error) { - v, err := parseReply(rd) - if err != nil { - return nil, err - } - return v.(int64) == 1, nil -} - -func (r *BoolReq) Val() bool { - if r.val == nil { - return false - } - return r.val.(bool) -} - -//------------------------------------------------------------------------------ - -type StringReq struct { - *baseReq -} - -func NewStringReq(args ...string) *StringReq { - return &StringReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *StringReq) Val() string { - if r.val == nil { - return "" - } - return r.val.(string) -} - -//------------------------------------------------------------------------------ - -type FloatReq struct { - *baseReq -} - -func NewFloatReq(args ...string) *FloatReq { - return &FloatReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *FloatReq) ParseReply(rd reader) (interface{}, error) { - v, err := parseReply(rd) - if err != nil { - return nil, err - } - return strconv.ParseFloat(v.(string), 64) -} - -func (r *FloatReq) Val() float64 { - if r.val == nil { - return 0 - } - return r.val.(float64) -} - -//------------------------------------------------------------------------------ - -type IfaceSliceReq struct { - *baseReq -} - -func NewIfaceSliceReq(args ...string) *IfaceSliceReq { - return &IfaceSliceReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *IfaceSliceReq) Val() []interface{} { - if r.val == nil { - return nil - } - return r.val.([]interface{}) -} - -//------------------------------------------------------------------------------ - -type StringSliceReq struct { - *baseReq -} - -func NewStringSliceReq(args ...string) *StringSliceReq { - return &StringSliceReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *StringSliceReq) ParseReply(rd reader) (interface{}, error) { - return parseStringSliceReply(rd) -} - -func (r *StringSliceReq) Val() []string { - if r.val == nil { - return nil - } - return r.val.([]string) -} - -//------------------------------------------------------------------------------ - -type BoolSliceReq struct { - *baseReq -} - -func NewBoolSliceReq(args ...string) *BoolSliceReq { - return &BoolSliceReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *BoolSliceReq) ParseReply(rd reader) (interface{}, error) { - return parseBoolSliceReply(rd) -} - -func (r *BoolSliceReq) Val() []bool { - if r.val == nil { - return nil - } - return r.val.([]bool) -} - -//------------------------------------------------------------------------------ - -type StringStringMapReq struct { - *baseReq -} - -func NewStringStringMapReq(args ...string) *StringStringMapReq { - return &StringStringMapReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *StringStringMapReq) ParseReply(rd reader) (interface{}, error) { - return parseStringStringMapReply(rd) -} - -func (r *StringStringMapReq) Val() map[string]string { - if r.val == nil { - return nil - } - return r.val.(map[string]string) -} - -//------------------------------------------------------------------------------ - -type StringFloatMapReq struct { - *baseReq -} - -func NewStringFloatMapReq(args ...string) *StringFloatMapReq { - return &StringFloatMapReq{ - baseReq: newBaseReq(args...), - } -} - -func (r *StringFloatMapReq) ParseReply(rd reader) (interface{}, error) { - return parseStringFloatMapReply(rd) -} - -func (r *StringFloatMapReq) Val() map[string]float64 { - if r.val == nil { - return nil - } - return r.val.(map[string]float64) -} diff --git a/v2/req_test.go b/v2/req_test.go deleted file mode 100644 index d7588a31..00000000 --- a/v2/req_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package redis_test - -import ( - "github.com/vmihailenco/bufio" - . "launchpad.net/gocheck" - - "github.com/vmihailenco/redis" -) - -//------------------------------------------------------------------------------ - -type LineReader struct { - line []byte -} - -func NewLineReader(line []byte) *LineReader { - return &LineReader{line: line} -} - -func (r *LineReader) Read(buf []byte) (int, error) { - return copy(buf, r.line), nil -} - -//------------------------------------------------------------------------------ - -type RequestTest struct{} - -var _ = Suite(&RequestTest{}) - -//------------------------------------------------------------------------------ - -func (t *RequestTest) SetUpTest(c *C) {} - -func (t *RequestTest) TearDownTest(c *C) {} - -//------------------------------------------------------------------------------ - -func (t *RequestTest) benchmarkReq(c *C, reqString string, req redis.Req, checker Checker, expected interface{}) { - c.StopTimer() - - lineReader := NewLineReader([]byte(reqString)) - rd := bufio.NewReaderSize(lineReader, 1024) - - for i := 0; i < 10; i++ { - vIface, err := req.ParseReply(rd) - c.Check(err, IsNil) - c.Check(vIface, checker, expected) - req.SetVal(vIface) - } - - c.StartTimer() - - for i := 0; i < c.N; i++ { - v, _ := req.ParseReply(rd) - req.SetVal(v) - } -} - -func (t *RequestTest) BenchmarkStatusReq(c *C) { - t.benchmarkReq(c, "+OK\r\n", redis.NewStatusReq(), Equals, "OK") -} - -func (t *RequestTest) BenchmarkIntReq(c *C) { - t.benchmarkReq(c, ":1\r\n", redis.NewIntReq(), Equals, int64(1)) -} - -func (t *RequestTest) BenchmarkStringReq(c *C) { - t.benchmarkReq(c, "$5\r\nhello\r\n", redis.NewStringReq(), Equals, "hello") -} - -func (t *RequestTest) BenchmarkFloatReq(c *C) { - t.benchmarkReq(c, "$5\r\n1.111\r\n", redis.NewFloatReq(), Equals, 1.111) -} - -func (t *RequestTest) BenchmarkStringSliceReq(c *C) { - t.benchmarkReq( - c, - "*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n", - redis.NewStringSliceReq(), - DeepEquals, - []string{"hello", "hello"}, - ) -} - -func (t *RequestTest) BenchmarkIfaceSliceReq(c *C) { - t.benchmarkReq( - c, - "*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n", - redis.NewIfaceSliceReq(), - DeepEquals, - []interface{}{"hello", "hello"}, - ) -} diff --git a/v2/script.go b/v2/script.go index 6284a236..ed77abf2 100644 --- a/v2/script.go +++ b/v2/script.go @@ -20,23 +20,23 @@ func NewScript(src string) *Script { } } -func (s *Script) Load(c *Client) *StringReq { +func (s *Script) Load(c *Client) *StringCmd { return c.ScriptLoad(s.src) } -func (s *Script) Exists(c *Client) *BoolSliceReq { +func (s *Script) Exists(c *Client) *BoolSliceCmd { return c.ScriptExists(s.src) } -func (s *Script) Eval(c *Client, keys []string, args []string) *IfaceReq { +func (s *Script) Eval(c *Client, keys []string, args []string) *Cmd { return c.Eval(s.src, keys, args) } -func (s *Script) EvalSha(c *Client, keys []string, args []string) *IfaceReq { +func (s *Script) EvalSha(c *Client, keys []string, args []string) *Cmd { return c.EvalSha(s.hash, keys, args) } -func (s *Script) Run(c *Client, keys []string, args []string) *IfaceReq { +func (s *Script) Run(c *Client, keys []string, args []string) *Cmd { r := s.EvalSha(c, keys, args) if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") { return s.Eval(c, keys, args)