-
Notifications
You must be signed in to change notification settings - Fork 7
/
JSONDocument.go
53 lines (45 loc) · 1.02 KB
/
JSONDocument.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
package main
import (
"context"
"fmt"
"github.com/alibaba/tair-go/tair"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
var tairClient *tair.TairClient
func init() {
tairClient = tair.NewTairClient(&redis.Options{
Addr: "***.redis.rds.aliyuncs.com:6379",
Password: "xxx",
DB: 0,
})
}
type JSONDocument struct {
// field
}
func (l *JSONDocument) jsonSave(key, path, json string) bool {
result, err := tairClient.JsonSet(ctx, key, path, json).Result()
if err != nil {
// process err
//panic(err)
}
if result == "OK" {
return true
}
return false
}
func (l *JSONDocument) jsonGet(key, path string) string {
result, err := tairClient.JsonGetPath(ctx, key, path).Result()
if err != nil {
// process err
//panic(err)
}
return result
}
func main() {
key := "JSONDocument"
target := JSONDocument{}
target.jsonSave(key, ".", "{\"name\":\"tom\",\"age\":22,\"description\":\"A man with a blue "+
"lightsaber\",\"friends\":[]}")
fmt.Println(target.jsonGet(key, ".description"))
}