godis: github.com/simonz05/godis/exp Index | Files | Directories

package redis

import "github.com/simonz05/godis/exp"

Package redis implements a db client for Redis.

Connection interface

The Connection interface is a very simple interface to Redis. The Conn struct implements this interface and can be used to write commands and read replies from Redis.

The Connection interface is used to implement the Client and AsyncClient. Unless you like to implment your own client, use either of them instead of a single connection.


The Client implements one method; Call(). This writes your command to the database, then reads the subsequent reply and returns it to you.

The Client struct also has a pool of connections so it's safe to use a client in a concurrent context. You can create one client for your entire program and share it between go routines.

c := redis.NewClient("tcp:")
reply, e := c.Call("GET", "foo")

if e != nil {
    // handle error



The AsyncClient works exactly like the regular Client, and implements a single method Call(), but this method does not return any reply, only an error or nil.

c := redis.NewAsyncClient("tcp:")
c.Call("SET", "foo", 1)
c.Call("GET", "foo")

When we send our command and arguments to the Call() method nothing is sent to the Redis server. To get the reply for our commands from Redis we use the Read() method. Read sends any buffered commands to the Redis server, and then reads one reply. Subsequent calls to Read will return more replies or block if there are none.

// reply from SET
reply, _ := c.Read()

// reply from GET
reply, _ = c.Read()

println(reply.Elem.Int()) // prints 1

Due to the nature of how the AsyncClient works, it's not safe to share it between go routines.


Package Files

connection.go format.go godis.go parse.go pool.go reply.go


var ConnSum = 0
var (
    ErrProtocol = errors.New("godis: protocol error")
var MaxConnections = 50

type AsyncClient Uses

type AsyncClient struct {
    // contains filtered or unexported fields

Async client implements an asynchronous client. It is very similar to Client except that it maintains a buffer of commands which first are sent to Redis once we explicitly request a reply.

func NewAsyncClient Uses

func NewAsyncClient(addr string, db int, password string) *AsyncClient

NewAsyncClient expects a addr like "tcp:" It returns a new *Client.

func (*AsyncClient) Call Uses

func (ac *AsyncClient) Call(args ...interface{}) (err error)

Call appends a command to the write buffer or returns an error.

func (*AsyncClient) Close Uses

func (ac *AsyncClient) Close()

The AsyncClient will only open one connection. This is not automatically closed, so to close it we need to call this method.

func (*AsyncClient) Queued Uses

func (ac *AsyncClient) Queued() int

func (*AsyncClient) Read Uses

func (ac *AsyncClient) Read() (*Reply, error)

Read does three things.

1) Open connection to Redis server, if there is none.
2) Write any buffered commands to the server.
3) Try to read a reply from the server, or block on read.

Read returns a Reply or error.

func (*AsyncClient) ReadAll Uses

func (ac *AsyncClient) ReadAll() ([]*Reply, error)

type Client Uses

type Client struct {
    Addr     string
    Proto    string
    Db       int
    Password string
    // contains filtered or unexported fields

Client implements a Redis client which handles connections to the database in a pool. The size of the pool can be adjusted with by setting the MaxConnections variable before creating a client.

func NewClient Uses

func NewClient(addr string, db int, password string) *Client

NewClient expects a addr like "tcp:" It returns a new *Client.

func (*Client) AsyncClient Uses

func (c *Client) AsyncClient() *AsyncClient

Use the connection settings from Client to create a new AsyncClient

func (*Client) Call Uses

func (c *Client) Call(args ...interface{}) (*Reply, error)

Call is the canonical way of talking to Redis. It accepts any Redis command and a arbitrary number of arguments. Call returns a Reply object or an error.

type Conn Uses

type Conn struct {
    // contains filtered or unexported fields

Conn implements the Connection interface.

func NewConn Uses

func NewConn(addr, proto string, db int, password string) (*Conn, error)

NewConn expects a network address and protocol.

NewConn("", "tcp")

or for a unix domain socket

NewConn("/path/to/redis.sock", "unix")

NewConn then returns a Conn struct which implements the Connection interface. It's easy to use this interface to create your own redis client or to simply talk to the redis database.

func (*Conn) Close Uses

func (c *Conn) Close() error

Close is a simple helper method to close socket connection.

func (*Conn) Read Uses

func (c *Conn) Read() (*Reply, error)

Read reads one reply of the socket connection. If there is no reply waiting this method will block. Returns either an error or a pointer to a Reply object.

func (*Conn) Sock Uses

func (c *Conn) Sock() net.Conn

Sock returns the underlying net.Conn. You can use this connection as you wish. An example could be to set a r/w deadline on the connection.


func (*Conn) Write Uses

func (c *Conn) Write(args ...interface{}) error

Write accepts any redis command and arbitrary list of arguments.

Write("SET", "counter", 1)
Write("INCR", "counter")

Write might return a net.Conn.Write error

type Connection Uses

type Connection interface {
    Write(args ...interface{}) error
    Read() (*Reply, error)
    Close() error
    Sock() net.Conn

type Elem Uses

type Elem []byte

func (Elem) Bool Uses

func (e Elem) Bool() bool

func (Elem) Bytes Uses

func (e Elem) Bytes() []byte

func (Elem) Float64 Uses

func (e Elem) Float64() float64

func (Elem) Int Uses

func (e Elem) Int() int

func (Elem) Int64 Uses

func (e Elem) Int64() int64

func (Elem) String Uses

func (e Elem) String() string

type Message Uses

type Message struct {
    Channel string
    Elem    Elem

type Reply Uses

type Reply struct {
    Err   error
    Elem  Elem
    Elems []*Reply

func Parse Uses

func Parse(buf *bufin.Reader) *Reply

func (*Reply) BytesArray Uses

func (r *Reply) BytesArray() [][]byte

func (*Reply) Hash Uses

func (r *Reply) Hash() map[string]Elem

func (*Reply) IntArray Uses

func (r *Reply) IntArray() []int64

func (*Reply) Len Uses

func (r *Reply) Len() int

func (*Reply) Message Uses

func (r *Reply) Message() *Message

func (*Reply) Nil Uses

func (r *Reply) Nil() bool

func (*Reply) StringArray Uses

func (r *Reply) StringArray() []string

func (*Reply) StringMap Uses

func (r *Reply) StringMap() map[string]string



Package redis imports 9 packages (graph) and is imported by 6 packages. Updated 2016-07-26. Refresh now. Tools for package owners.