Documentation ¶
Overview ¶
Copyright 2013 Leandro López (inkel)
This package implements a very low-level interface to read and write using the Redis protocol. It also provides a simple client to talk to a Redis server.
THIS IS FAR FROM BEING PRODUCTION READY.
API ¶
Gedis currently provides two API functions for writing and reading the [Redis protocol](http://redis.io/topics/protocol). It also defines two simple interfaces: `Reader` and `Writer`
Writing commands ¶
Gedis defines the following `Writer` interface:
type Writer interface { Write(p []byte) (n int, err error) }
It is possible to send Redis commands to any object that implements that interface, i.e. [`net.Conn`](http://golang.org/pkg/net/#Conn), by using the following function:
Write(w Writer, args ...string) (n int, err error)
Reading ¶
Gedis defines the following `Reader` interface:
type Reader interface { Read(b []byte) (n int, err error) }
It is possible to read Redis replies from any object that implements that interface, i.e. net.Conn, by using the following function:
Read(r Reader) (reply interface{}, err error)
API usage example
package main import ( "fmt" "github.com/inkel/gedis" "net" ) func main() { c, err := net.Dial("tcp", "localhost:6379") if err != nil { panic(err) } defer c.Close() f := func(args ...string) { fmt.Printf("> %s", args[0]) for _, arg := range args[1:] { fmt.Printf(" %q", arg) } fmt.Println() // Send to command to Redis server _, err := gedis.Write(c, args...) if err != nil { panic(err) } // Read the reply from the server res, err := gedis.Read(c) if err != nil { panic(err) } fmt.Printf("< %#v\n\n", res) } f("PING") f("SET", "lorem", "ipsum") f("INCR", "counter") f("HMSET", "hash", "field1", "lorem", "field2", "ipsum") f("HGETALL", "hash") f("MULTI") f("GET", "counter") f("GET", "nonexisting") f("EXEC") }
Client ¶
To avoid you the hassle of having to pass the connction parameter in every call, `gedis` defines the following `Client` object:
type Client struct {} func Dial(network, address string) (c Client, err error) func (c *Client) Close() error func (c *Client) Send(cmd string, args ...string) (interface{}, error)
Client usage example
package main import ( "fmt" "github.com/inkel/gedis" ) func main() { c, err := gedis.Dial("tcp", "localhost:6379") if err != nil { panic(err) } defer c.Close() f := func(args ...string) { fmt.Printf("> %s", args[0]) for _, arg := range args[1:] { fmt.Printf(" %q", arg) } fmt.Println() // Send to command to Redis server res, err := c.Send(args...) if err != nil { panic(err) } fmt.Printf("< %#v\n\n", res) } f("PING") f("SET", "lorem", "ipsum") f("INCR", "counter") f("HMSET", "hash", "field1", "lorem", "field2", "ipsum") f("HGETALL", "hash") f("MULTI") f("GET", "counter") f("GET", "nonexisting") f("EXEC") }
Why ¶
I wanted to learn Go, so I decided to write a minimal Redis client. Perhaps one day I might decide to do something else with it, but in the mean, the goals are merely academic. Feel free to comment on the code and send patches if you like.
Redis protocol ¶
Redis uses a very simple text protocol, which is binary safe.
*<num args> CR LF $<num bytes arg1> CR LF <arg data> CR LF ... $<num bytes argn> CR LF <arg data>
Index ¶
- func Read(r Reader) (ret interface{}, err error)
- func ReadNumber(r Reader) (n int64, err error)
- func Write(w Writer, args ...interface{}) (n int, err error)
- func WriteBulk(bulk string) []byte
- func WriteError(err error) []byte
- func WriteInt(n int64) []byte
- func WriteMultiBulk(args ...interface{}) []byte
- func WriteStatus(status string) []byte
- type ParseError
- type Reader
- type Writer
- Bugs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadNumber ¶ added in v0.0.5
Reads an int64 from the Reader
func WriteBulk ¶
Writes a string as a sequence of bytes to be send to a Redis instance, using the Redis Bulk format.
func WriteMultiBulk ¶
func WriteMultiBulk(args ...interface{}) []byte
Writes a sequence of strings as a sequence of bytes to be send to a Redis instance, using the Redis Multi-Bulk format.
func WriteStatus ¶ added in v0.0.5
Writes a status in the Redis protocol format
Types ¶
type ParseError ¶ added in v0.0.5
type ParseError struct {
// contains filtered or unexported fields
}
Struct to hold parsing errors
func NewParseError ¶ added in v0.0.5
func NewParseError(err string) *ParseError
func (*ParseError) Error ¶ added in v0.0.5
func (pe *ParseError) Error() string
Notes ¶
Bugs ¶
writeMultiBulk can't write multi-bulks inside multi-bulks
Directories ¶
Path | Synopsis |
---|---|
gedis client - Redis client written in Go This package allows to create clients that can talk to servers by using the Redis protocol Redis protocol: http://redis.io/topics/protocol Example package main import ( "fmt" "github.com/inkel/gedis/client" ) func main() { c, err := client.Dial("tcp", "localhost:6379") if err != nil { panic(err) } defer c.Close() f := func(args ...string) { fmt.Printf("> %s", args[0]) for _, arg := range args[1:] { fmt.Printf(" %q", arg) } fmt.Println() // Send to command to Redis server res, err := c.Send(args...) if err != nil { panic(err) } fmt.Printf("< %#v\n\n", res) } f("PING") f("SET", "lorem", "ipsum") f("INCR", "counter") f("HMSET", "hash", "field1", "lorem", "field2", "ipsum") f("HGETALL", "hash") }
|
gedis client - Redis client written in Go This package allows to create clients that can talk to servers by using the Redis protocol Redis protocol: http://redis.io/topics/protocol Example package main import ( "fmt" "github.com/inkel/gedis/client" ) func main() { c, err := client.Dial("tcp", "localhost:6379") if err != nil { panic(err) } defer c.Close() f := func(args ...string) { fmt.Printf("> %s", args[0]) for _, arg := range args[1:] { fmt.Printf(" %q", arg) } fmt.Println() // Send to command to Redis server res, err := c.Send(args...) if err != nil { panic(err) } fmt.Printf("< %#v\n\n", res) } f("PING") f("SET", "lorem", "ipsum") f("INCR", "counter") f("HMSET", "hash", "field1", "lorem", "field2", "ipsum") f("HGETALL", "hash") } |
gedis server - Generic Redis server implementation This package allows to create servers that can talk to clients by using the Redis protocol Redis protocol: http://redis.io/topics/protocol As an example, the following implements a very simple Redis server that only responds to the PING command: package main import ( "fmt" gedis "github.com/inkel/gedis/server" "os" "os/signal" ) func main() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) s, err := gedis.NewServer("tcp", ":10003") if err != nil { panic(err) } defer s.Close() pong := []byte("+PONG\r\n") earg := []byte("-ERR wrong number of arguments for 'ping' command\r\n") s.Handle("PING", func(c *gedis.Client, args [][]byte) error { if len(args) != 0 { c.Write(earg) return nil } else { c.Write(pong) } return nil }) go s.Loop() <-c fmt.Println("Bye!") }
|
gedis server - Generic Redis server implementation This package allows to create servers that can talk to clients by using the Redis protocol Redis protocol: http://redis.io/topics/protocol As an example, the following implements a very simple Redis server that only responds to the PING command: package main import ( "fmt" gedis "github.com/inkel/gedis/server" "os" "os/signal" ) func main() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) s, err := gedis.NewServer("tcp", ":10003") if err != nil { panic(err) } defer s.Close() pong := []byte("+PONG\r\n") earg := []byte("-ERR wrong number of arguments for 'ping' command\r\n") s.Handle("PING", func(c *gedis.Client, args [][]byte) error { if len(args) != 0 { c.Write(earg) return nil } else { c.Write(pong) } return nil }) go s.Loop() <-c fmt.Println("Bye!") } |