-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcacheme.go
357 lines (315 loc) · 7.11 KB
/
cacheme.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package cacheme
import (
"bytes"
"context"
"embed"
"errors"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"text/template"
"time"
"github.com/go-redis/redis/v8"
"github.com/vmihailenco/msgpack/v5"
)
//go:embed template/*
var templateDir embed.FS
var varRegex = regexp.MustCompile(`{{.([a-zA-Z0-9]+)}}`)
type RedisClient interface {
PSubscribe(ctx context.Context, channels ...string) *redis.PubSub
redis.Cmdable
}
type CacheStore interface {
}
func writeTo(name string, tvar templateVar, path string) error {
funcMap := template.FuncMap{
"FirstLower": firstLower,
}
tmpl, err := template.New(name).Funcs(funcMap).ParseFS(
templateDir, fmt.Sprintf("template/%s", name),
)
if err != nil {
return err
}
b := &bytes.Buffer{}
err = tmpl.Execute(b, tvar)
if err != nil {
return err
}
var buf []byte
if buf, err = format.Source(b.Bytes()); err != nil {
fmt.Println("formating:", err)
fmt.Println(b.String())
return err
}
if err = ioutil.WriteFile(path, buf, 0644); err != nil { //nolint
return err
}
return nil
}
type CachePipeline struct {
Pipeline redis.Pipeliner
Wg *sync.WaitGroup
Executed chan bool
}
func (p *CachePipeline) Execute(ctx context.Context) error {
_, err := p.Pipeline.Exec(ctx)
if err != nil {
fmt.Println(err)
}
close(p.Executed)
p.Wg.Wait()
return nil
}
func NewPipeline(client RedisClient) *CachePipeline {
return &CachePipeline{
Pipeline: client.Pipeline(),
Wg: &sync.WaitGroup{},
Executed: make(chan bool),
}
}
type StoreSchema struct {
Name string
Key string
To interface{}
Version interface{}
Vars []string
TTL time.Duration
Singleflight bool
MetaData bool
}
type storeInfo struct {
StoreSchema
Type string
VersionInfo *versionInfo
Imports map[string]string
}
func (s *StoreSchema) SetVars(a []string) {
s.Vars = a
}
func firstLower(s string) string {
if len(s) == 0 {
return s
}
return strings.ToLower(s[:1]) + s[1:]
}
type templateVar struct {
Stores []storeInfo
Imports []string
Prefix string
}
func pkgPath(t reflect.Type) string {
pkg := t.PkgPath()
if pkg != "" {
return pkg
}
switch t.Kind() {
case reflect.Slice, reflect.Array, reflect.Ptr:
return pkgPath(t.Elem())
case reflect.Map:
return pkgPath(t.Key()) + "|" + pkgPath(t.Elem())
}
return pkg
}
type versionInfo struct {
typ string
}
func (v *versionInfo) IsFunc() bool {
return v.typ == "func"
}
func (v *versionInfo) IsString() bool {
return v.typ == "str"
}
func (v *versionInfo) IsInt() bool {
return v.typ == "int"
}
func getVersionInfo(i interface{}) (*versionInfo, error) {
typ := reflect.TypeOf(i)
if typ.Kind() == reflect.String {
return &versionInfo{typ: "str"}, nil
}
if typ.Kind() == reflect.Int {
return &versionInfo{typ: "int"}, nil
}
if typ.Kind() != reflect.Func {
return nil, errors.New("version type not supported")
}
if typ.NumIn() != 0 || typ.NumOut() != 1 || typ.Out(0).Kind() != reflect.String {
return nil, errors.New("version function not valid")
}
return &versionInfo{typ: "func"}, nil
}
func SchemaToStore(pkg string, path string, prefix string, stores []*StoreSchema) error {
patternMapping := make(map[string]bool)
nameMapping := make(map[string]bool)
var info []storeInfo
var baseImports = []string{pkg}
for _, s := range stores {
vars := []string{}
kt := s.Key
importMap := make(map[string]string)
version, err := getVersionInfo(s.Version)
if err != nil {
return err
}
if len(baseImports) == 1 && version.IsInt() {
baseImports = append(baseImports, "strconv")
}
if n, ok := nameMapping[s.Name]; ok {
fmt.Println("find duplicate name", n)
return errors.New("find duplicate name")
}
nameMapping[s.Name] = true
pattern := varRegex.ReplaceAllString(kt, "{}")
if _, ok := patternMapping[pattern]; ok {
fmt.Println("find duplicate pattern", pattern)
return errors.New("find duplicate pattern")
}
patternMapping[pattern] = true
matches := varRegex.FindAllStringSubmatch(kt, -1)
for _, v := range matches {
vars = append(vars, v[1])
}
s.SetVars(vars)
tmp := strings.Split(s.Name, "")
s.Name = strings.ToUpper(tmp[0]) + strings.Join(tmp[1:], "")
t := reflect.TypeOf(s.To)
path := pkgPath(t)
s := storeInfo{
StoreSchema: *s,
Type: t.String(),
VersionInfo: version,
}
all := strings.Split(path, "|")
for _, p := range all {
if p != "" {
importMap[p] = ""
}
}
s.Imports = importMap
info = append(info, s)
}
// prepare dir
abs, err := filepath.Abs(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dir := filepath.Dir(abs)
err = os.RemoveAll(dir + "/store")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = os.MkdirAll(dir+"/store", os.ModePerm)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate store.go
storePath := strings.TrimSuffix(pkg, "schema") + "store"
err = writeTo("client.tmpl", templateVar{
Prefix: prefix,
Imports: []string{storePath},
}, dir+"/store.go")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate base.go
err = writeTo("base.tmpl", templateVar{
Imports: baseImports,
Stores: info,
}, dir+"/store/base.go")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate stores
for _, schema := range info {
imports := []string{}
for k := range schema.Imports {
imports = append(imports, k)
}
fullPath := fmt.Sprintf("%s/store/%s.go", dir, strings.ToLower(schema.Name))
err = writeTo("store.tmpl", templateVar{
Stores: []storeInfo{schema},
Prefix: prefix,
Imports: imports,
}, fullPath)
if err != nil {
return err
}
}
return nil
}
func InvalidAll(ctx context.Context, group string, client RedisClient) error {
iter := client.SScan(ctx, group, 0, "", 200).Iterator()
invalids := []string{}
for iter.Next(ctx) {
invalids = append(invalids, iter.Val())
if len(invalids) == 600 {
err := client.Unlink(ctx, invalids...).Err()
if err != nil {
fmt.Println(err)
}
invalids = []string{}
}
}
if len(invalids) > 0 {
err := client.Unlink(ctx, invalids...).Err()
if err != nil {
return err
}
err = client.Unlink(ctx, group).Err()
return err
}
return nil
}
func InvalidAllCluster(ctx context.Context, group string, client RedisClient) error {
clusterClient := client.(*redis.ClusterClient)
iter := clusterClient.SScan(ctx, group, 0, "", 200).Iterator()
counter := 0
var keys []string
for iter.Next(ctx) {
key := iter.Val()
keys = append(keys, key)
counter++
if counter == 600 {
pipeline := clusterClient.Pipeline()
for _, key := range keys {
pipeline.Unlink(ctx, key)
}
_, err := pipeline.Exec(ctx)
if err != nil {
return err
}
counter = 0
keys = []string{}
}
}
if counter > 0 {
pipeline := clusterClient.Pipeline()
for _, key := range keys {
pipeline.Unlink(ctx, key)
}
_, err := pipeline.Exec(ctx)
if err != nil {
return err
}
}
err := clusterClient.Unlink(ctx, group).Err()
return err
}
func Marshal(v interface{}) ([]byte, error) {
return msgpack.Marshal(v)
}
func Unmarshal(data []byte, v interface{}) error {
return msgpack.Unmarshal(data, v)
}