radix: github.com/juhanoi/radix/redis Index | Files | Directories

package redis

import "github.com/juhanoi/radix/redis"

A simple client for connecting and interacting with redis.

To import inside your package do:

import "github.com/fzzy/radix/redis"

Connecting

Use either Dial or DialTimeout:

client, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
	// handle err
}

Make sure to call Close on the client if you want to clean it up before the end of the program.

Cmd and Reply

The Cmd method returns a Reply, which has methods for converting to various types. Each of these methods returns an error which can either be a connection error (e.g. timeout), an application error (e.g. key is wrong type), or a conversion error (e.g. cannot convert to integer). You can also directly check the error using the Err field:

foo, err := client.Cmd("GET", "foo").Str()
if err != nil {
	// handle err
}

// Checking Err field directly

err = client.Cmd("SET", "foo", "bar", "EX", 3600).Err
if err != nil {
	// handle err
}

Multi Replies

The elements to Multi replies can be accessed as strings using List or ListBytes, or you can use the Elems field for more low-level access:

r := client.Cmd("MGET", "foo", "bar", "baz")

// This:
for _, elemStr := range r.List() {
	fmt.Println(elemStr)
}

// is equivalent to this:
for i := range r.Elems {
	elemStr, _ := r.Elems[i].Str()
	fmt.Println(elemStr)
}

Pipelining

Pipelining is when the client sends a bunch of commands to the server at once, and only once all the commands have been sent does it start reading the replies off the socket. This is supported using the Append and GetReply methods. Append will simply append the command to a buffer without sending it, the first time GetReply is called it will send all the commands in the buffer and return the Reply for the first command that was sent. Subsequent calls to GetReply return Replys for subsequent commands:

client.Append("GET", "foo")
client.Append("SET", "bar", "foo")
client.Append("DEL", "baz")

// Read GET foo reply
foo, err := client.GetReply().Str()
if err != nil {
	// handle err
}

// Read SET bar foo reply
if err := client.GetReply().Err; err != nil {
	// handle err
}

// Read DEL baz reply
if err := client.GetReply().Err; err != nil {
	// handle err
}

Index

Package Files

client.go doc.go reply.go

Variables

var LoadingError error = errors.New("server is busy loading dataset in memory")
var PipelineQueueEmptyError error = errors.New("pipeline queue empty")

type Client Uses

type Client struct {
    // The connection the client talks to redis over. Don't touch this unless
    // you know what you're doing.
    Conn net.Conn
    // contains filtered or unexported fields
}

Client describes a Redis client.

func Dial Uses

func Dial(network, addr string) (*Client, error)

Dial connects to the given Redis server.

func DialTimeout Uses

func DialTimeout(network, addr string, timeout time.Duration) (*Client, error)

Dial connects to the given Redis server with the given timeout, which will be used as the read/write timeout when communicating with redis

func (*Client) Append Uses

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

Append adds the given call to the pipeline queue. Use GetReply() to read the reply.

func (*Client) Close Uses

func (c *Client) Close() error

Close closes the connection.

func (*Client) Cmd Uses

func (c *Client) Cmd(cmd string, args ...interface{}) *Reply

Cmd calls the given Redis command.

func (*Client) GetReply Uses

func (c *Client) GetReply() *Reply

GetReply returns the reply for the next request in the pipeline queue. Error reply with PipelineQueueEmptyError is returned, if the pipeline queue is empty.

func (*Client) ReadReply Uses

func (c *Client) ReadReply() *Reply

This will read a redis reply off of the connection without sending anything first (useful after you've sent a SUSBSCRIBE command). This will block until a reply is received or the timeout is reached. On timeout an ErrorReply will be returned, you can check if it's a timeout like so:

r := conn.ReadReply()
if r.Err != nil {
	if t, ok := r.Err.(*net.OpError); ok && t.Timeout() {
		// Is timeout
	} else {
		// Not timeout
	}
}

Note: this is a more low-level function, you really shouldn't have to actually use it unless you're writing your own pub/sub code

type CmdError Uses

type CmdError struct {
    Err error
}

A CmdError implements the error interface and is what is returned when the server returns an error on the application level (e.g. key doesn't exist or is the wrong type), as opposed to a connection/transport error.

You can test if a reply is a CmdError like so:

r := conn.Cmd("GET", "key-which-isnt-a-string")
if r.Err != nil {
	if cerr, ok := r.Err.(*redis.CmdError); ok {
		// Is CmdError
	} else {
		// Is other error
	}
}

func (*CmdError) Error Uses

func (cerr *CmdError) Error() string

func (*CmdError) Readonly Uses

func (cerr *CmdError) Readonly() bool

Returns true if error returned was due to the redis server being read only

type Reply Uses

type Reply struct {
    Type  ReplyType // Reply type
    Elems []*Reply  // Sub-replies
    Err   error     // Reply error
    // contains filtered or unexported fields
}

Reply holds a Redis reply.

func (*Reply) Bool Uses

func (r *Reply) Bool() (bool, error)

Bool returns false, if the reply value equals to 0 or "0", otherwise true; or an error, if the reply type is not IntegerReply or BulkReply.

func (*Reply) Bytes Uses

func (r *Reply) Bytes() ([]byte, error)

Bytes returns the reply value as a byte string or an error, if the reply type is not StatusReply or BulkReply.

func (*Reply) Float64 Uses

func (r *Reply) Float64() (float64, error)

Float64 returns the reply value as a float64 or an error, if the reply type is not BulkReply or the reply type BulkReply could not be parsed to an float64.

func (*Reply) Hash Uses

func (r *Reply) Hash() (map[string]string, error)

Hash returns a multi bulk reply as a map[string]string or an error. The reply type must be MultiReply, it must have an even number of elements, they must be in a "key value key value..." order and values must all be either BulkReply or NilReply. Nil values are returned as empty strings. Useful for hash commands.

func (*Reply) Int Uses

func (r *Reply) Int() (int, error)

Int is a convenience method for calling Reply.Int64() and converting it to int.

func (*Reply) Int64 Uses

func (r *Reply) Int64() (int64, error)

Int64 returns the reply value as a int64 or an error, if the reply type is not IntegerReply or the reply type BulkReply could not be parsed to an int64.

func (*Reply) List Uses

func (r *Reply) List() ([]string, error)

List returns a multi bulk reply as a slice of strings or an error. The reply type must be MultiReply and its elements' types must all be either BulkReply or NilReply. Nil elements are returned as empty strings. Useful for list commands.

func (*Reply) ListBytes Uses

func (r *Reply) ListBytes() ([][]byte, error)

ListBytes returns a multi bulk reply as a slice of bytes slices or an error. The reply type must be MultiReply and its elements' types must all be either BulkReply or NilReply. Nil elements are returned as nil. Useful for list commands.

func (*Reply) Str Uses

func (r *Reply) Str() (string, error)

Str is a convenience method for calling Reply.Bytes() and converting it to string

func (*Reply) String Uses

func (r *Reply) String() string

String returns a string representation of the reply and its sub-replies. This method is for debugging. Use method Reply.Str() for reading string reply.

type ReplyType Uses

type ReplyType uint8

ReplyType describes type of a reply.

Possible values are:

StatusReply -- status reply ErrorReply -- error reply IntegerReply -- integer reply NilReply -- nil reply BulkReply -- bulk reply MultiReply -- multi bulk reply

const (
    StatusReply ReplyType = iota
    ErrorReply
    IntegerReply
    NilReply
    BulkReply
    MultiReply
)

Directories

PathSynopsis
respThis package provides an easy to use interface for creating and parsing messages encoded in the Redis Serialization Protocol (RESP).

Package redis imports 7 packages (graph). Updated 2018-02-16. Refresh now. Tools for package owners.