-
Notifications
You must be signed in to change notification settings - Fork 0
/
wfcache.go
342 lines (264 loc) · 6.83 KB
/
wfcache.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
package wfcache
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/thoas/go-funk"
)
type CacheItem struct {
Key string `json:"key"`
Value []byte `json:"value"`
ExpiresAt int64 `json:"expiresAt"`
}
type Storage interface {
TimeToLive() time.Duration
Get(ctx context.Context, key string) *CacheItem
BatchGet(ctx context.Context, keys []string) []*CacheItem
Set(ctx context.Context, key string, value []byte) error
BatchSet(ctx context.Context, pairs map[string][]byte) error
Del(ctx context.Context, key string) error
}
type StorageMaker func() (Storage, error)
type StartStorageOp func(ctx context.Context, opName string) interface{}
type FinishStorageOp func(interface{})
type Cache struct {
storages Future
startOperation StartStorageOp
finishOperation FinishStorageOp
}
var (
ErrNotFulfilled = errors.New("look up not fulfilled")
ErrPartiallyFulfilled = errors.New("look up only partially fulfilled")
)
var nosop = func(ctx context.Context, opName string) interface{} {
return nil
}
var nofop = func(input interface{}) {}
func hasDuplicates(keys []string) bool {
encountered := map[string]bool{}
for i := range keys {
if encountered[keys[i]] {
return true
} else {
encountered[keys[i]] = true
}
}
return false
}
func hasEmptyString(keys []string) bool {
for i := range keys {
if keys[i] == "" {
return true
}
}
return false
}
func New(maker StorageMaker, otherMakers ...StorageMaker) (*Cache, error) {
return NewWithHooks(
nosop,
nofop,
maker,
otherMakers...)
}
func NewWithHooks(sop StartStorageOp, fop FinishStorageOp, maker StorageMaker, otherMakers ...StorageMaker) (*Cache, error) {
var c *Cache
makers := append([]StorageMaker{maker}, otherMakers...)
c = &Cache{
startOperation: sop,
finishOperation: fop,
storages: Promise(func() (interface{}, error) {
return initializeStorages(c, makers)
}),
}
return c, nil
}
func initializeStorages(c *Cache, makers []StorageMaker) ([]Storage, error) {
storages := make([]Storage, 0, len(makers))
for _, makeStorage := range makers {
storage, err := makeStorage()
if err != nil {
return nil, fmt.Errorf(errWFCacheInitialize, err)
}
storages = append(storages, storage)
}
return storages, nil
}
func (c *Cache) Storages() ([]Storage, error) {
ss, err := c.storages.Await()
if err != nil {
return nil, err
}
storages, ok := ss.([]Storage)
if !ok {
return nil, errors.New("invalid storage")
}
return storages, nil
}
func (c *Cache) Get(key string) (*CacheItem, error) {
return c.GetWithContext(context.Background(), key)
}
func (c *Cache) GetWithContext(ctx context.Context, key string) (*CacheItem, error) {
storages, err := c.Storages()
if err != nil {
return nil, err
}
so := c.startOperation(ctx, "Get")
defer c.finishOperation(so)
missingKeyByStorage := map[Storage]string{}
// start waterfall
for _, storage := range storages {
cacheItem := storage.Get(ctx, key)
if cacheItem == nil {
missingKeyByStorage[storage] = key
continue
} else {
// prime previous storages
for s := range missingKeyByStorage {
s.Set(ctx, key, cacheItem.Value)
}
}
// value := interface{}
// err := json.Unmarshal(cacheItem.Value, value)
// if err != nil {
// return nil, err
// }
return cacheItem, nil
}
return nil, ErrNotFulfilled
}
func (c *Cache) BatchGet(keys []string) ([]*CacheItem, error) {
return c.BatchGetWithContext(context.Background(), keys)
}
func (c *Cache) BatchGetWithContext(ctx context.Context, keys []string) ([]*CacheItem, error) {
if hasDuplicates(keys) {
return nil, errors.New("duplicated keys are not allowed")
}
if hasEmptyString(keys) {
return nil, errors.New("empty keys are not allowed")
}
storages, err := c.Storages()
if err != nil {
return nil, err
}
so := c.startOperation(ctx, "BatchGet")
defer c.finishOperation(so)
if len(keys) == 0 {
return nil, errors.New("at least one key is required")
}
missingKeys := keys
cacheItems := []*CacheItem{}
missingKeysByStorage := map[Storage][]string{}
// start waterfall
for _, storage := range storages {
mds := storage.BatchGet(ctx, missingKeys)
if len(mds) != 0 {
resolvedKeys := funk.Map(mds, func(md *CacheItem) string {
return md.Key
}).([]string)
mKeys1, mKeys2 := funk.DifferenceString(resolvedKeys, missingKeys)
missingKeys = append(mKeys1, mKeys2...)
cacheItems = append(cacheItems, mds...)
}
if len(missingKeys) == 0 {
break
}
missingKeysByStorage[storage] = missingKeys
}
// for _, cacheItem := range cacheItems {
// if cacheItem != nil {
// var m interface{}
// json.Unmarshal(cacheItem.Value, &m)
// *values = append(*values, m)
// }
// }
if len(cacheItems) == 0 {
return nil, ErrNotFulfilled
}
// prime previous storages
for s, misses := range missingKeysByStorage {
missedValues := map[string][]byte{}
missedCacheItems := funk.Filter(cacheItems, func(md *CacheItem) bool {
return funk.ContainsString(misses, md.Key)
}).([]*CacheItem)
for _, m := range missedCacheItems {
missedValues[m.Key] = m.Value
}
if len(missedValues) != 0 {
s.BatchSet(ctx, missedValues)
}
}
if len(missingKeys) != 0 {
return cacheItems, ErrPartiallyFulfilled
}
return cacheItems, nil
}
func (c *Cache) Set(key string, value interface{}) error {
return c.SetWithContext(context.Background(), key, value)
}
func (c *Cache) SetWithContext(ctx context.Context, key string, value interface{}) error {
storages, err := c.Storages()
if err != nil {
return err
}
so := c.startOperation(ctx, "Set")
defer c.finishOperation(so)
v, err := json.Marshal(value)
if err != nil {
return err
}
for _, storage := range storages {
err := storage.Set(ctx, key, v)
if err != nil {
return err
}
}
return nil
}
func (c *Cache) BatchSet(pairs map[string]interface{}) error {
return c.BatchSetWithContext(context.Background(), pairs)
}
func (c *Cache) BatchSetWithContext(ctx context.Context, pairs map[string]interface{}) error {
storages, err := c.Storages()
if err != nil {
return err
}
so := c.startOperation(ctx, "BatchSet")
defer c.finishOperation(so)
vPairs := map[string][]byte{}
for key, value := range pairs {
v, err := json.Marshal(value)
if err != nil {
return err
}
vPairs[key] = v
}
for _, storage := range storages {
err := storage.BatchSet(ctx, vPairs)
if err != nil {
return err
}
}
return nil
}
func (c *Cache) Del(key string) error {
return c.DelWithContext(context.Background(), key)
}
func (c *Cache) DelWithContext(ctx context.Context, key string) error {
storages, err := c.Storages()
if err != nil {
return err
}
so := c.startOperation(ctx, "Del")
defer c.finishOperation(so)
for _, storage := range storages {
err := storage.Del(ctx, key)
if err != nil {
return err
}
}
return nil
}
const errWFCacheInitialize = `error: %s
wfcache failed to initialize`