Change redis.Nil value.

This commit is contained in:
Vladimir Mihailenco 2013-11-04 10:08:03 +02:00
parent a05ac42753
commit ab4d0d6b62
3 changed files with 17 additions and 44 deletions

View File

@ -7,6 +7,14 @@ import (
"github.com/vmihailenco/redis/v2" "github.com/vmihailenco/redis/v2"
) )
var client *redis.Client
func init() {
client = redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
}
func ExampleNewTCPClient() { func ExampleNewTCPClient() {
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
@ -32,27 +40,17 @@ func ExampleNewUnixClient() {
} }
func ExampleClient() { func ExampleClient() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
set := client.Set("foo", "bar") set := client.Set("foo", "bar")
fmt.Println(set.Val(), set.Err()) fmt.Println(set.Val(), set.Err())
get := client.Get("foo") get := client.Get("hello")
fmt.Println(get.Val(), get.Err()) fmt.Printf("%q %s %v", get.Val(), get.Err(), get.Err() == redis.Nil)
// Output: OK <nil> // Output: OK <nil>
// bar <nil> // "" redis: nil true
} }
func ExampleClient_Pipelined() { func ExampleClient_Pipelined() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
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("key1") c.Get("key1")
@ -64,21 +62,16 @@ func ExampleClient_Pipelined() {
} }
func ExamplePipeline() { func ExamplePipeline() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
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("key1")
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 key1: hello1] <nil>
// SET key1 hello1: OK // SET key1 hello1: OK
// GET key2: (nil) // GET key1: hello1
} }
func ExampleMulti() { func ExampleMulti() {
@ -95,11 +88,6 @@ func ExampleMulti() {
}) })
} }
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
client.Del("key") client.Del("key")
tx := client.Multi() tx := client.Multi()
@ -123,11 +111,6 @@ func ExampleMulti() {
} }
func ExamplePubSub() { func ExamplePubSub() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
pubsub := client.PubSub() pubsub := client.PubSub()
defer pubsub.Close() defer pubsub.Close()
@ -148,11 +131,6 @@ func ExamplePubSub() {
} }
func ExampleScript() { func ExampleScript() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
setnx := redis.NewScript(` setnx := redis.NewScript(`
if redis.call("get", KEYS[1]) == false then if redis.call("get", KEYS[1]) == false then
redis.call("set", KEYS[1], ARGV[1]) redis.call("set", KEYS[1], ARGV[1])
@ -182,12 +160,7 @@ func Example_customCommand() {
return cmd return cmd
} }
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
get := Get(client, "key_does_not_exist") get := Get(client, "key_does_not_exist")
fmt.Printf("%q %s", get.Val(), get.Err()) fmt.Printf("%q %s", get.Val(), get.Err())
// Output: "" (nil) // Output: "" redis: nil
} }

View File

@ -19,7 +19,7 @@ const (
) )
// Redis nil reply. // Redis nil reply.
var Nil = errors.New("(nil)") var Nil = errors.New("redis: nil")
// Redis transaction failed. // Redis transaction failed.
var TxFailedErr = errors.New("redis: transaction failed") var TxFailedErr = errors.New("redis: transaction failed")

View File

@ -267,7 +267,7 @@ func (t *RedisTest) TestCmdStringMethod(c *C) {
func (t *RedisTest) TestCmdStringMethodError(c *C) { func (t *RedisTest) TestCmdStringMethodError(c *C) {
get2 := t.client.Get("key_does_not_exists") get2 := t.client.Get("key_does_not_exists")
c.Assert(get2.String(), Equals, "GET key_does_not_exists: (nil)") c.Assert(get2.String(), Equals, "GET key_does_not_exists: redis: nil")
} }
func (t *RedisTest) TestRunWithouthCheckingErrVal(c *C) { func (t *RedisTest) TestRunWithouthCheckingErrVal(c *C) {