Update docs.

This commit is contained in:
Vladimir Mihailenco 2012-08-25 16:27:05 +03:00
parent 1f7cc3fea5
commit a60c0a550b
2 changed files with 188 additions and 157 deletions

222
README.md
View File

@ -20,215 +20,123 @@ Install:
go get github.com/vmihailenco/redis go get github.com/vmihailenco/redis
Getting Client instance Getting started
----------------------- ---------------
Example 1: Let's start with connecting to Redis:
import "github.com/vmihailenco/redis" password := "" // no password set
db := -1 // use default DB
client := redis.NewTCPClient("localhost:6379", password, db)
defer client.Close()
Then we can start sending commands:
address := "localhost:6379" if err := client.Set("foo", "bar"); err != nil { panic(err) }
password := "secret"
db := 0
redisClient := redis.NewTCPClient(address, password, db)
Example 2: get := client.Get("foo")
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
import "github.com/vmihailenco/redis" We can also pipeline two commands together:
var set *redis.StatusReq
openConn := func() (net.Conn, error) { var get *redis.StringReq
fmt.Println("Connecting...") reqs, err := client.Pipelined(func(c *redis.PipelineClient)) {
return net.Dial("tcp", ":6379") set = c.Set("key1", "hello1")
get = c.Get("key2")
} }
if err != nil { panic(err) }
if err := set.Err(); err != nil { panic(err) }
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)
closeConn := func(conn net.Conn) error { or:
fmt.Println("Disconnecting...")
return nil
}
initConn := func(client *redis.Client) error { pipeline, err := client.PipelineClient()
auth := client.Auth("key")
if err := auth.Err(); err != nil {
return err
}
ping := client.Ping()
if err := ping.Err(); err != nil {
return err
}
return nil
}
redisClient := redis.NewClient(openConn, closeConn, initConn)
Both `closeConn` and `initConn` functions can be `nil`.
Running commands
----------------
set := redisClient.Set("key", "hello")
if err := set.Err(); err != nil {
panic(err)
}
ok := set.Val()
get := redisClient.Get("key")
if err := get.Err(); err != nil && err != redis.Nil {
panic(err)
}
val := get.Val()
Pipelining
----------
Client has ability to run commands in batches:
reqs, err := redisClient.Pipelined(func(c *redis.PipelineClient) {
c.Set("key1", "hello1") // queue command SET
c.Set("key2", "hello2") // queue command SET
})
if err != nil {
panic(err)
}
for _, req := range reqs {
// ...
}
Or:
pipeline, err := redisClient.PipelineClient()
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer pipeline.Close() defer pipeline.Close()
setReq := pipeline.Set("key1", "hello1") // queue command SET set := pipeline.Set("key1", "hello1")
getReq := pipeline.Get("key2") // queue command GET get := pipline.Get("key2")
reqs, err := pipeline.RunQueued() // run queued commands reqs, err := pipeline.RunQueued()
if err != nil { if err != nil { panic(err) }
panic(err)
}
for _, req := range reqs {
// ...
}
if err := setReq.Err(); err != nil { if err := set.Err(); err != nil { panic(err) }
panic(err if err := get.Err(); err != nil { panic(err) }
} fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)
if err := getReq.Err(); err != nil && err != redis.Nil { We can also send several commands in transaction:
panic(err)
}
Multi/Exec func incrKeyInTransaction(multi *redis.MultiClient) ([]redis.Req, error) {
---------- get := multi.Get("key")
Example:
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
get := multiClient.Get("key")
if err := get.Err(); err != nil { if err := get.Err(); err != nil {
panic(err) panic(err)
} }
reqs, err = multiClient.Exec(func() { val, err := strconv.ParseInt(get.Val(), 10, 64)
multi.Set("key", get.Val() + "1") if err != nil { panic(err) }
})
if err == redis.Nil {
return transaction()
}
reqs, err = multi.Exec(func() {
multi.Set("key", val + 1)
})
// Transaction failed. Repeat.
if err == redis.Nil {
return incrKeyInTransaction(multi)
}
return reqs, err return reqs, err
} }
multiClient, err := redisClient.MultiClient() multi, err := client.MultiClient()
if err != nil { if err != nil { panic(err) }
panic(err) defer multi.Close()
}
defer multiClient.Close()
watch := multiClient.Watch("key") watch := multi.Watch("key")
if err := watch.Err(); err != nil { if err := watch.Err(); err != nil { panic(err) }
panic(err)
}
reqs, err := transaction(multiClient) reqs, err := incrKeyInTransaction(multi)
if err != nil { if err != nil { panic(err) }
panic(err)
}
for _, req := range reqs { for _, req := range reqs {
// ... // ...
} }
Pub/sub To subscribe to the channel:
-------
Publish: pubsub, err := client.PubSubClient()
if err != nil { panic(err) }
pub := redisClient.Publish("mychannel", "hello")
if err := pub.Err(); err != nil {
panic(err)
}
Subscribe:
pubsub, err := redisClient.PubSubClient()
if err != nil {
panic(err)
}
defer pubsub.Close() defer pubsub.Close()
ch, err := pubsub.Subscribe("mychannel") ch, err := pubsub.Subscribe("mychannel")
if err != nil { if err != nil { panic(err) }
panic(err)
}
go func() { go func() {
for msg := range ch { for msg := range ch {
if msg.Err != nil { if err := msg.Err; err != nil { panic(err) }
panic(err)
}
message := msg.Message message := msg.Message
} }
} }
Thread safety You can also write custom commands:
-------------
Commands are thread safe. Following code is correct: func Get(client *redis.Client, key string) *redis.StringReq {
req := redis.NewStringReq("GET", key)
for i := 0; i < 1000; i++ {
go func() {
redisClient.Incr("key")
}()
}
Custom commands
---------------
Example:
func Get(client *redis.Client, key string) *redis.BulkReq {
req := redis.NewBulkReq("GET", key)
client.Process(req) client.Process(req)
return req return req
} }
get := Get(redisClient, "key") get := Get(redisClient, "key")
if err := get.Err(); err != nil && err != redis.Nil { if err := get.Err(); err != nil && err != redis.Nil { panic(err) }
panic(err)
}
Connection pool Client uses connection pool to send commands. You can change maximum number of connections with:
---------------
Client uses connection pool with default capacity of 10 connections. To change pool capacity: client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
redisClient.ConnPool.(*redis.MultiConnPool).MaxCap = 1
Look and feel Look and feel
------------- -------------

123
doc.go Normal file
View File

@ -0,0 +1,123 @@
// Copyright 2012 The Redis Client Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package github.com/vmihailenco/redis implements a Redis client.
Let's start with connecting to Redis:
password := "" // no password set
db := -1 // use default DB
client := redis.NewTCPClient("localhost:6379", password, db)
defer client.Close()
Then we can start sending commands:
if err := client.Set("foo", "bar"); err != nil { panic(err) }
get := client.Get("foo")
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
We can also pipeline two commands together:
var set *redis.StatusReq
var get *redis.StringReq
reqs, err := client.Pipelined(func(c *redis.PipelineClient)) {
set = c.Set("key1", "hello1")
get = c.Get("key2")
}
if err != nil { panic(err) }
if err := set.Err(); err != nil { panic(err) }
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)
or:
pipeline, err := client.PipelineClient()
if err != nil {
panic(err)
}
defer pipeline.Close()
set := pipeline.Set("key1", "hello1")
get := pipline.Get("key2")
reqs, err := pipeline.RunQueued()
if err != nil { panic(err) }
if err := set.Err(); err != nil { panic(err) }
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)
We can also send several commands in transaction:
func incrKeyInTransaction(multi *redis.MultiClient) ([]redis.Req, error) {
get := multi.Get("key")
if err := get.Err(); err != nil {
panic(err)
}
val, err := strconv.ParseInt(get.Val(), 10, 64)
if err != nil { panic(err) }
reqs, err = multi.Exec(func() {
multi.Set("key", val + 1)
})
// Transaction failed. Repeat.
if err == redis.Nil {
return incrKeyInTransaction(multi)
}
return reqs, err
}
multi, err := client.MultiClient()
if err != nil { panic(err) }
defer multi.Close()
watch := multi.Watch("key")
if err := watch.Err(); err != nil { panic(err) }
reqs, err := incrKeyInTransaction(multi)
if err != nil { panic(err) }
for _, req := range reqs {
// ...
}
To subscribe to the channel:
pubsub, err := client.PubSubClient()
if err != nil { panic(err) }
defer pubsub.Close()
ch, err := pubsub.Subscribe("mychannel")
if err != nil { panic(err) }
go func() {
for msg := range ch {
if err := msg.Err; err != nil { panic(err) }
message := msg.Message
}
}
You can also write custom commands:
func Get(client *redis.Client, key string) *redis.StringReq {
req := redis.NewStringReq("GET", key)
client.Process(req)
return req
}
get := Get(redisClient, "key")
if err := get.Err(); err != nil && err != redis.Nil { panic(err) }
Client uses connection pool to send commands. You can change maximum number of connections with:
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
*/
package redis