redis/README.md

181 lines
3.3 KiB
Markdown
Raw Normal View History

2012-07-25 17:00:50 +04:00
Readme
======
2012-07-27 13:51:21 +04:00
Redis client for Golang.
2012-07-25 17:00:50 +04:00
Usage
-----
Example:
2012-07-27 13:51:21 +04:00
import "github.com/vmihailenco/redis"
2012-07-25 17:00:50 +04:00
connect := func() (io.ReadWriter, error) {
fmt.Println("Connecting...")
return net.Dial("tcp", "localhost:6379")
}
disconnect := func(conn io.ReadWriter) error {
fmt.Println("Disconnecting...")
conn.Close()
return nil
}
redisClient = redis.NewClient(connect, disconnect)
_, err := redisClient.Set("foo", "bar").Reply()
if err != nil {
2012-07-27 13:51:21 +04:00
panic(err)
2012-07-25 17:00:50 +04:00
}
value, err := redisClient.Get("foo").Reply()
if err != nil {
2012-07-27 13:51:21 +04:00
if err != redis.Nil {
panic(err)
}
2012-07-25 17:00:50 +04:00
}
2012-07-29 13:42:00 +04:00
Pipelining
----------
Client has ability to run several commands with one read/write:
setReq := redisClient.Set("foo1", "bar1") // queue command SET
getReq := redisClient.Get("foo2") // queue command GET
reqs, err := redisClient.RunQueued() // run queued commands
if err != nil {
panic(err)
}
ok, err := setReq.Reply()
if err != nil {
panic(err)
}
value, err := getReq.Reply()
if err != nil {
if err != redis.Nil {
panic(err)
}
}
2012-07-25 17:00:50 +04:00
Multi/Exec
----------
2012-07-27 13:51:21 +04:00
Getting multiClient:
2012-07-25 17:00:50 +04:00
multiClient := redisClient.Multi()
2012-07-27 13:51:21 +04:00
Or:
multiClient = redis.NewMultiClient(connect, disconnect)
2012-07-27 15:54:29 +04:00
Working with multiClient:
2012-07-27 13:51:21 +04:00
2012-07-25 17:00:50 +04:00
futureGet1 := multiClient.Get("foo1")
futureGet2 := multiClient.Get("foo2")
_, err := multiClient.Exec()
if err != nil {
panic(err)
}
value1, err := futureGet1.Reply()
if err != nil {
2012-07-27 13:51:21 +04:00
if err != redis.Nil {
panic(err)
}
2012-07-25 17:00:50 +04:00
}
value2, err := futureGet2.Reply()
if err != nil {
2012-07-27 13:51:21 +04:00
if err != redis.Nil {
panic(err)
}
2012-07-25 17:00:50 +04:00
}
2012-07-27 13:51:21 +04:00
Or:
2012-07-25 17:00:50 +04:00
multiClient := redisClient.Multi()
multiClient.Get("foo1")
multiClient.Get("foo2")
reqs, err := multiClient.Exec()
if err != nil {
2012-07-27 13:51:21 +04:00
panic(err)
2012-07-25 17:00:50 +04:00
}
for req := range reqs {
value, err := req.Reply()
if err != nil {
2012-07-27 13:51:21 +04:00
if err != redis.Nil {
panic(err)
}
2012-07-25 17:00:50 +04:00
}
}
Pub/sub
-------
Publish:
_, err := redisClient.Publish("mychannel", "hello").Reply()
if err != nil {
panic(err)
}
Subscribe:
pubsub := redisClient.PubSubClient()
2012-07-27 13:51:21 +04:00
// pubsub := redis.NewPubSubClient(connect, disconnect)
2012-07-25 17:00:50 +04:00
ch, err := pubsub.Subscribe("mychannel")
if err != nil {
panic(err)
}
go func() {
for msg := range ch {
if msg.Err != nil {
panic(err)
}
fmt.Println(msg.Message)
}
}
Thread safety
-------------
Client is thread safe. Internally sync.Mutex is used to synchronize writes and reads.
2012-07-27 15:53:23 +04:00
Custom commands
---------------
2012-07-29 13:42:00 +04:00
Lazy command:
2012-07-27 15:53:23 +04:00
func Get(client *redis.Client, key string) *redis.BulkReq {
req := redis.NewBulkReq("GET", key)
2012-07-29 13:42:00 +04:00
client.Queue(req)
2012-07-27 15:53:23 +04:00
return req
}
2012-07-27 18:23:18 +04:00
value, err := Get(redisClient, "foo").Reply()
2012-07-29 13:42:00 +04:00
if err != nil {
if err != redis.Nil {
panic(err)
}
}
Immediate command:
func Quit(client *redis.Client) *redis.StatusReq {
req := redis.NewStatusReq("QUIT")
client.Run(req)
return req
}
status, err := Quit(redisClient).Reply()
2012-07-27 15:53:23 +04:00
if err != nil {
panic(err)
}