-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgocache_elasticsearch.go
265 lines (232 loc) · 5.08 KB
/
gocache_elasticsearch.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package cache
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/morkid/gocache"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)
// ElasticCacheConfig struct config
type ElasticCacheConfig struct {
Client *elasticsearch.Client
Index string
ExpiresIn time.Duration
}
// NewElasticCache func
func NewElasticCache(config ElasticCacheConfig) *gocache.AdapterInterface {
if nil == config.Client {
panic("Client config is required")
}
if config.Index == "" {
config.Index = "gocache"
}
if config.ExpiresIn <= 0 {
config.ExpiresIn = 3600 * time.Second
}
var adapter gocache.AdapterInterface = &elasticCache{
Client: config.Client,
Index: config.Index,
ExpiresIn: config.ExpiresIn,
}
return &adapter
}
type hit struct {
Source *documentObject `json:"_source"`
}
type hits struct {
Hits *[]hit `json:"hits"`
}
type response struct {
Hits hits `json:"hits"`
}
type documentObject struct {
Key string `json:"key"`
Value string `json:"value"`
CreatedAt time.Time `json:"created_at"`
}
type elasticCache struct {
Client *elasticsearch.Client
Index string
ExpiresIn time.Duration
}
func (e elasticCache) Set(key string, value string) error {
es := e.Client
data := documentObject{
Key: key,
Value: value,
CreatedAt: time.Now(),
}
bte, err := json.Marshal(data)
if nil != err {
return err
}
var wg sync.WaitGroup
wg.Add(1)
go func(es *elasticsearch.Client, index string, data string) {
req := esapi.IndexRequest{
Index: index,
DocumentID: key,
Body: strings.NewReader(data),
Refresh: "true",
}
res, err := req.Do(context.Background(), es)
if nil != err {
log.Println(err)
}
defer res.Body.Close()
wg.Done()
}(es, e.Index, string(bte))
wg.Wait()
return nil
}
func (e elasticCache) Get(key string) (string, error) {
result, err := e.find(key)
if nil != err {
return "", err
}
if e.isExpired(result) {
return "", errors.New("Cache expired")
}
return result.Value, nil
}
func (e elasticCache) IsValid(key string) bool {
result, err := e.Get(key)
if err == nil && result != "" {
return true
}
return false
}
func (e elasticCache) Clear(key string) error {
query := map[string]map[string]map[string]string{
"query": {
"match": {
"key": key,
},
},
}
return e.deleteByQuery(query)
}
func (e elasticCache) ClearPrefix(keyPrefix string) error {
query := map[string]map[string]map[string]string{
"query": {
"match_phrase_prefix": {
"key": keyPrefix,
},
},
}
return e.deleteByQuery(query)
}
func (e elasticCache) ClearAll() error {
query := map[string]map[string]map[string]string{
"query": {
"match_all": {},
},
}
return e.deleteByQuery(query)
}
func (e elasticCache) isExpired(source *documentObject) bool {
if nil == source {
return true
}
expired := time.Now().Sub(source.CreatedAt) > e.ExpiresIn
if expired {
var wg sync.WaitGroup
wg.Add(1)
go func(key string) {
e.Clear(key)
wg.Done()
}(source.Key)
wg.Wait()
}
return expired
}
func (e elasticCache) find(key string) (*documentObject, error) {
var search bytes.Buffer
query := map[string]map[string]map[string]string{
"query": {
"match": {
"key": key,
},
},
}
if err := json.NewEncoder(&search).Encode(query); err != nil {
return nil, err
}
es := e.Client
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex(e.Index),
es.Search.WithBody(&search),
es.Search.WithTrackTotalHits(true),
)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.IsError() {
var er map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&er); err != nil {
return nil, err
}
if nil != er["error"] {
return nil, fmt.Errorf("[%s] %s: %s",
res.Status(),
er["error"].(map[string]interface{})["type"],
er["error"].(map[string]interface{})["reason"],
)
}
}
var r response
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return nil, err
}
if nil != r.Hits.Hits && len(*r.Hits.Hits) > 0 {
h := *r.Hits.Hits
if result := h[0]; nil != result.Source {
return result.Source, nil
}
}
return nil, errors.New("Not found")
}
func (e elasticCache) deleteByQuery(query map[string]map[string]map[string]string) error {
es := e.Client
var wg sync.WaitGroup
wg.Add(1)
go func(query map[string]map[string]map[string]string) {
var search bytes.Buffer
if err := json.NewEncoder(&search).Encode(query); err != nil {
log.Println(err)
return
}
res, err := es.DeleteByQuery([]string{e.Index}, &search)
if nil != err {
log.Println(err)
return
}
defer res.Body.Close()
if res.IsError() {
var er map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&er); err != nil {
log.Println(err)
return
}
if nil != er["error"] {
log.Println(fmt.Errorf("[%s] %s: %s",
res.Status(),
er["error"].(map[string]interface{})["type"],
er["error"].(map[string]interface{})["reason"],
))
}
}
wg.Done()
}(query)
wg.Wait()
return nil
}