Documentation ¶
Overview ¶
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.
Client ¶
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:127.0.0.1:6379") reply, e := c.Call("GET", "foo") if e != nil { // handle error } println(reply.Elem.String())
AsyncClient ¶
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:127.0.0.1:6379") 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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ConnSum = 0
var (
ErrProtocol = errors.New("godis: protocol error")
)
var MaxConnections = 50
Functions ¶
This section is empty.
Types ¶
type AsyncClient ¶
type AsyncClient struct { *Client // 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 ¶
func NewAsyncClient(addr string, db int, password string) *AsyncClient
NewAsyncClient expects a addr like "tcp:127.0.0.1:6379" It returns a new *Client.
func (*AsyncClient) Call ¶
func (ac *AsyncClient) Call(args ...interface{}) (err error)
Call appends a command to the write buffer or returns an error.
func (*AsyncClient) Close ¶
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 ¶
func (ac *AsyncClient) Queued() int
func (*AsyncClient) Read ¶
func (ac *AsyncClient) Read() (*Reply, error)
Read does three things.
- Open connection to Redis server, if there is none.
- Write any buffered commands to the server.
- Try to read a reply from the server, or block on read.
Read returns a Reply or error.
func (*AsyncClient) ReadAll ¶
func (ac *AsyncClient) ReadAll() ([]*Reply, error)
type Client ¶
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 (*Client) AsyncClient ¶
func (c *Client) AsyncClient() *AsyncClient
Use the connection settings from Client to create a new AsyncClient
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn implements the Connection interface.
func NewConn ¶
NewConn expects a network address and protocol.
NewConn("127.0.0.1:6379", "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) Read ¶
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.