diff --git a/README.md b/README.md index 3bad983..3acf3ee 100644 --- a/README.md +++ b/README.md @@ -23,110 +23,120 @@ Install: Getting started --------------- -Let's start with connecting to Redis: +Let's start with connecting to Redis using TCP: - password := "" // no password set - db := -1 // use default DB + password := "" // no password set + db := int64(-1) // use default DB client := redis.NewTCPClient("localhost:6379", password, db) defer client.Close() + ping := client.Ping() + fmt.Println(ping.Err(), ping.Val()) + // Output: PONG + +or using Unix socket: + + client := redis.NewUnixClient("/tmp/redis.sock", "", -1) + defer client.Close() + + ping := client.Ping() + fmt.Println(ping.Err(), ping.Val()) + // Output: PONG + Then we can start sending commands: - set := client.Set("foo", "bar"); - if err := set.Err(); err != nil { - panic(err) - } + set := client.Set("foo", "bar") + fmt.Println(set.Err(), set.Val()) get := client.Get("foo") - if err := get.Err(); err != nil { - panic(err) - } - fmt.Println(get.Val()) + fmt.Println(get.Err(), get.Val()) + + // Output: OK + // bar We can also pipeline two commands together: var set *redis.StatusReq var get *redis.StringReq - reqs, err := client.Pipelined(func(c *redis.PipelineClient)) { + 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) + }) + fmt.Println(err) + fmt.Println(reqs) + fmt.Println(set) + fmt.Println(get) + // Output: [SET key1 hello1: OK GET key2: (nil)]> + // SET key1 hello1: OK + // GET key2: (nil) or: - pipeline, err := client.PipelineClient() - if err != nil { - panic(err) - } - defer pipeline.Close() - - set := pipeline.Set("key1", "hello1") - get := pipleine.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) + 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") + }) + fmt.Println(err, reqs) + fmt.Println(set) + fmt.Println(get) + // Output: [SET key1 hello1 GET key2] + // SET key1 hello1 + // GET key2 We can also send several commands in transaction: - func incrKeyInTransaction(multi *redis.MultiClient) ([]redis.Req, error) { + func transaction(multi *redis.MultiClient) ([]redis.Req, error) { get := multi.Get("key") - if err := get.Err(); err != nil { - panic(err) + if err := get.Err(); err != nil && err != redis.Nil { + return nil, err } - val, err := strconv.ParseInt(get.Val(), 10, 64) - if err != nil { panic(err) } + val, _ := strconv.ParseInt(get.Val(), 10, 64) - reqs, err = multi.Exec(func() { - multi.Set("key", val + 1) + reqs, err := multi.Exec(func() { + multi.Set("key", strconv.FormatInt(val+1, 10)) }) // Transaction failed. Repeat. if err == redis.Nil { - return incrKeyInTransaction(multi) + return transaction(multi) } return reqs, err } multi, err := client.MultiClient() - if err != nil { panic(err) } + _ = err defer multi.Close() watch := multi.Watch("key") - if err := watch.Err(); err != nil { panic(err) } + _ = watch.Err() - reqs, err := incrKeyInTransaction(multi) - if err != nil { panic(err) } - for _, req := range reqs { - // ... - } + reqs, err := transaction(multi) + fmt.Println(err, reqs) + + // Output: [SET key 1: OK] 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) } + _ = err - go func() { - for msg := range ch { - if err := msg.Err; err != nil { panic(err) } - message := msg.Message - } - } + subscribeMsg := <-ch + fmt.Println(subscribeMsg.Err, subscribeMsg.Name) + + pub := client.Publish("mychannel", "hello") + _ = pub.Err() + + msg := <-ch + fmt.Println(msg.Err, msg.Message) + + // Output: subscribe + // hello You can also write custom commands: @@ -136,8 +146,9 @@ You can also write custom commands: return req } - get := Get(redisClient, "key") - if err := get.Err(); err != nil && err != redis.Nil { panic(err) } + get := Get(client, "key_does_not_exist") + fmt.Println(get.Err(), get.Val()) + // Output: (nil) Client uses connection pool to send commands. You can change maximum number of connections with: diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..2661667 --- /dev/null +++ b/example_test.go @@ -0,0 +1,135 @@ +package redis_test + +import ( + "fmt" + "strconv" + + "github.com/vmihailenco/redis" +) + +func ExampleTCPClient() { + password := "" // no password set + db := int64(-1) // use default DB + client := redis.NewTCPClient("localhost:6379", password, db) + defer client.Close() + + ping := client.Ping() + fmt.Println(ping.Err(), ping.Val()) + // Output: PONG +} + +func ExampleUnixClient() { + client := redis.NewUnixClient("/tmp/redis.sock", "", -1) + defer client.Close() + + ping := client.Ping() + fmt.Println(ping.Err(), ping.Val()) + // Output: PONG +} + +func ExampleSetGet() { + client := redis.NewTCPClient(":6379", "", -1) + defer client.Close() + + set := client.Set("foo", "bar") + fmt.Println(set.Err(), set.Val()) + + get := client.Get("foo") + fmt.Println(get.Err(), get.Val()) + + // Output: OK + // bar +} + +func ExamplePipeline() { + client := redis.NewTCPClient(":6379", "", -1) + defer client.Close() + + 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") + }) + fmt.Println(err, reqs) + fmt.Println(set) + fmt.Println(get) + // Output: [SET key1 hello1: OK GET key2: (nil)] + // SET key1 hello1: OK + // GET key2: (nil) +} + +func transaction(multi *redis.MultiClient) ([]redis.Req, error) { + get := multi.Get("key") + if err := get.Err(); err != nil && err != redis.Nil { + return nil, err + } + + val, _ := strconv.ParseInt(get.Val(), 10, 64) + + reqs, err := multi.Exec(func() { + multi.Set("key", strconv.FormatInt(val+1, 10)) + }) + // Transaction failed. Repeat. + if err == redis.Nil { + return transaction(multi) + } + return reqs, err +} + +func ExampleTransaction() { + client := redis.NewTCPClient(":6379", "", -1) + defer client.Close() + + client.Del("key") + + multi, err := client.MultiClient() + _ = err + defer multi.Close() + + watch := multi.Watch("key") + _ = watch.Err() + + reqs, err := transaction(multi) + fmt.Println(err, reqs) + + // Output: [SET key 1: OK] +} + +func ExamplePubSub() { + client := redis.NewTCPClient(":6379", "", -1) + defer client.Close() + + pubsub, err := client.PubSubClient() + defer pubsub.Close() + + ch, err := pubsub.Subscribe("mychannel") + _ = err + + subscribeMsg := <-ch + fmt.Println(subscribeMsg.Err, subscribeMsg.Name) + + pub := client.Publish("mychannel", "hello") + _ = pub.Err() + + msg := <-ch + fmt.Println(msg.Err, msg.Message) + + // Output: subscribe + // hello +} + +func Get(client *redis.Client, key string) *redis.StringReq { + req := redis.NewStringReq("GET", key) + client.Process(req) + return req +} + +func ExampleCustomCommand() { + client := redis.NewTCPClient(":6379", "", -1) + defer client.Close() + + get := Get(client, "key_does_not_exist") + fmt.Println(get.Err(), get.Val()) + // Output: (nil) +} diff --git a/redis_test.go b/redis_test.go index 8d39c81..6102b07 100644 --- a/redis_test.go +++ b/redis_test.go @@ -270,6 +270,19 @@ func (t *RedisTest) resetRedis(c *C) { //------------------------------------------------------------------------------ +func (t *RedisTest) TestReqStringMethod(c *C) { + set := t.client.Set("foo", "bar") + c.Assert(set.String(), Equals, "SET foo bar: OK") + + get := t.client.Get("foo") + c.Assert(get.String(), Equals, "GET foo: bar") +} + +func (t *RedisTest) TestReqStringMethodError(c *C) { + get2 := t.client.Get("key_does_not_exists") + c.Assert(get2.String(), Equals, "GET key_does_not_exists: (nil)") +} + func (t *RedisTest) TestRunWithouthCheckingErrVal(c *C) { set := t.client.Set("key", "hello") c.Assert(set.Err(), IsNil) diff --git a/req.go b/req.go index b5940d7..180bfa4 100644 --- a/req.go +++ b/req.go @@ -1,7 +1,9 @@ package redis import ( + "fmt" "strconv" + "strings" ) type Req interface { @@ -64,6 +66,16 @@ func (r *BaseReq) ParseReply(rd reader) (interface{}, error) { return parseReply(rd) } +func (r *BaseReq) String() string { + args := strings.Join(r.args, " ") + if r.err != nil { + return args + ": " + r.err.Error() + } else if r.val != nil { + return args + ": " + fmt.Sprint(r.val) + } + return args +} + //------------------------------------------------------------------------------ type IfaceReq struct {