Skip to content

Commit

Permalink
chore: add godoc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Jan 1, 2022
1 parent 57c5fca commit bc84a73
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,17 @@ func (c *client) Close() {
c.CloseFn()
}
}

func ExampleLua_exec() {
client, err := NewClient(ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer client.Close()

ctx := context.Background()

script := NewLuaScript("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}")

script.Exec(ctx, client, []string{"k1", "k2"}, []string{"a1", "a2"}).ToArray()
}
32 changes: 32 additions & 0 deletions pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rueidis
import (
"context"
"errors"
"fmt"
"log"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -84,3 +85,34 @@ func TestClusterClientPubSubReconnect(t *testing.T) {
t.Fatalf("errs count should be 1")
}
}

func ExampleNewPubSubOption_subscribe() {
messages := make(chan string, 100)

ctx := context.Background()

client, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:6379"},
PubSubOption: NewPubSubOption(func(prev error, client DedicatedClient) {
if prev != nil {
fmt.Printf("auto reconnected, previous err: %v\n", prev)
}
// do subscribe here
client.Do(ctx, client.B().Subscribe().Channel("ch").Build())
}, PubSubHandler{
OnMessage: func(channel, message string) {
// Users should avoid OnMessage blocking too long,
// otherwise Client performance will decrease.
messages <- message
},
}),
})
if err != nil {
panic(err)
}
defer client.Close()

for msg := range messages {
fmt.Println(msg)
}
}
89 changes: 89 additions & 0 deletions rueidis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package rueidis

import (
"bufio"
"context"
"fmt"
"net"
"testing"
"time"

"github.com/rueian/rueidis/internal/proto"
)
Expand Down Expand Up @@ -95,3 +98,89 @@ func TestIsRedisNil(t *testing.T) {
t.Fatal("IsRedisNil fail")
}
}

func ExampleIsRedisNil() {
client, err := NewClient(ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer client.Close()

_, err = client.Do(context.Background(), client.B().Get().Key("not_exists").Build()).ToString()
if err != nil && IsRedisNil(err) {
fmt.Printf("it is a nil response")
}
}

func ExampleClient_do() {
client, err := NewClient(ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer client.Close()

ctx := context.Background()

client.Do(ctx, client.B().Set().Key("k").Value("v").Build()).Error()

client.Do(ctx, client.B().Get().Key("k").Build()).ToString()

client.Do(ctx, client.B().Hmget().Key("h").Field("a", "b").Build()).ToMap()

client.Do(ctx, client.B().Scard().Key("s").Build()).ToInt64()

client.Do(ctx, client.B().Smembers().Key("s").Build()).ToArray()
}

func ExampleClient_doCache() {
client, err := NewClient(ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer client.Close()

ctx := context.Background()

client.DoCache(ctx, client.B().Get().Key("k").Cache(), time.Minute).ToString()

client.DoCache(ctx, client.B().Hmget().Key("h").Field("a", "b").Cache(), time.Minute).ToMap()

client.DoCache(ctx, client.B().Scard().Key("s").Cache(), time.Minute).ToInt64()

client.DoCache(ctx, client.B().Smembers().Key("s").Cache(), time.Minute).ToArray()
}

func ExampleClient_dedicated() {
client, err := NewClient(ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer client.Close()

ctx := context.Background()

client.Dedicated(func(client DedicatedClient) error {
// watch keys first
if err := client.Do(ctx, client.B().Watch().Key("k1", "k2").Build()).Error(); err != nil {
return err
}
// perform read here
values, err := client.Do(ctx, client.B().Mget().Key("k1", "k2").Build()).ToArray()
if err != nil {
return err
}
// perform write with MULTI EXEC
for _, resp := range client.DoMulti(
ctx,
client.B().Multi().Build(),
client.B().Set().Key("k1").Value(values[0].String).Build(),
client.B().Set().Key("k2").Value(values[1].String).Build(),
client.B().Exec().Build(),
) {
if err := resp.Error(); err != nil {
return err
}
}
return nil
})
}

0 comments on commit bc84a73

Please sign in to comment.