import "github.com/simonz05/godis/exp"
Package redis implements a db client for Redis.
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:127.0.0.1:6379")
reply, e := c.Call("GET", "foo")
if e != nil {
// handle error
}
println(reply.Elem.String())
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.
var ConnSum = 0var (
ErrProtocol = errors.New("godis: protocol error")
)var MaxConnections = 50
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(addr string, db int, password string) *AsyncClient
NewAsyncClient expects a addr like "tcp:127.0.0.1:6379" It returns a new *Client.
func (ac *AsyncClient) Call(args ...interface{}) (err error)
Call appends a command to the write buffer or returns an error.
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 (ac *AsyncClient) Queued() int
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 (ac *AsyncClient) ReadAll() ([]*Reply, error)
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(addr string, db int, password string) *Client
NewClient expects a addr like "tcp:127.0.0.1:6379" It returns a new *Client.
func (c *Client) AsyncClient() *AsyncClient
Use the connection settings from Client to create a new AsyncClient
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 struct {
// contains filtered or unexported fields
}Conn implements the Connection interface.
func NewConn(addr, proto string, db int, password string) (*Conn, error)
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 (c *Conn) Close() error
Close is a simple helper method to close socket connection.
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 (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.
Sock().SetDeadline(t)
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 interface {
Write(args ...interface{}) error
Read() (*Reply, error)
Close() error
Sock() net.Conn
}
type Elem []byte
func (e Elem) Bool() bool
func (e Elem) Bytes() []byte
func (e Elem) Float64() float64
func (e Elem) Int() int
func (e Elem) Int64() int64
func (e Elem) String() string
type Message struct {
Channel string
Elem Elem
}
type Reply struct {
Err error
Elem Elem
Elems []*Reply
}
func Parse(buf *bufin.Reader) *Reply
func (r *Reply) BytesArray() [][]byte
func (r *Reply) Hash() map[string]Elem
func (r *Reply) IntArray() []int64
func (r *Reply) Len() int
func (r *Reply) Message() *Message
func (r *Reply) Nil() bool
func (r *Reply) StringArray() []string
func (r *Reply) StringMap() map[string]string