-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
85 lines (63 loc) · 1.39 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"github.com/bradfitz/gomemcache/memcache"
)
type Person struct {
ID string `json:"id"`
Name string `json:"name"`
}
func (p *Person) JSON() ([]byte, error) {
return json.Marshal(p)
}
func main() {
mc := memcache.New("127.0.0.1:11211")
setSimpleValue(mc)
setMultipleValue(mc)
setJSONValue(mc)
}
func setSimpleValue(mc *memcache.Client) {
err := mc.Set(&memcache.Item{Key: "a", Value: []byte("wury")})
if err != nil {
fmt.Println(err)
}
val, err := mc.Get("a")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(val.Value))
}
func setMultipleValue(mc *memcache.Client) {
var err error
err = mc.Set(&memcache.Item{Key: "1", Value: []byte("wury")})
err = mc.Set(&memcache.Item{Key: "2", Value: []byte("yanto")})
if err != nil {
fmt.Println(err)
}
vals, err := mc.GetMulti([]string{"1", "2"})
if err != nil {
fmt.Println(err)
}
for _, v := range vals {
fmt.Println(string(v.Value))
}
}
func setJSONValue(mc *memcache.Client) {
person := &Person{ID: "U1", Name: "Wuriyanto"}
personJSON, _ := person.JSON()
err := mc.Set(&memcache.Item{Key: person.ID, Value: personJSON})
if err != nil {
fmt.Println(err)
}
val, err := mc.Get("U1")
if err != nil {
fmt.Println(err)
}
var personResult Person
err = json.Unmarshal(val.Value, &personResult)
if err != nil {
fmt.Println(err)
}
fmt.Println(personResult)
}