-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
330 lines (296 loc) · 7.34 KB
/
cache.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
package cache
import (
"encoding/json"
"errors"
"fmt"
"io"
"time"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
)
const (
// PriorityMedium is a medium level storage priority
PriorityMedium Priority = iota
// PriorityHigh is a high level storage priority
PriorityHigh
)
var (
// ErrKeyNotExist indicates that the key does not exist in the storage
ErrKeyNotExist = errors.New("key does not exist")
// ErrNotJSONMarshalable indicates that the content is not json marshalable
ErrNotJSONMarshalable = errors.New("value is not json marshalable")
)
// Cache manages to Set, Get, Delet and Tag keys
type Cache struct {
storage map[Priority][]Storage
logger *logrus.Logger
tagger Tagger
ns string
}
// New constructs a new Cache instance which can store, read
// and remove items with tags
func New(options ...Option) *Cache {
c := &Cache{
logger: logrus.New(),
storage: make(map[Priority][]Storage),
}
options = append(
[]Option{
WithTagger(newStdTagger(c.logger, "go:cache:tagger")),
WithNamespace("go:cache"),
},
options...,
)
for i := range options {
options[i](c)
}
return c
}
// NsKey wraps the given key with the namespace prefix
func (c *Cache) NsKey(key string) string {
return fmt.Sprintf("%s:%s", c.ns, key)
}
// Loop iterates through registered high and medium storage and pass them to the
// coressponding function to use
func (c *Cache) Loop(high func(s Storage) (bool, error), medium func(s Storage) (bool, error)) (e error) {
if medium == nil {
medium = high
}
highPriority, _ := c.storage[PriorityHigh]
for _, storage := range highPriority {
if terminate, err := high(storage); err != nil {
e = multierror.Append(e, err)
} else if terminate {
return nil
}
}
mediumPriority, _ := c.storage[PriorityMedium]
for _, storage := range mediumPriority {
terminate, _ := medium(storage)
if terminate {
return nil
}
}
return
}
// Set stores a value into configured stores expiration and tags list
// Zero expiration means the key has no expiration time. Additionally,
// all string argument passed after expiration will be used to tag the value
// It ignores any error occurred for medium level storage
func (c *Cache) Set(key string, v interface{}, expiration time.Duration, tags ...string) error {
return c.set(key, v, expiration, tags...)
}
func (c *Cache) set(key string, v interface{}, expiration time.Duration, tags ...string) (err error) {
item := item{Key: key, Val: v, Created: time.Now(), Expires: expiration}
return c.Loop(
func(s Storage) (bool, error) {
return false, c.write(s, key, item, expiration, tags...)
},
nil,
)
}
func (c *Cache) write(s Storage, key string, v interface{}, expiration time.Duration, tags ...string) error {
if err := s.Write(c.NsKey(key), v, expiration); err != nil {
return err
}
if len(tags) > 0 {
if err := c.tagger.Tag(s, key, tags...); err != nil {
return err
}
}
return nil
}
// Get reads for the given key from the registered storage unless
// a valid content is received. It will ignore any error occurred
// for medium level storage
func (c *Cache) Get(key string, out interface{}) error { return c.get(key, out) }
func (c *Cache) get(key string, out interface{}) error {
var (
p []Storage
it *item
w Storage
)
err := c.Loop(
func(s Storage) (bool, error) {
item, err := c.read(s, key)
if err != nil {
if ErrKeyNotExist == err {
p = append(p, s)
}
return false, err
}
it = item
w = s
if err := mapstructure.Decode(item.Val, out); err != nil {
return false, err
}
return true, nil
},
func(s Storage) (bool, error) {
item, err := c.read(s, key)
if err != nil {
return false, err
}
it = item
w = s
if err := mapstructure.Decode(item.Val, out); err != nil {
return false, err
}
return true, nil
},
)
if it != nil {
for _, s := range p {
c.propagate(s, w, it, key)
}
}
return err
}
func (c *Cache) read(s Storage, key string) (*item, error) {
v, err := s.Read(c.NsKey(key))
if err != nil {
return nil, err
}
var cacheItem = &item{}
switch x := v.(type) {
case item:
cacheItem = &x
case string:
json.Unmarshal([]byte(x), cacheItem)
case []byte:
json.Unmarshal(x, cacheItem)
}
if cacheItem.expired() {
return nil, c.del(c.NsKey(key))
}
return cacheItem, nil
}
// Del deletes the given key from all registered storage
func (c *Cache) Del(keys ...string) error { return c.del(keys...) }
func (c *Cache) del(keys ...string) error {
return c.Loop(
func(s Storage) (bool, error) {
for _, key := range keys {
if err := s.Delete(c.NsKey(key)); err != nil {
return false, err
}
if err := c.tagger.UnTag(s, key); err != nil {
return false, err
}
}
return false, nil
},
nil,
)
}
// Extend sets the new expiration time for the given key
// If the expiration has not initially been set this method
// will add one
func (c *Cache) Extend(key string, expiration time.Duration) error {
return c.Loop(
func(s Storage) (bool, error) {
v, err := s.Read(c.NsKey(key))
if err != nil {
return false, err
}
it := v.(item)
it.Created = time.Now()
it.Expires = expiration
return false, s.Write(key, it, expiration)
},
nil,
)
}
// Propagate propagates all the given keys from s1 data storage
// into s2 data storage
func (c *Cache) propagate(s1, s2 Storage, it *item, keys ...string) error {
for _, key := range keys {
tags, err := c.tagger.Tags(s2, key)
if err != nil {
return err
}
New(WithStorage(s1), WithNamespace(c.ns)).Set(key, it.Val, it.Expires-(time.Now().Sub(it.Created)), tags...)
}
return nil
}
// Flush flushes all the data in all registered storage
func (c *Cache) Flush() (err error) {
return c.Loop(
func(s Storage) (bool, error) {
return false, s.Flush()
},
nil,
)
}
// Close closes the storage resource if it implements io.Closer
func (c *Cache) Close() (err error) {
return c.Loop(
func(s Storage) (bool, error) {
if closer, ok := s.(io.Closer); ok {
return false, closer.Close()
}
return false, nil
},
nil,
)
}
// ByTag reads tagged values into `out`
// `out` is always a slice of values
func (c *Cache) ByTag(tag string, out interface{}) error {
if out == nil {
out = make([]interface{}, 0, 0)
}
return c.Loop(func(s Storage) (bool, error) {
keys, err := c.tagger.Keys(s, tag)
if err != nil {
return false, err
}
output := make([]interface{}, 0, len(keys))
for _, key := range keys {
v, errRead := s.Read(c.NsKey(key))
if errRead != nil {
err = multierror.Append(err, errRead)
}
it := c.item(v)
if it.Val != nil {
output = append(output, it.Val)
}
}
return true, mapstructure.Decode(output, out)
}, nil)
}
// DelByTag deletes tagged values
func (c *Cache) DelByTag(tags ...string) error {
return c.Loop(
func(s Storage) (bool, error) {
for _, tag := range tags {
keys, err := c.tagger.Keys(s, tag)
if err != nil {
return false, err
}
var slice = make([]string, len(keys))
copy(slice, keys)
if err := c.Del(slice...); err != nil {
return false, err
}
}
return false, nil
},
nil,
)
}
func (c *Cache) item(v interface{}) item {
var i item
switch x := v.(type) {
case item:
return x
case []byte:
json.Unmarshal(x, &i)
case string:
json.Unmarshal([]byte(x), &i)
default:
mapstructure.Decode(v, &i)
}
return i
}