mirror of https://github.com/go-redis/redis.git
Add Client.Do
This commit is contained in:
parent
e0dc0beeb5
commit
d5fc79d4ee
|
@ -5,6 +5,7 @@
|
|||
- Added Options.MinIdleConns.
|
||||
- Added Options.MaxConnAge.
|
||||
- PoolStats.FreeConns is renamed to PoolStats.IdleConns.
|
||||
- Add Client.Do to simplify creating custom commands.
|
||||
|
||||
## v6.13
|
||||
|
||||
|
|
11
cluster.go
11
cluster.go
|
@ -898,6 +898,13 @@ func (c *ClusterClient) Close() error {
|
|||
return c.nodes.Close()
|
||||
}
|
||||
|
||||
// Do creates a Cmd from the args and processes the cmd.
|
||||
func (c *ClusterClient) Do(args ...interface{}) *Cmd {
|
||||
cmd := NewCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *ClusterClient) WrapProcess(
|
||||
fn func(oldProcess func(Cmder) error) func(Cmder) error,
|
||||
) {
|
||||
|
@ -1242,7 +1249,7 @@ func (c *ClusterClient) defaultProcessPipeline(cmds []Cmder) error {
|
|||
cmdsMap = failedCmds
|
||||
}
|
||||
|
||||
return firstCmdsErr(cmds)
|
||||
return cmdsFirstErr(cmds)
|
||||
}
|
||||
|
||||
func (c *ClusterClient) mapCmdsByNode(cmds []Cmder) (map[*clusterNode][]Cmder, error) {
|
||||
|
@ -1424,7 +1431,7 @@ func (c *ClusterClient) defaultProcessTxPipeline(cmds []Cmder) error {
|
|||
}
|
||||
}
|
||||
|
||||
return firstCmdsErr(cmds)
|
||||
return cmdsFirstErr(cmds)
|
||||
}
|
||||
|
||||
func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) map[int][]Cmder {
|
||||
|
|
77
command.go
77
command.go
|
@ -25,7 +25,7 @@ type Cmder interface {
|
|||
readTimeout() *time.Duration
|
||||
|
||||
Err() error
|
||||
fmt.Stringer
|
||||
// fmt.Stringer
|
||||
}
|
||||
|
||||
func setCmdsErr(cmds []Cmder, e error) {
|
||||
|
@ -36,7 +36,7 @@ func setCmdsErr(cmds []Cmder, e error) {
|
|||
}
|
||||
}
|
||||
|
||||
func firstCmdsErr(cmds []Cmder) error {
|
||||
func cmdsFirstErr(cmds []Cmder) error {
|
||||
for _, cmd := range cmds {
|
||||
if err := cmd.Err(); err != nil {
|
||||
return err
|
||||
|
@ -167,8 +167,77 @@ func (cmd *Cmd) Result() (interface{}, error) {
|
|||
return cmd.val, cmd.err
|
||||
}
|
||||
|
||||
func (cmd *Cmd) String() string {
|
||||
return cmdString(cmd, cmd.val)
|
||||
func (cmd *Cmd) String() (string, error) {
|
||||
if cmd.err != nil {
|
||||
return "", cmd.err
|
||||
}
|
||||
switch val := cmd.val.(type) {
|
||||
case string:
|
||||
return val, nil
|
||||
default:
|
||||
err := fmt.Errorf("redis: unexpected type=%T for String", val)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Cmd) Int64() (int64, error) {
|
||||
if cmd.err != nil {
|
||||
return 0, cmd.err
|
||||
}
|
||||
switch val := cmd.val.(type) {
|
||||
case int64:
|
||||
return val, nil
|
||||
case string:
|
||||
return strconv.ParseInt(val, 10, 64)
|
||||
default:
|
||||
err := fmt.Errorf("redis: unexpected type=%T for Int64", val)
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Cmd) Uint64() (uint64, error) {
|
||||
if cmd.err != nil {
|
||||
return 0, cmd.err
|
||||
}
|
||||
switch val := cmd.val.(type) {
|
||||
case int64:
|
||||
return uint64(val), nil
|
||||
case string:
|
||||
return strconv.ParseUint(val, 10, 64)
|
||||
default:
|
||||
err := fmt.Errorf("redis: unexpected type=%T for Uint64", val)
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Cmd) Float64() (float64, error) {
|
||||
if cmd.err != nil {
|
||||
return 0, cmd.err
|
||||
}
|
||||
switch val := cmd.val.(type) {
|
||||
case int64:
|
||||
return float64(val), nil
|
||||
case string:
|
||||
return strconv.ParseFloat(val, 64)
|
||||
default:
|
||||
err := fmt.Errorf("redis: unexpected type=%T for Float64", val)
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Cmd) Bool() (bool, error) {
|
||||
if cmd.err != nil {
|
||||
return false, cmd.err
|
||||
}
|
||||
switch val := cmd.val.(type) {
|
||||
case int64:
|
||||
return val != 0, nil
|
||||
case string:
|
||||
return strconv.ParseBool(val)
|
||||
default:
|
||||
err := fmt.Errorf("redis: unexpected type=%T for Bool", val)
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Cmd) readReply(cn *pool.Conn) error {
|
||||
|
|
|
@ -420,6 +420,12 @@ func Example_customCommand() {
|
|||
// Output: "" redis: nil
|
||||
}
|
||||
|
||||
func Example_customCommand2() {
|
||||
v, err := client.Do("get", "key_does_not_exist").String()
|
||||
fmt.Printf("%q %s", v, err)
|
||||
// Output: "" redis: nil
|
||||
}
|
||||
|
||||
func ExampleScanIterator() {
|
||||
iter := client.Scan(0, "", 0).Iterator()
|
||||
for iter.Next() {
|
||||
|
|
13
redis.go
13
redis.go
|
@ -123,8 +123,17 @@ func (c *baseClient) initConn(cn *pool.Conn) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Do creates a Cmd from the args and processes the cmd.
|
||||
func (c *baseClient) Do(args ...interface{}) *Cmd {
|
||||
cmd := NewCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// WrapProcess wraps function that processes Redis commands.
|
||||
func (c *baseClient) WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error) {
|
||||
func (c *baseClient) WrapProcess(
|
||||
fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error,
|
||||
) {
|
||||
c.process = fn(c.process)
|
||||
}
|
||||
|
||||
|
@ -243,7 +252,7 @@ func (c *baseClient) generalProcessPipeline(cmds []Cmder, p pipelineProcessor) e
|
|||
break
|
||||
}
|
||||
}
|
||||
return firstCmdsErr(cmds)
|
||||
return cmdsFirstErr(cmds)
|
||||
}
|
||||
|
||||
func (c *baseClient) pipelineProcessCmds(cn *pool.Conn, cmds []Cmder) (bool, error) {
|
||||
|
|
13
ring.go
13
ring.go
|
@ -516,7 +516,16 @@ func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
|
|||
return c.shards.GetByKey(firstKey)
|
||||
}
|
||||
|
||||
func (c *Ring) WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error) {
|
||||
// Do creates a Cmd from the args and processes the cmd.
|
||||
func (c *Ring) Do(args ...interface{}) *Cmd {
|
||||
cmd := NewCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *Ring) WrapProcess(
|
||||
fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error,
|
||||
) {
|
||||
c.ForEachShard(func(c *Client) error {
|
||||
c.WrapProcess(fn)
|
||||
return nil
|
||||
|
@ -602,7 +611,7 @@ func (c *Ring) defaultProcessPipeline(cmds []Cmder) error {
|
|||
cmdsMap = failedCmdsMap
|
||||
}
|
||||
|
||||
return firstCmdsErr(cmds)
|
||||
return cmdsFirstErr(cmds)
|
||||
}
|
||||
|
||||
func (c *Ring) TxPipeline() Pipeliner {
|
||||
|
|
|
@ -92,7 +92,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
|
|||
},
|
||||
}
|
||||
c.baseClient.init()
|
||||
c.setProcessor(c.Process)
|
||||
c.cmdable.setProcessor(c.Process)
|
||||
|
||||
return &c
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue