mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
Merge pull request #10 from gmcintire/master
Update README to enable syntax highlighting
This commit is contained in:
commit
dc90e359e9
21
README.md
21
README.md
@ -25,6 +25,7 @@ Getting started
|
|||||||
|
|
||||||
Let's start with connecting to Redis using TCP:
|
Let's start with connecting to Redis using TCP:
|
||||||
|
|
||||||
|
```go
|
||||||
password := "" // no password set
|
password := "" // no password set
|
||||||
db := int64(-1) // use default DB
|
db := int64(-1) // use default DB
|
||||||
client := redis.NewTCPClient("localhost:6379", password, db)
|
client := redis.NewTCPClient("localhost:6379", password, db)
|
||||||
@ -33,18 +34,20 @@ Let's start with connecting to Redis using TCP:
|
|||||||
ping := client.Ping()
|
ping := client.Ping()
|
||||||
fmt.Println(ping.Err(), ping.Val())
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
// Output: <nil> PONG
|
// Output: <nil> PONG
|
||||||
|
```
|
||||||
or using Unix socket:
|
or using Unix socket:
|
||||||
|
|
||||||
|
```go
|
||||||
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
ping := client.Ping()
|
ping := client.Ping()
|
||||||
fmt.Println(ping.Err(), ping.Val())
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
// Output: <nil> PONG
|
// Output: <nil> PONG
|
||||||
|
```
|
||||||
Then we can start sending commands:
|
Then we can start sending commands:
|
||||||
|
|
||||||
|
```go
|
||||||
set := client.Set("foo", "bar")
|
set := client.Set("foo", "bar")
|
||||||
fmt.Println(set.Err(), set.Val())
|
fmt.Println(set.Err(), set.Val())
|
||||||
|
|
||||||
@ -53,9 +56,11 @@ Then we can start sending commands:
|
|||||||
|
|
||||||
// Output: <nil> OK
|
// Output: <nil> OK
|
||||||
// <nil> bar
|
// <nil> bar
|
||||||
|
```
|
||||||
|
|
||||||
We can also pipeline two commands together:
|
We can also pipeline two commands together:
|
||||||
|
|
||||||
|
```go
|
||||||
var set *redis.StatusReq
|
var set *redis.StatusReq
|
||||||
var get *redis.StringReq
|
var get *redis.StringReq
|
||||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
@ -68,9 +73,10 @@ We can also pipeline two commands together:
|
|||||||
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
||||||
// SET key1 hello1: OK
|
// SET key1 hello1: OK
|
||||||
// GET key2: (nil)
|
// GET key2: (nil)
|
||||||
|
```
|
||||||
or:
|
or:
|
||||||
|
|
||||||
|
```go
|
||||||
var set *redis.StatusReq
|
var set *redis.StatusReq
|
||||||
var get *redis.StringReq
|
var get *redis.StringReq
|
||||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
@ -83,9 +89,11 @@ or:
|
|||||||
// Output: <nil> [SET key1 hello1 GET key2]
|
// Output: <nil> [SET key1 hello1 GET key2]
|
||||||
// SET key1 hello1
|
// SET key1 hello1
|
||||||
// GET key2
|
// GET key2
|
||||||
|
```
|
||||||
|
|
||||||
We can also send several commands in transaction:
|
We can also send several commands in transaction:
|
||||||
|
|
||||||
|
```go
|
||||||
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
||||||
get := multi.Get("key")
|
get := multi.Get("key")
|
||||||
if err := get.Err(); err != nil && err != redis.Nil {
|
if err := get.Err(); err != nil && err != redis.Nil {
|
||||||
@ -115,9 +123,11 @@ We can also send several commands in transaction:
|
|||||||
fmt.Println(err, reqs)
|
fmt.Println(err, reqs)
|
||||||
|
|
||||||
// Output: <nil> [SET key 1: OK]
|
// Output: <nil> [SET key 1: OK]
|
||||||
|
```
|
||||||
|
|
||||||
To subscribe to the channel:
|
To subscribe to the channel:
|
||||||
|
|
||||||
|
```go
|
||||||
pubsub, err := client.PubSubClient()
|
pubsub, err := client.PubSubClient()
|
||||||
defer pubsub.Close()
|
defer pubsub.Close()
|
||||||
|
|
||||||
@ -135,9 +145,11 @@ To subscribe to the channel:
|
|||||||
|
|
||||||
// Output: <nil> subscribe
|
// Output: <nil> subscribe
|
||||||
// <nil> hello
|
// <nil> hello
|
||||||
|
```
|
||||||
|
|
||||||
You can also write custom commands:
|
You can also write custom commands:
|
||||||
|
|
||||||
|
```go
|
||||||
func Get(client *redis.Client, key string) *redis.StringReq {
|
func Get(client *redis.Client, key string) *redis.StringReq {
|
||||||
req := redis.NewStringReq("GET", key)
|
req := redis.NewStringReq("GET", key)
|
||||||
client.Process(req)
|
client.Process(req)
|
||||||
@ -147,10 +159,13 @@ You can also write custom commands:
|
|||||||
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:
|
||||||
|
|
||||||
|
```go
|
||||||
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
||||||
|
```
|
||||||
|
|
||||||
Look and feel
|
Look and feel
|
||||||
-------------
|
-------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user