redis

package
v0.0.0-...-109ffb8 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2012 License: BSD-3-Clause, BSD-3-Clause Imports: 9 Imported by: 0

README

exp-godis

godis/exp - an experimental Redis client for Go. It supports commands and features through a simple API which aims to be is easy to use.

  1. Package docs
  2. Source code

Install godis

godis/exp is available at github.com. Get it by running.

$ go get github.com/simonz05/godis/exp

Importing godis/exp will add a redis context to program.

Use exp-godis

A few examples are included. The following demonstrates SET and GET. See example/ for more.

package main

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

func main() {
    c := redis.NewClient("tcp:127.0.0.1:6379")

    res, err := c.Call("SET", "foo", "bar")

    if err != nil {
        println(err.Error())
        return
    }

    res, _ = c.Call("GET", "foo")
    println("GET foo:", res.Elem.String())
}

Build and run the example.

$ make string; ./string

You should see the following printed in the terminal.

GET foo: bar 

In case your redis server isn't running, you'll get an error.

ERR 127.0.0.1:6379

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

View Source
var ConnSum = 0
View Source
var (
	ErrProtocol = errors.New("godis: protocol error")
)
View Source
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.

  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

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 NewClient

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 (*Client) AsyncClient

func (c *Client) AsyncClient() *AsyncClient

Use the connection settings from Client to create a new AsyncClient

func (*Client) Call

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

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

Conn implements the Connection interface.

func NewConn

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 (*Conn) Close

func (c *Conn) Close() error

Close is a simple helper method to close socket connection.

func (*Conn) Read

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

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 (*Conn) Write

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

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

type Elem

type Elem []byte

func (Elem) Bool

func (e Elem) Bool() bool

func (Elem) Bytes

func (e Elem) Bytes() []byte

func (Elem) Float64

func (e Elem) Float64() float64

func (Elem) Int

func (e Elem) Int() int

func (Elem) Int64

func (e Elem) Int64() int64

func (Elem) String

func (e Elem) String() string

type Message

type Message struct {
	Channel string
	Elem    Elem
}

type Reply

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

func Parse

func Parse(buf *bufin.Reader) *Reply

func (*Reply) BytesArray

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

func (*Reply) Hash

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

func (*Reply) IntArray

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

func (*Reply) Len

func (r *Reply) Len() int

func (*Reply) Message

func (r *Reply) Message() *Message

func (*Reply) Nil

func (r *Reply) Nil() bool

func (*Reply) StringArray

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

func (*Reply) StringMap

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

Directories

Path Synopsis
misc
net

Jump to

Keyboard shortcuts

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