-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
70 lines (57 loc) · 1.56 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package rejonson_test
import (
"encoding/json"
"fmt"
"github.com/KromDaniel/rejonson"
"github.com/go-redis/redis"
"time"
)
func init() {
goRedisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
goRedisClient.Del("go-redis-cmd", "rejson-cmd", "rejson-cmd-pipeline", "go-redis-pipeline-command")
_ = goRedisClient.Close()
}
func ExampleExtendClient() {
goRedisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
client := rejonson.ExtendClient(goRedisClient)
defer client.Close()
arr := []interface{}{"hello", "world", 1, map[string]interface{}{"key": 12}}
js, err := json.Marshal(arr)
if err != nil {
// handle
}
// redis "native" command
client.Set("go-redis-cmd", "hello", time.Second)
client.JsonSet("rejson-cmd", ".", string(js))
// int command
arrLen, err := client.JsonArrLen("rejson-cmd", ".").Result()
if err != nil {
// handle
}
fmt.Printf("Array length: %d", arrLen)
// Output: Array length: 4
}
func ExampleExtendPipeline() {
goRedisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
client := rejonson.ExtendClient(goRedisClient)
pipeline := client.Pipeline()
pipeline.JsonSet("rejson-cmd-pipeline", ".", "[10]")
pipeline.JsonNumMultBy("rejson-cmd-pipeline", "[0]", 10)
pipeline.Set("go-redis-pipeline-command", "hello from go-redis", time.Second)
_, err := pipeline.Exec()
if err != nil {
// handle error
}
jsonString, err := client.JsonGet("rejson-cmd-pipeline").Result()
if err != nil {
// handle error
}
fmt.Printf("Array %s", jsonString)
// Output: Array [100]
}