Untabify package doc.

This commit is contained in:
Vladimir Mihailenco 2013-09-29 11:33:06 +03:00
parent 37e2d9f22a
commit b2786fe22b
1 changed files with 111 additions and 111 deletions

222
v2/doc.go
View File

@ -3,160 +3,160 @@ Package github.com/vmihailenco/redis implements a Redis client.
Let's start with connecting to Redis using TCP: Let's start with connecting to Redis using TCP:
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
Password: "", // no password set Password: "", // no password set
DB: 0, // use default DB DB: 0, // use default DB
}) })
defer client.Close() defer client.Close()
ping := client.Ping() ping := client.Ping()
fmt.Println(ping.Err(), ping.Val()) fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG // Output: <nil> PONG
or using Unix socket: or using Unix socket:
client := redis.NewUnixClient(&redis.Options{ client := redis.NewUnixClient(&redis.Options{
Addr: "/tmp/redis.sock", Addr: "/tmp/redis.sock",
}) })
defer client.Close() defer client.Close()
ping := client.Ping() ping := client.Ping()
fmt.Println(ping.Err(), ping.Val()) fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG // Output: <nil> PONG
Then we can start sending commands: Then we can start sending commands:
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
set := client.Set("foo", "bar") set := client.Set("foo", "bar")
fmt.Println(set.Err(), set.Val()) fmt.Println(set.Err(), set.Val())
get := client.Get("foo") get := client.Get("foo")
fmt.Println(get.Err(), get.Val()) fmt.Println(get.Err(), get.Val())
// Output: <nil> OK // Output: <nil> OK
// <nil> bar // <nil> bar
We can also pipeline two commands together: We can also pipeline two commands together:
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
cmds, err := client.Pipelined(func(c *redis.Pipeline) { cmds, err := client.Pipelined(func(c *redis.Pipeline) {
c.Set("key1", "hello1") c.Set("key1", "hello1")
c.Get("key2") c.Get("key2")
}) })
fmt.Println(cmds, err) fmt.Println(cmds, err)
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil) // Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
or: or:
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
pipeline := client.Pipeline() pipeline := client.Pipeline()
set := pipeline.Set("key1", "hello1") set := pipeline.Set("key1", "hello1")
get := pipeline.Get("key2") get := pipeline.Get("key2")
cmds, err := pipeline.Exec() cmds, err := pipeline.Exec()
fmt.Println(cmds, err) fmt.Println(cmds, err)
fmt.Println(set) fmt.Println(set)
fmt.Println(get) fmt.Println(get)
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil) // Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
// SET key1 hello1: OK // SET key1 hello1: OK
// GET key2: (nil) // GET key2: (nil)
We can also send several commands in transaction: We can also send several commands in transaction:
incr := func(tx *redis.Multi) ([]redis.Cmder, error) { incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
get := tx.Get("key") get := tx.Get("key")
if err := get.Err(); err != nil && err != redis.Nil { if err := get.Err(); err != nil && err != redis.Nil {
return nil, err return nil, err
} }
val, _ := strconv.ParseInt(get.Val(), 10, 64) val, _ := strconv.ParseInt(get.Val(), 10, 64)
return tx.Exec(func() { return tx.Exec(func() {
tx.Set("key", strconv.FormatInt(val+1, 10)) tx.Set("key", strconv.FormatInt(val+1, 10))
}) })
} }
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
client.Del("key") client.Del("key")
tx := client.Multi() tx := client.Multi()
defer tx.Close() defer tx.Close()
watch := tx.Watch("key") watch := tx.Watch("key")
_ = watch.Err() _ = watch.Err()
for { for {
cmds, err := incr(tx) cmds, err := incr(tx)
if err == redis.Nil { if err == redis.Nil {
// Transaction failed. Repeat. // Transaction failed. Repeat.
continue continue
} else if err != nil { } else if err != nil {
panic(err) panic(err)
} }
fmt.Println(err, cmds) fmt.Println(err, cmds)
break break
} }
// Output: <nil> [SET key 1: OK] // Output: <nil> [SET key 1: OK]
To subscribe to the channel: To subscribe to the channel:
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
pubsub := client.PubSub() pubsub := client.PubSub()
defer pubsub.Close() defer pubsub.Close()
err := pubsub.Subscribe("mychannel") err := pubsub.Subscribe("mychannel")
_ = err _ = err
msg, err := pubsub.Receive() msg, err := pubsub.Receive()
fmt.Println(msg, err) fmt.Println(msg, err)
pub := client.Publish("mychannel", "hello") pub := client.Publish("mychannel", "hello")
_ = pub.Err() _ = pub.Err()
msg, err = pubsub.Receive() msg, err = pubsub.Receive()
fmt.Println(msg, err) fmt.Println(msg, err)
// Output: &{subscribe mychannel 1} <nil> // Output: &{subscribe mychannel 1} <nil>
// &{mychannel hello} <nil> // &{mychannel hello} <nil>
You can also write custom commands: You can also write custom commands:
func Get(client *redis.Client, key string) *redis.StringCmd { func Get(client *redis.Client, key string) *redis.StringCmd {
cmd := redis.NewStringCmd("GET", key) cmd := redis.NewStringCmd("GET", key)
client.Process(cmd) client.Process(cmd)
return cmd return cmd
} }
func ExampleCustomCommand() { func ExampleCustomCommand() {
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
get := Get(client, "key_does_not_exist") get := Get(client, "key_does_not_exist")
fmt.Println(get.Err(), get.Val()) fmt.Println(get.Err(), get.Val())
// Output: (nil) // Output: (nil)
} }
*/ */
package redis package redis