client

package
v0.0.0-...-dd03fda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2019 License: MIT, MIT Imports: 14 Imported by: 0

Documentation

Overview

The client is the primary interface for redis. You must first create a client with redis address for working.

c := NewClient("127.0.0.1:6380")

or

c := NewClient("127.0.0.1:6380",DialWriteTimeout(1024),DialPassword("1234"))

The most important function for client is Do function to send commands to remote server.

reply, err := c.Do("ping")

reply, err := c.Do("set", "key", "value")

reply, err := c.Do("get", "key")

Connection

You can use an independent connection to send commands.

//get a connection
conn, _ := c.Get()

//connection send command
conn.Do("ping")

Reply Helper

You can use reply helper to convert a reply to a specific type.

exists, err := Bool(c.Do("exists", "key"))

score, err := Int64(c.Do("zscore", "key", "member"))

Index

Constants

This section is empty.

Variables

View Source
var (
	OkReply   interface{} = "OK"
	PongReply interface{} = "PONG"
	NilReply  interface{} = nil
)
View Source
var ErrNil = errors.New("nil returned")

ErrNil indicates that a reply value is nil.

Functions

func Bool

func Bool(reply interface{}, err error) (bool, error)

Bool is a helper that converts a command reply to a boolean. If err is not equal to nil, then Bool returns false, err. Otherwise Bool converts the reply to boolean as follows:

Reply type      Result
integer         value != 0, nil
bulk string     strconv.ParseBool(reply)
nil             false, ErrNil
other           false, error

func Bytes

func Bytes(reply interface{}, err error) ([]byte, error)

Bytes is a helper that converts a command reply to a slice of bytes. If err is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts the reply to a slice of bytes as follows:

Reply type      Result
bulk string     reply, nil
simple string   []byte(reply), nil
nil             nil, ErrNil
other           nil, error

func Float64

func Float64(reply interface{}, err error) (float64, error)

Float64 is a helper that converts a command reply to 64 bit float. If err is not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts the reply to an int as follows:

Reply type    Result
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int

func Int(reply interface{}, err error) (int, error)

Int is a helper that converts a command reply to an integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int converts the reply to an int as follows:

Reply type    Result
integer       int(reply), nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int64

func Int64(reply interface{}, err error) (int64, error)

Int64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func MultiBulk

func MultiBulk(reply interface{}, err error) ([]interface{}, error)

MultiBulk is deprecated. Use Values.

func String

func String(reply interface{}, err error) (string, error)

String is a helper that converts a command reply to a string. If err is not equal to nil, then String returns "", err. Otherwise String converts the reply to a string as follows:

Reply type      Result
bulk string     string(reply), nil
simple string   reply, nil
nil             "",  ErrNil
other           "",  error

func Strings

func Strings(reply interface{}, err error) ([]string, error)

Strings is a helper that converts an array command reply to a []string. If err is not equal to nil, then Strings returns nil, err. If one of the array items is not a bulk string or nil, then Strings returns an error.

func Uint64

func Uint64(reply interface{}, err error) (uint64, error)

Uint64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Values

func Values(reply interface{}, err error) ([]interface{}, error)

Values is a helper that converts an array command reply to a []interface{}. If err is not equal to nil, then Values returns nil, err. Otherwise, Values converts the reply as follows:

Reply type      Result
array           reply, nil
nil             nil, ErrNil
other           nil, error

Types

type Client

type Client struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewClient

func NewClient(addr string, dialOpts ...DialOption) *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) Do

func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error)

func (*Client) Get

func (c *Client) Get() (*PoolConn, error)

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(addr string, opt *Options) (*Conn, error)

func ConnectWithOptions

func ConnectWithOptions(addr string, opt *Options) (*Conn, error)

func NewConn

func NewConn(conn net.Conn, opt *Options) (*Conn, error)

func NewConnWithSize

func NewConnWithSize(conn net.Conn, opt *Options) (*Conn, error)

func (*Conn) Addr

func (c *Conn) Addr() (string, string, error)

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) Do

func (c *Conn) Do(cmd string, args ...interface{}) (interface{}, error)

Send RESP command and receive the reply

func (*Conn) DumpAndParse

func (c *Conn) DumpAndParse(parse func(io.Reader) error) error

func (*Conn) GetTotalReadSize

func (c *Conn) GetTotalReadSize() int64

func (*Conn) GetTotalWriteSize

func (c *Conn) GetTotalWriteSize() int64

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

func (*Conn) Receive

func (c *Conn) Receive() (interface{}, error)

Receive RESP reply

func (*Conn) ReceiveBulkTo

func (c *Conn) ReceiveBulkTo(w io.Writer) error

Receive RESP bulk string reply into writer w

func (*Conn) ReceiveRequest

func (c *Conn) ReceiveRequest() ([][]byte, error)

Receive RESP command request, must array of bulk stirng

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

func (*Conn) Send

func (c *Conn) Send(cmd string, args ...interface{}) error

Send RESP command

func (*Conn) SendValue

func (c *Conn) SendValue(v interface{}) error

Send RESP value, must be string, int64, []byte, error, nil or []interface{}

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

type DataStorage

type DataStorage interface {
	// data operate command
	Set([]byte, []byte) error
	Get([]byte) ([]byte, error)
	Hset([]byte, []byte, []byte) error
	Hget([]byte, []byte) ([]byte, error)

	// system command
	Cluster() ([]byte, error)
	Sentinel() ([]interface{}, error)
	Info() ([]byte, error)
}

type DialOption

type DialOption struct {
	// contains filtered or unexported fields
}

DialOption specifies an option for dialing a Redis server.

func DialConnectTimeout

func DialConnectTimeout(d time.Duration) DialOption

DialConnectTimeout specifies the timeout for connecting to the Redis server when no DialNetDial option is specified.

func DialKeepAlive

func DialKeepAlive(d time.Duration) DialOption

DialKeepAlive specifies the keep-alive period for TCP connections to the Redis server when no DialNetDial option is specified. If zero, keep-alives are not enabled. If no DialKeepAlive option is specified then the default of 5 minutes is used to ensure that half-closed TCP sessions are detected.

func DialMaxIdelConns

func DialMaxIdelConns(i int) DialOption

func DialNetDial

func DialNetDial(dial func(network, addr string) (net.Conn, error)) DialOption

DialNetDial specifies a custom dial function for creating TCP connections, otherwise a net.Dialer customized via the other options is used. DialNetDial overrides DialConnectTimeout and DialKeepAlive.

func DialPassword

func DialPassword(password string) DialOption

DialPassword specifies the password to use when connecting to the Redis server.

func DialReadTimeout

func DialReadTimeout(d time.Duration) DialOption

DialReadTimeout specifies the timeout for reading a single command reply.

func DialTLSConfig

func DialTLSConfig(c *tls.Config) DialOption

DialTLSConfig specifies the config to use when a TLS connection is dialed. Has no effect when not dialing a TLS connection.

func DialTLSSkipVerify

func DialTLSSkipVerify(skip bool) DialOption

DialTLSSkipVerify disables server name verification when connecting over TLS. Has no effect when not dialing a TLS connection.

func DialUseTLS

func DialUseTLS(useTLS bool) DialOption

DialUseTLS specifies whether TLS should be used when connecting to the server. This option is ignore by DialURL.

func DialWriteTimeout

func DialWriteTimeout(d time.Duration) DialOption

DialWriteTimeout specifies the timeout for writing a single command.

type Error

type Error string

func (Error) Error

func (err Error) Error() string

type HandlesFunc

type HandlesFunc = func(rWriter *RespWriter, req [][]byte) error

type Options

type Options struct {
	// contains filtered or unexported fields
}

func NewOption

func NewOption() *Options

func (*Options) SetMaxIdelConns

func (o *Options) SetMaxIdelConns(i int)

func (*Options) SetPassword

func (o *Options) SetPassword(s string)

type PoolConn

type PoolConn struct {
	*Conn
	*Client
}

func (*PoolConn) Close

func (c *PoolConn) Close()

func (*PoolConn) Finalize

func (c *PoolConn) Finalize()

force close inner connection and not put it into pool

type RespReader

type RespReader struct {
	// contains filtered or unexported fields
}

func NewRespReader

func NewRespReader(br *bufio.Reader) *RespReader

func (*RespReader) Parse

func (resp *RespReader) Parse() (interface{}, error)

Parse RESP

func (*RespReader) ParseBulkTo

func (resp *RespReader) ParseBulkTo(w io.Writer) error

Parse bulk string and write it with writer w

func (*RespReader) ParseRequest

func (resp *RespReader) ParseRequest() ([][]byte, error)

Parse client -> server command request, must be array of bulk strings

type RespWriter

type RespWriter struct {
	// contains filtered or unexported fields
}

func NewRespWriter

func NewRespWriter(bw *bufio.Writer) *RespWriter

func (*RespWriter) Flush

func (resp *RespWriter) Flush() error

func (*RespWriter) FlushArray

func (resp *RespWriter) FlushArray(ay []interface{}) error

func (*RespWriter) FlushBulk

func (resp *RespWriter) FlushBulk(b []byte) error

func (*RespWriter) FlushError

func (resp *RespWriter) FlushError(e error) error

func (*RespWriter) FlushInteger

func (resp *RespWriter) FlushInteger(n int64) error

func (*RespWriter) FlushString

func (resp *RespWriter) FlushString(s string) error

func (*RespWriter) WriteArray

func (resp *RespWriter) WriteArray(ay []interface{}) error

func (*RespWriter) WriteBulk

func (resp *RespWriter) WriteBulk(b []byte) error

func (*RespWriter) WriteBytesArray

func (resp *RespWriter) WriteBytesArray(ay [][]byte) error

func (*RespWriter) WriteCommand

func (resp *RespWriter) WriteCommand(cmd string, args ...interface{}) error

RESP command is array of bulk string

func (*RespWriter) WriteError

func (resp *RespWriter) WriteError(e error) error

func (*RespWriter) WriteInteger

func (resp *RespWriter) WriteInteger(n int64) error

func (*RespWriter) WriteStr2BytesArray

func (resp *RespWriter) WriteStr2BytesArray(ay []interface{}) error

func (*RespWriter) WriteString

func (resp *RespWriter) WriteString(s string) error

type VirtualServer

type VirtualServer struct {
	DataStorage
	// contains filtered or unexported fields
}

func NewVirtualServer

func NewVirtualServer(stge DataStorage) *VirtualServer

func (*VirtualServer) AddHandles

func (v *VirtualServer) AddHandles(name string, h HandlesFunc)

func (*VirtualServer) Serve

func (v *VirtualServer) Serve(addr string) (cancel func(), port string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL