-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
98 lines (75 loc) · 1.87 KB
/
client.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
86
87
88
89
90
91
92
93
94
95
96
97
98
package gocache
import (
"context"
"encoding/json"
"google.golang.org/grpc"
)
type AddCacheRequest struct {
Key string
Value interface{}
Life int64 //as second
}
func GetCache(address, key string, cacheResponse interface{}) error {
cc, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return err
}
defer cc.Close()
client := NewCacheServiceClient(cc)
request := &GetCacheRequest{Key: key}
resp, clientError := client.GetCache(context.Background(), request)
if clientError != nil {
return clientError
}
marshallError := json.Unmarshal(resp.Value, &cacheResponse)
if marshallError != nil {
return marshallError
}
return nil
}
func SaveCache(address string, cache AddCacheRequest) error {
cc, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return err
}
defer cc.Close()
client := NewCacheServiceClient(cc)
cacheBytes, bodyError := json.Marshal(&cache.Value)
if bodyError != nil {
return bodyError
}
request := &SaveCacheRequest{Key: cache.Key, Value: cacheBytes, Life: cache.Life}
_, clientError := client.SaveCache(context.Background(), request)
if clientError != nil {
return clientError
}
return nil
}
func ClearCache(address string) error {
cc, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return err
}
defer cc.Close()
client := NewCacheServiceClient(cc)
request := &Empty{}
_, clientError := client.ClearCache(context.Background(), request)
if clientError != nil {
return clientError
}
return nil
}
func DeleteCacheItem(address string, key string) error {
cc, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return err
}
defer cc.Close()
client := NewCacheServiceClient(cc)
request := &DeleteCacheItemRequest{Key: key}
_, clientError := client.DeleteCachedItem(context.Background(), request)
if clientError != nil {
return clientError
}
return nil
}