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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
Working with multiClient::
|
|
|
|
|
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
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
func Get(client *redis.Client, key string) *redis.BulkReq {
|
|
|
|
req := redis.NewBulkReq("GET", key)
|
|
|
|
client.Run(req)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := Get(redisClient).Reply()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|