forked from mirror/redis
Fix example in readme
This commit is contained in:
parent
a8cd040892
commit
a999d1ecd8
88
README.md
88
README.md
|
@ -44,44 +44,44 @@ import "github.com/go-redis/redis/v8"
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ExampleNewClient() {
|
func ExampleNewClient() {
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&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
|
||||||
})
|
})
|
||||||
|
|
||||||
pong, err := client.Ping().Result()
|
pong, err := client.Ping(ctx).Result()
|
||||||
fmt.Println(pong, err)
|
fmt.Println(pong, err)
|
||||||
// Output: PONG <nil>
|
// Output: PONG <nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleClient() {
|
func ExampleClient() {
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&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
|
||||||
})
|
})
|
||||||
err := client.Set("key", "value", 0).Err()
|
err := client.Set(ctx, "key", "value", 0).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := client.Get("key").Result()
|
val, err := client.Get(ctx, "key").Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("key", val)
|
fmt.Println("key", val)
|
||||||
|
|
||||||
val2, err := client.Get("key2").Result()
|
val2, err := client.Get(ctx, "key2").Result()
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
fmt.Println("key2 does not exist")
|
fmt.Println("key2 does not exist")
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("key2", val2)
|
fmt.Println("key2", val2)
|
||||||
}
|
}
|
||||||
// Output: key value
|
// Output: key value
|
||||||
// key2 does not exist
|
// key2 does not exist
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -95,30 +95,30 @@ Some corner cases:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// SET key value EX 10 NX
|
// SET key value EX 10 NX
|
||||||
set, err := client.SetNX("key", "value", 10*time.Second).Result()
|
set, err := client.SetNX(ctx, "key", "value", 10*time.Second).Result()
|
||||||
|
|
||||||
// SORT list LIMIT 0 2 ASC
|
// SORT list LIMIT 0 2 ASC
|
||||||
vals, err := client.Sort("list", &redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result()
|
vals, err := client.Sort(ctx, "list", &redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result()
|
||||||
|
|
||||||
// ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
|
// ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
|
||||||
vals, err := client.ZRangeByScoreWithScores("zset", &redis.ZRangeBy{
|
vals, err := client.ZRangeByScoreWithScores(ctx, "zset", &redis.ZRangeBy{
|
||||||
Min: "-inf",
|
Min: "-inf",
|
||||||
Max: "+inf",
|
Max: "+inf",
|
||||||
Offset: 0,
|
Offset: 0,
|
||||||
Count: 2,
|
Count: 2,
|
||||||
}).Result()
|
}).Result()
|
||||||
|
|
||||||
// ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
|
// ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
|
||||||
vals, err := client.ZInterStore("out", &redis.ZStore{
|
vals, err := client.ZInterStore(ctx, "out", &redis.ZStore{
|
||||||
Keys: []string{"zset1", "zset2"},
|
Keys: []string{"zset1", "zset2"},
|
||||||
Weights: []int64{2, 3}
|
Weights: []int64{2, 3}
|
||||||
}).Result()
|
}).Result()
|
||||||
|
|
||||||
// EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
|
// EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
|
||||||
vals, err := client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result()
|
vals, err := client.Eval(ctx, "return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result()
|
||||||
|
|
||||||
// custom command
|
// custom command
|
||||||
res, err := client.Do("set", "key", "value").Result()
|
res, err := client.Do(ctx, "set", "key", "value").Result()
|
||||||
```
|
```
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
Loading…
Reference in New Issue