123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875 |
- package redis
-
- import (
- "fmt"
- "net"
- "strconv"
- "strings"
- "time"
-
- "github.com/go-redis/redis/internal"
- "github.com/go-redis/redis/internal/proto"
- )
-
- type Cmder interface {
- Name() string
- Args() []interface{}
- stringArg(int) string
-
- readReply(rd *proto.Reader) error
- setErr(error)
-
- readTimeout() *time.Duration
-
- Err() error
- }
-
- func setCmdsErr(cmds []Cmder, e error) {
- for _, cmd := range cmds {
- if cmd.Err() == nil {
- cmd.setErr(e)
- }
- }
- }
-
- func cmdsFirstErr(cmds []Cmder) error {
- for _, cmd := range cmds {
- if err := cmd.Err(); err != nil {
- return err
- }
- }
- return nil
- }
-
- func writeCmd(wr *proto.Writer, cmds ...Cmder) error {
- for _, cmd := range cmds {
- err := wr.WriteArgs(cmd.Args())
- if err != nil {
- return err
- }
- }
- return nil
- }
-
- func cmdString(cmd Cmder, val interface{}) string {
- var ss []string
- for _, arg := range cmd.Args() {
- ss = append(ss, fmt.Sprint(arg))
- }
- s := strings.Join(ss, " ")
- if err := cmd.Err(); err != nil {
- return s + ": " + err.Error()
- }
- if val != nil {
- switch vv := val.(type) {
- case []byte:
- return s + ": " + string(vv)
- default:
- return s + ": " + fmt.Sprint(val)
- }
- }
- return s
-
- }
-
- func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
- switch cmd.Name() {
- case "eval", "evalsha":
- if cmd.stringArg(2) != "0" {
- return 3
- }
-
- return 0
- case "publish":
- return 1
- }
- if info == nil {
- return 0
- }
- return int(info.FirstKeyPos)
- }
-
- //------------------------------------------------------------------------------
-
- type baseCmd struct {
- _args []interface{}
- err error
-
- _readTimeout *time.Duration
- }
-
- var _ Cmder = (*Cmd)(nil)
-
- func (cmd *baseCmd) Err() error {
- return cmd.err
- }
-
- func (cmd *baseCmd) Args() []interface{} {
- return cmd._args
- }
-
- func (cmd *baseCmd) stringArg(pos int) string {
- if pos < 0 || pos >= len(cmd._args) {
- return ""
- }
- s, _ := cmd._args[pos].(string)
- return s
- }
-
- func (cmd *baseCmd) Name() string {
- if len(cmd._args) > 0 {
- // Cmd name must be lower cased.
- s := internal.ToLower(cmd.stringArg(0))
- cmd._args[0] = s
- return s
- }
- return ""
- }
-
- func (cmd *baseCmd) readTimeout() *time.Duration {
- return cmd._readTimeout
- }
-
- func (cmd *baseCmd) setReadTimeout(d time.Duration) {
- cmd._readTimeout = &d
- }
-
- func (cmd *baseCmd) setErr(e error) {
- cmd.err = e
- }
-
- //------------------------------------------------------------------------------
-
- type Cmd struct {
- baseCmd
-
- val interface{}
- }
-
- func NewCmd(args ...interface{}) *Cmd {
- return &Cmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *Cmd) Val() interface{} {
- return cmd.val
- }
-
- func (cmd *Cmd) Result() (interface{}, error) {
- return cmd.val, cmd.err
- }
-
- 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) Int() (int, error) {
- if cmd.err != nil {
- return 0, cmd.err
- }
- switch val := cmd.val.(type) {
- case int64:
- return int(val), nil
- case string:
- return strconv.Atoi(val)
- default:
- err := fmt.Errorf("redis: unexpected type=%T for Int", val)
- return 0, 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(rd *proto.Reader) error {
- cmd.val, cmd.err = rd.ReadReply(sliceParser)
- return cmd.err
- }
-
- // Implements proto.MultiBulkParse
- func sliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- vals := make([]interface{}, 0, n)
- for i := int64(0); i < n; i++ {
- v, err := rd.ReadReply(sliceParser)
- if err != nil {
- if err == Nil {
- vals = append(vals, nil)
- continue
- }
- if err, ok := err.(proto.RedisError); ok {
- vals = append(vals, err)
- continue
- }
- return nil, err
- }
-
- switch v := v.(type) {
- case string:
- vals = append(vals, v)
- default:
- vals = append(vals, v)
- }
- }
- return vals, nil
- }
-
- //------------------------------------------------------------------------------
-
- type SliceCmd struct {
- baseCmd
-
- val []interface{}
- }
-
- var _ Cmder = (*SliceCmd)(nil)
-
- func NewSliceCmd(args ...interface{}) *SliceCmd {
- return &SliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *SliceCmd) Val() []interface{} {
- return cmd.val
- }
-
- func (cmd *SliceCmd) Result() ([]interface{}, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *SliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *SliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(sliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]interface{})
- return nil
- }
-
- //------------------------------------------------------------------------------
-
- type StatusCmd struct {
- baseCmd
-
- val string
- }
-
- var _ Cmder = (*StatusCmd)(nil)
-
- func NewStatusCmd(args ...interface{}) *StatusCmd {
- return &StatusCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StatusCmd) Val() string {
- return cmd.val
- }
-
- func (cmd *StatusCmd) Result() (string, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *StatusCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StatusCmd) readReply(rd *proto.Reader) error {
- cmd.val, cmd.err = rd.ReadString()
- return cmd.err
- }
-
- //------------------------------------------------------------------------------
-
- type IntCmd struct {
- baseCmd
-
- val int64
- }
-
- var _ Cmder = (*IntCmd)(nil)
-
- func NewIntCmd(args ...interface{}) *IntCmd {
- return &IntCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *IntCmd) Val() int64 {
- return cmd.val
- }
-
- func (cmd *IntCmd) Result() (int64, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *IntCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *IntCmd) readReply(rd *proto.Reader) error {
- cmd.val, cmd.err = rd.ReadIntReply()
- return cmd.err
- }
-
- //------------------------------------------------------------------------------
-
- type DurationCmd struct {
- baseCmd
-
- val time.Duration
- precision time.Duration
- }
-
- var _ Cmder = (*DurationCmd)(nil)
-
- func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd {
- return &DurationCmd{
- baseCmd: baseCmd{_args: args},
- precision: precision,
- }
- }
-
- func (cmd *DurationCmd) Val() time.Duration {
- return cmd.val
- }
-
- func (cmd *DurationCmd) Result() (time.Duration, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *DurationCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *DurationCmd) readReply(rd *proto.Reader) error {
- var n int64
- n, cmd.err = rd.ReadIntReply()
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = time.Duration(n) * cmd.precision
- return nil
- }
-
- //------------------------------------------------------------------------------
-
- type TimeCmd struct {
- baseCmd
-
- val time.Time
- }
-
- var _ Cmder = (*TimeCmd)(nil)
-
- func NewTimeCmd(args ...interface{}) *TimeCmd {
- return &TimeCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *TimeCmd) Val() time.Time {
- return cmd.val
- }
-
- func (cmd *TimeCmd) Result() (time.Time, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *TimeCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *TimeCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(timeParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.(time.Time)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func timeParser(rd *proto.Reader, n int64) (interface{}, error) {
- if n != 2 {
- return nil, fmt.Errorf("got %d elements, expected 2", n)
- }
-
- sec, err := rd.ReadInt()
- if err != nil {
- return nil, err
- }
-
- microsec, err := rd.ReadInt()
- if err != nil {
- return nil, err
- }
-
- return time.Unix(sec, microsec*1000), nil
- }
-
- //------------------------------------------------------------------------------
-
- type BoolCmd struct {
- baseCmd
-
- val bool
- }
-
- var _ Cmder = (*BoolCmd)(nil)
-
- func NewBoolCmd(args ...interface{}) *BoolCmd {
- return &BoolCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *BoolCmd) Val() bool {
- return cmd.val
- }
-
- func (cmd *BoolCmd) Result() (bool, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *BoolCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *BoolCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadReply(nil)
- // `SET key value NX` returns nil when key already exists. But
- // `SETNX key value` returns bool (0/1). So convert nil to bool.
- // TODO: is this okay?
- if cmd.err == Nil {
- cmd.val = false
- cmd.err = nil
- return nil
- }
- if cmd.err != nil {
- return cmd.err
- }
- switch v := v.(type) {
- case int64:
- cmd.val = v == 1
- return nil
- case string:
- cmd.val = v == "OK"
- return nil
- default:
- cmd.err = fmt.Errorf("got %T, wanted int64 or string", v)
- return cmd.err
- }
- }
-
- //------------------------------------------------------------------------------
-
- type StringCmd struct {
- baseCmd
-
- val string
- }
-
- var _ Cmder = (*StringCmd)(nil)
-
- func NewStringCmd(args ...interface{}) *StringCmd {
- return &StringCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StringCmd) Val() string {
- return cmd.val
- }
-
- func (cmd *StringCmd) Result() (string, error) {
- return cmd.Val(), cmd.err
- }
-
- func (cmd *StringCmd) Bytes() ([]byte, error) {
- return []byte(cmd.val), cmd.err
- }
-
- func (cmd *StringCmd) Int() (int, error) {
- if cmd.err != nil {
- return 0, cmd.err
- }
- return strconv.Atoi(cmd.Val())
- }
-
- func (cmd *StringCmd) Int64() (int64, error) {
- if cmd.err != nil {
- return 0, cmd.err
- }
- return strconv.ParseInt(cmd.Val(), 10, 64)
- }
-
- func (cmd *StringCmd) Uint64() (uint64, error) {
- if cmd.err != nil {
- return 0, cmd.err
- }
- return strconv.ParseUint(cmd.Val(), 10, 64)
- }
-
- func (cmd *StringCmd) Float64() (float64, error) {
- if cmd.err != nil {
- return 0, cmd.err
- }
- return strconv.ParseFloat(cmd.Val(), 64)
- }
-
- func (cmd *StringCmd) Scan(val interface{}) error {
- if cmd.err != nil {
- return cmd.err
- }
- return proto.Scan([]byte(cmd.val), val)
- }
-
- func (cmd *StringCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StringCmd) readReply(rd *proto.Reader) error {
- cmd.val, cmd.err = rd.ReadString()
- return cmd.err
- }
-
- //------------------------------------------------------------------------------
-
- type FloatCmd struct {
- baseCmd
-
- val float64
- }
-
- var _ Cmder = (*FloatCmd)(nil)
-
- func NewFloatCmd(args ...interface{}) *FloatCmd {
- return &FloatCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *FloatCmd) Val() float64 {
- return cmd.val
- }
-
- func (cmd *FloatCmd) Result() (float64, error) {
- return cmd.Val(), cmd.Err()
- }
-
- func (cmd *FloatCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *FloatCmd) readReply(rd *proto.Reader) error {
- cmd.val, cmd.err = rd.ReadFloatReply()
- return cmd.err
- }
-
- //------------------------------------------------------------------------------
-
- type StringSliceCmd struct {
- baseCmd
-
- val []string
- }
-
- var _ Cmder = (*StringSliceCmd)(nil)
-
- func NewStringSliceCmd(args ...interface{}) *StringSliceCmd {
- return &StringSliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StringSliceCmd) Val() []string {
- return cmd.val
- }
-
- func (cmd *StringSliceCmd) Result() ([]string, error) {
- return cmd.Val(), cmd.Err()
- }
-
- func (cmd *StringSliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StringSliceCmd) ScanSlice(container interface{}) error {
- return proto.ScanSlice(cmd.Val(), container)
- }
-
- func (cmd *StringSliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(stringSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]string)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func stringSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- ss := make([]string, 0, n)
- for i := int64(0); i < n; i++ {
- s, err := rd.ReadString()
- if err == Nil {
- ss = append(ss, "")
- } else if err != nil {
- return nil, err
- } else {
- ss = append(ss, s)
- }
- }
- return ss, nil
- }
-
- //------------------------------------------------------------------------------
-
- type BoolSliceCmd struct {
- baseCmd
-
- val []bool
- }
-
- var _ Cmder = (*BoolSliceCmd)(nil)
-
- func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd {
- return &BoolSliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *BoolSliceCmd) Val() []bool {
- return cmd.val
- }
-
- func (cmd *BoolSliceCmd) Result() ([]bool, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *BoolSliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *BoolSliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(boolSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]bool)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func boolSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- bools := make([]bool, 0, n)
- for i := int64(0); i < n; i++ {
- n, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- bools = append(bools, n == 1)
- }
- return bools, nil
- }
-
- //------------------------------------------------------------------------------
-
- type StringStringMapCmd struct {
- baseCmd
-
- val map[string]string
- }
-
- var _ Cmder = (*StringStringMapCmd)(nil)
-
- func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd {
- return &StringStringMapCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StringStringMapCmd) Val() map[string]string {
- return cmd.val
- }
-
- func (cmd *StringStringMapCmd) Result() (map[string]string, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *StringStringMapCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StringStringMapCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(stringStringMapParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.(map[string]string)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func stringStringMapParser(rd *proto.Reader, n int64) (interface{}, error) {
- m := make(map[string]string, n/2)
- for i := int64(0); i < n; i += 2 {
- key, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- value, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- m[key] = value
- }
- return m, nil
- }
-
- //------------------------------------------------------------------------------
-
- type StringIntMapCmd struct {
- baseCmd
-
- val map[string]int64
- }
-
- var _ Cmder = (*StringIntMapCmd)(nil)
-
- func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd {
- return &StringIntMapCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StringIntMapCmd) Val() map[string]int64 {
- return cmd.val
- }
-
- func (cmd *StringIntMapCmd) Result() (map[string]int64, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *StringIntMapCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StringIntMapCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(stringIntMapParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.(map[string]int64)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func stringIntMapParser(rd *proto.Reader, n int64) (interface{}, error) {
- m := make(map[string]int64, n/2)
- for i := int64(0); i < n; i += 2 {
- key, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- n, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
-
- m[key] = n
- }
- return m, nil
- }
-
- //------------------------------------------------------------------------------
-
- type StringStructMapCmd struct {
- baseCmd
-
- val map[string]struct{}
- }
-
- var _ Cmder = (*StringStructMapCmd)(nil)
-
- func NewStringStructMapCmd(args ...interface{}) *StringStructMapCmd {
- return &StringStructMapCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *StringStructMapCmd) Val() map[string]struct{} {
- return cmd.val
- }
-
- func (cmd *StringStructMapCmd) Result() (map[string]struct{}, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *StringStructMapCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *StringStructMapCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(stringStructMapParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.(map[string]struct{})
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func stringStructMapParser(rd *proto.Reader, n int64) (interface{}, error) {
- m := make(map[string]struct{}, n)
- for i := int64(0); i < n; i++ {
- key, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- m[key] = struct{}{}
- }
- return m, nil
- }
-
- //------------------------------------------------------------------------------
-
- type XMessage struct {
- ID string
- Values map[string]interface{}
- }
-
- type XMessageSliceCmd struct {
- baseCmd
-
- val []XMessage
- }
-
- var _ Cmder = (*XMessageSliceCmd)(nil)
-
- func NewXMessageSliceCmd(args ...interface{}) *XMessageSliceCmd {
- return &XMessageSliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *XMessageSliceCmd) Val() []XMessage {
- return cmd.val
- }
-
- func (cmd *XMessageSliceCmd) Result() ([]XMessage, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *XMessageSliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *XMessageSliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(xMessageSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]XMessage)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func xMessageSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- msgs := make([]XMessage, 0, n)
- for i := int64(0); i < n; i++ {
- _, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
- id, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- v, err := rd.ReadArrayReply(stringInterfaceMapParser)
- if err != nil {
- return nil, err
- }
-
- msgs = append(msgs, XMessage{
- ID: id,
- Values: v.(map[string]interface{}),
- })
- return nil, nil
- })
- if err != nil {
- return nil, err
- }
- }
- return msgs, nil
- }
-
- // Implements proto.MultiBulkParse
- func stringInterfaceMapParser(rd *proto.Reader, n int64) (interface{}, error) {
- m := make(map[string]interface{}, n/2)
- for i := int64(0); i < n; i += 2 {
- key, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- value, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- m[key] = value
- }
- return m, nil
- }
-
- //------------------------------------------------------------------------------
-
- type XStream struct {
- Stream string
- Messages []XMessage
- }
-
- type XStreamSliceCmd struct {
- baseCmd
-
- val []XStream
- }
-
- var _ Cmder = (*XStreamSliceCmd)(nil)
-
- func NewXStreamSliceCmd(args ...interface{}) *XStreamSliceCmd {
- return &XStreamSliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *XStreamSliceCmd) Val() []XStream {
- return cmd.val
- }
-
- func (cmd *XStreamSliceCmd) Result() ([]XStream, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *XStreamSliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *XStreamSliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(xStreamSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]XStream)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func xStreamSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- ret := make([]XStream, 0, n)
- for i := int64(0); i < n; i++ {
- _, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
- if n != 2 {
- return nil, fmt.Errorf("got %d, wanted 2", n)
- }
-
- stream, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- v, err := rd.ReadArrayReply(xMessageSliceParser)
- if err != nil {
- return nil, err
- }
-
- ret = append(ret, XStream{
- Stream: stream,
- Messages: v.([]XMessage),
- })
- return nil, nil
- })
- if err != nil {
- return nil, err
- }
- }
- return ret, nil
- }
-
- //------------------------------------------------------------------------------
-
- type XPending struct {
- Count int64
- Lower string
- Higher string
- Consumers map[string]int64
- }
-
- type XPendingCmd struct {
- baseCmd
- val *XPending
- }
-
- var _ Cmder = (*XPendingCmd)(nil)
-
- func NewXPendingCmd(args ...interface{}) *XPendingCmd {
- return &XPendingCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *XPendingCmd) Val() *XPending {
- return cmd.val
- }
-
- func (cmd *XPendingCmd) Result() (*XPending, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *XPendingCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *XPendingCmd) readReply(rd *proto.Reader) error {
- var info interface{}
- info, cmd.err = rd.ReadArrayReply(xPendingParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = info.(*XPending)
- return nil
- }
-
- func xPendingParser(rd *proto.Reader, n int64) (interface{}, error) {
- if n != 4 {
- return nil, fmt.Errorf("got %d, wanted 4", n)
- }
-
- count, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
-
- lower, err := rd.ReadString()
- if err != nil && err != Nil {
- return nil, err
- }
-
- higher, err := rd.ReadString()
- if err != nil && err != Nil {
- return nil, err
- }
-
- pending := &XPending{
- Count: count,
- Lower: lower,
- Higher: higher,
- }
- _, err = rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
- for i := int64(0); i < n; i++ {
- _, err = rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
- if n != 2 {
- return nil, fmt.Errorf("got %d, wanted 2", n)
- }
-
- consumerName, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- consumerPending, err := rd.ReadInt()
- if err != nil {
- return nil, err
- }
-
- if pending.Consumers == nil {
- pending.Consumers = make(map[string]int64)
- }
- pending.Consumers[consumerName] = consumerPending
-
- return nil, nil
- })
- if err != nil {
- return nil, err
- }
- }
- return nil, nil
- })
- if err != nil && err != Nil {
- return nil, err
- }
-
- return pending, nil
- }
-
- //------------------------------------------------------------------------------
-
- type XPendingExt struct {
- Id string
- Consumer string
- Idle time.Duration
- RetryCount int64
- }
-
- type XPendingExtCmd struct {
- baseCmd
- val []XPendingExt
- }
-
- var _ Cmder = (*XPendingExtCmd)(nil)
-
- func NewXPendingExtCmd(args ...interface{}) *XPendingExtCmd {
- return &XPendingExtCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *XPendingExtCmd) Val() []XPendingExt {
- return cmd.val
- }
-
- func (cmd *XPendingExtCmd) Result() ([]XPendingExt, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *XPendingExtCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *XPendingExtCmd) readReply(rd *proto.Reader) error {
- var info interface{}
- info, cmd.err = rd.ReadArrayReply(xPendingExtSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = info.([]XPendingExt)
- return nil
- }
-
- func xPendingExtSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- ret := make([]XPendingExt, 0, n)
- for i := int64(0); i < n; i++ {
- _, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
- if n != 4 {
- return nil, fmt.Errorf("got %d, wanted 4", n)
- }
-
- id, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- consumer, err := rd.ReadString()
- if err != nil && err != Nil {
- return nil, err
- }
-
- idle, err := rd.ReadIntReply()
- if err != nil && err != Nil {
- return nil, err
- }
-
- retryCount, err := rd.ReadIntReply()
- if err != nil && err != Nil {
- return nil, err
- }
-
- ret = append(ret, XPendingExt{
- Id: id,
- Consumer: consumer,
- Idle: time.Duration(idle) * time.Millisecond,
- RetryCount: retryCount,
- })
- return nil, nil
- })
- if err != nil {
- return nil, err
- }
- }
- return ret, nil
- }
-
- //------------------------------------------------------------------------------
-
- //------------------------------------------------------------------------------
-
- type ZSliceCmd struct {
- baseCmd
-
- val []Z
- }
-
- var _ Cmder = (*ZSliceCmd)(nil)
-
- func NewZSliceCmd(args ...interface{}) *ZSliceCmd {
- return &ZSliceCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *ZSliceCmd) Val() []Z {
- return cmd.val
- }
-
- func (cmd *ZSliceCmd) Result() ([]Z, error) {
- return cmd.val, cmd.err
- }
-
- func (cmd *ZSliceCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *ZSliceCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(zSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]Z)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func zSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- zz := make([]Z, n/2)
- for i := int64(0); i < n; i += 2 {
- var err error
-
- z := &zz[i/2]
-
- z.Member, err = rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- z.Score, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
- }
- return zz, nil
- }
-
- //------------------------------------------------------------------------------
-
- type ScanCmd struct {
- baseCmd
-
- page []string
- cursor uint64
-
- process func(cmd Cmder) error
- }
-
- var _ Cmder = (*ScanCmd)(nil)
-
- func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd {
- return &ScanCmd{
- baseCmd: baseCmd{_args: args},
- process: process,
- }
- }
-
- func (cmd *ScanCmd) Val() (keys []string, cursor uint64) {
- return cmd.page, cmd.cursor
- }
-
- func (cmd *ScanCmd) Result() (keys []string, cursor uint64, err error) {
- return cmd.page, cmd.cursor, cmd.err
- }
-
- func (cmd *ScanCmd) String() string {
- return cmdString(cmd, cmd.page)
- }
-
- func (cmd *ScanCmd) readReply(rd *proto.Reader) error {
- cmd.page, cmd.cursor, cmd.err = rd.ReadScanReply()
- return cmd.err
- }
-
- // Iterator creates a new ScanIterator.
- func (cmd *ScanCmd) Iterator() *ScanIterator {
- return &ScanIterator{
- cmd: cmd,
- }
- }
-
- //------------------------------------------------------------------------------
-
- type ClusterNode struct {
- Id string
- Addr string
- }
-
- type ClusterSlot struct {
- Start int
- End int
- Nodes []ClusterNode
- }
-
- type ClusterSlotsCmd struct {
- baseCmd
-
- val []ClusterSlot
- }
-
- var _ Cmder = (*ClusterSlotsCmd)(nil)
-
- func NewClusterSlotsCmd(args ...interface{}) *ClusterSlotsCmd {
- return &ClusterSlotsCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *ClusterSlotsCmd) Val() []ClusterSlot {
- return cmd.val
- }
-
- func (cmd *ClusterSlotsCmd) Result() ([]ClusterSlot, error) {
- return cmd.Val(), cmd.Err()
- }
-
- func (cmd *ClusterSlotsCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *ClusterSlotsCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(clusterSlotsParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.([]ClusterSlot)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func clusterSlotsParser(rd *proto.Reader, n int64) (interface{}, error) {
- slots := make([]ClusterSlot, n)
- for i := 0; i < len(slots); i++ {
- n, err := rd.ReadArrayLen()
- if err != nil {
- return nil, err
- }
- if n < 2 {
- err := fmt.Errorf("redis: got %d elements in cluster info, expected at least 2", n)
- return nil, err
- }
-
- start, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
-
- end, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
-
- nodes := make([]ClusterNode, n-2)
- for j := 0; j < len(nodes); j++ {
- n, err := rd.ReadArrayLen()
- if err != nil {
- return nil, err
- }
- if n != 2 && n != 3 {
- err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n)
- return nil, err
- }
-
- ip, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- port, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- nodes[j].Addr = net.JoinHostPort(ip, port)
-
- if n == 3 {
- id, err := rd.ReadString()
- if err != nil {
- return nil, err
- }
- nodes[j].Id = id
- }
- }
-
- slots[i] = ClusterSlot{
- Start: int(start),
- End: int(end),
- Nodes: nodes,
- }
- }
- return slots, nil
- }
-
- //------------------------------------------------------------------------------
-
- // GeoLocation is used with GeoAdd to add geospatial location.
- type GeoLocation struct {
- Name string
- Longitude, Latitude, Dist float64
- GeoHash int64
- }
-
- // GeoRadiusQuery is used with GeoRadius to query geospatial index.
- type GeoRadiusQuery struct {
- Radius float64
- // Can be m, km, ft, or mi. Default is km.
- Unit string
- WithCoord bool
- WithDist bool
- WithGeoHash bool
- Count int
- // Can be ASC or DESC. Default is no sort order.
- Sort string
- Store string
- StoreDist string
- }
-
- type GeoLocationCmd struct {
- baseCmd
-
- q *GeoRadiusQuery
- locations []GeoLocation
- }
-
- var _ Cmder = (*GeoLocationCmd)(nil)
-
- func NewGeoLocationCmd(q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd {
- args = append(args, q.Radius)
- if q.Unit != "" {
- args = append(args, q.Unit)
- } else {
- args = append(args, "km")
- }
- if q.WithCoord {
- args = append(args, "withcoord")
- }
- if q.WithDist {
- args = append(args, "withdist")
- }
- if q.WithGeoHash {
- args = append(args, "withhash")
- }
- if q.Count > 0 {
- args = append(args, "count", q.Count)
- }
- if q.Sort != "" {
- args = append(args, q.Sort)
- }
- if q.Store != "" {
- args = append(args, "store")
- args = append(args, q.Store)
- }
- if q.StoreDist != "" {
- args = append(args, "storedist")
- args = append(args, q.StoreDist)
- }
- return &GeoLocationCmd{
- baseCmd: baseCmd{_args: args},
- q: q,
- }
- }
-
- func (cmd *GeoLocationCmd) Val() []GeoLocation {
- return cmd.locations
- }
-
- func (cmd *GeoLocationCmd) Result() ([]GeoLocation, error) {
- return cmd.locations, cmd.err
- }
-
- func (cmd *GeoLocationCmd) String() string {
- return cmdString(cmd, cmd.locations)
- }
-
- func (cmd *GeoLocationCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(newGeoLocationSliceParser(cmd.q))
- if cmd.err != nil {
- return cmd.err
- }
- cmd.locations = v.([]GeoLocation)
- return nil
- }
-
- func newGeoLocationParser(q *GeoRadiusQuery) proto.MultiBulkParse {
- return func(rd *proto.Reader, n int64) (interface{}, error) {
- var loc GeoLocation
- var err error
-
- loc.Name, err = rd.ReadString()
- if err != nil {
- return nil, err
- }
- if q.WithDist {
- loc.Dist, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
- }
- if q.WithGeoHash {
- loc.GeoHash, err = rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- }
- if q.WithCoord {
- n, err := rd.ReadArrayLen()
- if err != nil {
- return nil, err
- }
- if n != 2 {
- return nil, fmt.Errorf("got %d coordinates, expected 2", n)
- }
-
- loc.Longitude, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
- loc.Latitude, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
- }
-
- return &loc, nil
- }
- }
-
- func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
- return func(rd *proto.Reader, n int64) (interface{}, error) {
- locs := make([]GeoLocation, 0, n)
- for i := int64(0); i < n; i++ {
- v, err := rd.ReadReply(newGeoLocationParser(q))
- if err != nil {
- return nil, err
- }
- switch vv := v.(type) {
- case string:
- locs = append(locs, GeoLocation{
- Name: vv,
- })
- case *GeoLocation:
- locs = append(locs, *vv)
- default:
- return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v)
- }
- }
- return locs, nil
- }
- }
-
- //------------------------------------------------------------------------------
-
- type GeoPos struct {
- Longitude, Latitude float64
- }
-
- type GeoPosCmd struct {
- baseCmd
-
- positions []*GeoPos
- }
-
- var _ Cmder = (*GeoPosCmd)(nil)
-
- func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
- return &GeoPosCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *GeoPosCmd) Val() []*GeoPos {
- return cmd.positions
- }
-
- func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
- return cmd.Val(), cmd.Err()
- }
-
- func (cmd *GeoPosCmd) String() string {
- return cmdString(cmd, cmd.positions)
- }
-
- func (cmd *GeoPosCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(geoPosSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.positions = v.([]*GeoPos)
- return nil
- }
-
- func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- positions := make([]*GeoPos, 0, n)
- for i := int64(0); i < n; i++ {
- v, err := rd.ReadReply(geoPosParser)
- if err != nil {
- if err == Nil {
- positions = append(positions, nil)
- continue
- }
- return nil, err
- }
- switch v := v.(type) {
- case *GeoPos:
- positions = append(positions, v)
- default:
- return nil, fmt.Errorf("got %T, expected *GeoPos", v)
- }
- }
- return positions, nil
- }
-
- func geoPosParser(rd *proto.Reader, n int64) (interface{}, error) {
- var pos GeoPos
- var err error
-
- pos.Longitude, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
-
- pos.Latitude, err = rd.ReadFloatReply()
- if err != nil {
- return nil, err
- }
-
- return &pos, nil
- }
-
- //------------------------------------------------------------------------------
-
- type CommandInfo struct {
- Name string
- Arity int8
- Flags []string
- FirstKeyPos int8
- LastKeyPos int8
- StepCount int8
- ReadOnly bool
- }
-
- type CommandsInfoCmd struct {
- baseCmd
-
- val map[string]*CommandInfo
- }
-
- var _ Cmder = (*CommandsInfoCmd)(nil)
-
- func NewCommandsInfoCmd(args ...interface{}) *CommandsInfoCmd {
- return &CommandsInfoCmd{
- baseCmd: baseCmd{_args: args},
- }
- }
-
- func (cmd *CommandsInfoCmd) Val() map[string]*CommandInfo {
- return cmd.val
- }
-
- func (cmd *CommandsInfoCmd) Result() (map[string]*CommandInfo, error) {
- return cmd.Val(), cmd.Err()
- }
-
- func (cmd *CommandsInfoCmd) String() string {
- return cmdString(cmd, cmd.val)
- }
-
- func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
- var v interface{}
- v, cmd.err = rd.ReadArrayReply(commandInfoSliceParser)
- if cmd.err != nil {
- return cmd.err
- }
- cmd.val = v.(map[string]*CommandInfo)
- return nil
- }
-
- // Implements proto.MultiBulkParse
- func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
- m := make(map[string]*CommandInfo, n)
- for i := int64(0); i < n; i++ {
- v, err := rd.ReadReply(commandInfoParser)
- if err != nil {
- return nil, err
- }
- vv := v.(*CommandInfo)
- m[vv.Name] = vv
-
- }
- return m, nil
- }
-
- func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
- var cmd CommandInfo
- var err error
-
- if n != 6 {
- return nil, fmt.Errorf("redis: got %d elements in COMMAND reply, wanted 6", n)
- }
-
- cmd.Name, err = rd.ReadString()
- if err != nil {
- return nil, err
- }
-
- arity, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- cmd.Arity = int8(arity)
-
- flags, err := rd.ReadReply(stringSliceParser)
- if err != nil {
- return nil, err
- }
- cmd.Flags = flags.([]string)
-
- firstKeyPos, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- cmd.FirstKeyPos = int8(firstKeyPos)
-
- lastKeyPos, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- cmd.LastKeyPos = int8(lastKeyPos)
-
- stepCount, err := rd.ReadIntReply()
- if err != nil {
- return nil, err
- }
- cmd.StepCount = int8(stepCount)
-
- for _, flag := range cmd.Flags {
- if flag == "readonly" {
- cmd.ReadOnly = true
- break
- }
- }
-
- return &cmd, nil
- }
-
- //------------------------------------------------------------------------------
-
- type cmdsInfoCache struct {
- fn func() (map[string]*CommandInfo, error)
-
- once internal.Once
- cmds map[string]*CommandInfo
- }
-
- func newCmdsInfoCache(fn func() (map[string]*CommandInfo, error)) *cmdsInfoCache {
- return &cmdsInfoCache{
- fn: fn,
- }
- }
-
- func (c *cmdsInfoCache) Get() (map[string]*CommandInfo, error) {
- err := c.once.Do(func() error {
- cmds, err := c.fn()
- if err != nil {
- return err
- }
- c.cmds = cmds
- return nil
- })
- return c.cmds, err
- }
|