forked from mirror/redis
Merge pull request #10 from gmcintire/master
Update README to enable syntax highlighting
This commit is contained in:
commit
dc90e359e9
195
README.md
195
README.md
|
@ -25,132 +25,147 @@ Getting started
|
||||||
|
|
||||||
Let's start with connecting to Redis using TCP:
|
Let's start with connecting to Redis using TCP:
|
||||||
|
|
||||||
password := "" // no password set
|
```go
|
||||||
db := int64(-1) // use default DB
|
password := "" // no password set
|
||||||
client := redis.NewTCPClient("localhost:6379", password, db)
|
db := int64(-1) // use default DB
|
||||||
defer client.Close()
|
client := redis.NewTCPClient("localhost:6379", password, db)
|
||||||
|
defer client.Close()
|
||||||
ping := client.Ping()
|
|
||||||
fmt.Println(ping.Err(), ping.Val())
|
|
||||||
// Output: <nil> PONG
|
|
||||||
|
|
||||||
|
ping := client.Ping()
|
||||||
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
|
// Output: <nil> PONG
|
||||||
|
```
|
||||||
or using Unix socket:
|
or using Unix socket:
|
||||||
|
|
||||||
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
```go
|
||||||
defer client.Close()
|
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
||||||
|
defer client.Close()
|
||||||
ping := client.Ping()
|
|
||||||
fmt.Println(ping.Err(), ping.Val())
|
|
||||||
// Output: <nil> PONG
|
|
||||||
|
|
||||||
|
ping := client.Ping()
|
||||||
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
|
// Output: <nil> PONG
|
||||||
|
```
|
||||||
Then we can start sending commands:
|
Then we can start sending commands:
|
||||||
|
|
||||||
set := client.Set("foo", "bar")
|
```go
|
||||||
fmt.Println(set.Err(), set.Val())
|
set := client.Set("foo", "bar")
|
||||||
|
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:
|
||||||
|
|
||||||
var set *redis.StatusReq
|
```go
|
||||||
var get *redis.StringReq
|
var set *redis.StatusReq
|
||||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
var get *redis.StringReq
|
||||||
set = c.Set("key1", "hello1")
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
get = c.Get("key2")
|
set = c.Set("key1", "hello1")
|
||||||
})
|
get = c.Get("key2")
|
||||||
fmt.Println(err, reqs)
|
})
|
||||||
fmt.Println(set)
|
fmt.Println(err, reqs)
|
||||||
fmt.Println(get)
|
fmt.Println(set)
|
||||||
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
fmt.Println(get)
|
||||||
// SET key1 hello1: OK
|
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
||||||
// GET key2: (nil)
|
// SET key1 hello1: OK
|
||||||
|
// GET key2: (nil)
|
||||||
|
```
|
||||||
or:
|
or:
|
||||||
|
|
||||||
var set *redis.StatusReq
|
```go
|
||||||
var get *redis.StringReq
|
var set *redis.StatusReq
|
||||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
var get *redis.StringReq
|
||||||
set = c.Set("key1", "hello1")
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
get = c.Get("key2")
|
set = c.Set("key1", "hello1")
|
||||||
})
|
get = c.Get("key2")
|
||||||
fmt.Println(err, reqs)
|
})
|
||||||
fmt.Println(set)
|
fmt.Println(err, reqs)
|
||||||
fmt.Println(get)
|
fmt.Println(set)
|
||||||
// Output: <nil> [SET key1 hello1 GET key2]
|
fmt.Println(get)
|
||||||
// SET key1 hello1
|
// Output: <nil> [SET key1 hello1 GET key2]
|
||||||
// GET key2
|
// SET key1 hello1
|
||||||
|
// GET key2
|
||||||
|
```
|
||||||
|
|
||||||
We can also send several commands in transaction:
|
We can also send several commands in transaction:
|
||||||
|
|
||||||
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
```go
|
||||||
get := multi.Get("key")
|
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
||||||
if err := get.Err(); err != nil && err != redis.Nil {
|
get := multi.Get("key")
|
||||||
return nil, err
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
multi, err := client.MultiClient()
|
val, _ := strconv.ParseInt(get.Val(), 10, 64)
|
||||||
_ = err
|
|
||||||
defer multi.Close()
|
|
||||||
|
|
||||||
watch := multi.Watch("key")
|
reqs, err := multi.Exec(func() {
|
||||||
_ = watch.Err()
|
multi.Set("key", strconv.FormatInt(val+1, 10))
|
||||||
|
})
|
||||||
|
// Transaction failed. Repeat.
|
||||||
|
if err == redis.Nil {
|
||||||
|
return transaction(multi)
|
||||||
|
}
|
||||||
|
return reqs, err
|
||||||
|
}
|
||||||
|
|
||||||
reqs, err := transaction(multi)
|
multi, err := client.MultiClient()
|
||||||
fmt.Println(err, reqs)
|
_ = err
|
||||||
|
defer multi.Close()
|
||||||
|
|
||||||
// Output: <nil> [SET key 1: OK]
|
watch := multi.Watch("key")
|
||||||
|
_ = watch.Err()
|
||||||
|
|
||||||
|
reqs, err := transaction(multi)
|
||||||
|
fmt.Println(err, reqs)
|
||||||
|
|
||||||
|
// Output: <nil> [SET key 1: OK]
|
||||||
|
```
|
||||||
|
|
||||||
To subscribe to the channel:
|
To subscribe to the channel:
|
||||||
|
|
||||||
pubsub, err := client.PubSubClient()
|
```go
|
||||||
defer pubsub.Close()
|
pubsub, err := client.PubSubClient()
|
||||||
|
defer pubsub.Close()
|
||||||
|
|
||||||
ch, err := pubsub.Subscribe("mychannel")
|
ch, err := pubsub.Subscribe("mychannel")
|
||||||
_ = err
|
_ = err
|
||||||
|
|
||||||
subscribeMsg := <-ch
|
subscribeMsg := <-ch
|
||||||
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
|
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
|
||||||
|
|
||||||
pub := client.Publish("mychannel", "hello")
|
pub := client.Publish("mychannel", "hello")
|
||||||
_ = pub.Err()
|
_ = pub.Err()
|
||||||
|
|
||||||
msg := <-ch
|
msg := <-ch
|
||||||
fmt.Println(msg.Err, msg.Message)
|
fmt.Println(msg.Err, msg.Message)
|
||||||
|
|
||||||
// Output: <nil> subscribe
|
// Output: <nil> subscribe
|
||||||
// <nil> hello
|
// <nil> hello
|
||||||
|
```
|
||||||
|
|
||||||
You can also write custom commands:
|
You can also write custom commands:
|
||||||
|
|
||||||
func Get(client *redis.Client, key string) *redis.StringReq {
|
```go
|
||||||
req := redis.NewStringReq("GET", key)
|
func Get(client *redis.Client, key string) *redis.StringReq {
|
||||||
client.Process(req)
|
req := redis.NewStringReq("GET", key)
|
||||||
return req
|
client.Process(req)
|
||||||
}
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
```
|
||||||
|
|
||||||
Client uses connection pool to send commands. You can change maximum number of connections with:
|
Client uses connection pool to send commands. You can change maximum number of connections with:
|
||||||
|
|
||||||
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
```go
|
||||||
|
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
||||||
|
```
|
||||||
|
|
||||||
Look and feel
|
Look and feel
|
||||||
-------------
|
-------------
|
||||||
|
|
Loading…
Reference in New Issue