-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
298 lines (244 loc) · 7.28 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
package smartcache
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
type ResultType int
// Result types.
const (
Miss ResultType = iota
WarmHit
HotHit
)
func (t ResultType) String() string {
switch t {
case Miss:
return "miss"
case WarmHit:
return "warmHit"
case HotHit:
return "hotHit"
default:
return ""
}
}
type Result[T any] struct {
Data *T
Type ResultType
Age time.Duration
}
// Backend can store and retrieve cache data by key.
type Backend[T any] interface {
// Get returns cache data by key.
// It should return nil if the key is not found.
Get(ctx context.Context, key string) (*CacheEntry[T], error)
// Set stores cache data by key.
// It should obey the ttl value without inspecting the entry.
Set(ctx context.Context, key string, ttl time.Duration, data *CacheEntry[T]) error
// Closes the backend.
Close()
}
// FetchFunc fetches data to be cached.
type FetchFunc[T any] func(ctx context.Context, key string) (*FetchResult[T], error)
// FetchResult is a container for cached item.
type FetchResult[T any] struct {
// Data contains the result to store in cache.
Data *T
// CreatedAt is a time when the data was fetched as fresh.
// Optional, defaults to the function call time.
CreatedAt time.Time
}
// ErrorTTLFunc defines if and for how long to cache errors returned by `FetchFunc`.
// If it returns 0, error will not be cached.
type ErrorTTLFunc func(err error) time.Duration
// BackgroundErrorHandler is a handler for `FetchFunc` errors, if they happen during a background refresh.
type BackgroundErrorHandler func(err error)
type request struct {
requests uint
lock chan struct{}
updatePending bool
}
// Cache stores the internal in-memory LRU cache and is responsible for coordinating the cache access.
type Cache[T any] struct {
backend Backend[T]
requests chan map[string]*request
config config
// ctx is the parent context of background refreshes.
// It will be closed when `Close` method is called.
ctx context.Context
ctxCancel func()
wg sync.WaitGroup
}
// New creates a new cache.
func New[T any](backend Backend[T], options ...Option) (*Cache[T], error) {
if backend == nil {
return nil, errors.New("backend is nil")
}
// Create an initial config with sane defaults.
cfg := config{
primaryTTL: time.Minute,
secondaryTTL: time.Hour,
backgroundFetchTimeout: time.Minute,
backgroundErrorHandler: func(err error) {}, // Empty function to avoid nil checks.
errorTTLFunc: func(err error) time.Duration { return 0 }, // Don't cache errors.
}
// Apply all user options.
for _, o := range options {
if err := o(&cfg); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
}
requestsCh := make(chan map[string]*request, 1)
requestsCh <- make(map[string]*request)
ctx, cancel := context.WithCancel(context.Background())
return &Cache[T]{
backend: backend,
requests: requestsCh,
config: cfg,
ctx: ctx,
ctxCancel: cancel,
}, nil
}
// Close closes the cache.
func (sc *Cache[T]) Close() {
sc.ctxCancel()
sc.wg.Wait()
}
// Get retrieves a value from the cache for a given key. If the value is not found or expired, the function fetches the data using the provided fetchFunc and updates the cache accordingly.
func (sc *Cache[T]) Get(ctx context.Context, key string, fetchFunc FetchFunc[T]) (Result[T], error) {
var result Result[T]
if err := sc.ctx.Err(); err != nil {
return result, err
}
if err := ctx.Err(); err != nil {
return result, err
}
sc.wg.Add(1)
defer sc.wg.Done()
// Obtain request with a lock, increase requests count for the key, obtain a lock for the key.
requests := <-sc.requests
req, found := requests[key]
if found {
req.requests++
} else {
req = &request{
requests: 1,
lock: make(chan struct{}, 1),
}
req.lock <- struct{}{}
requests[key] = req
}
lockCh := req.lock
sc.requests <- requests
// On finish: decrease requests count for key, remove the entry if count goes to 0, release the lock.
defer func() {
requests := <-sc.requests
requests[key].requests--
if requests[key].requests == 0 {
delete(requests, key)
}
sc.requests <- requests
lockCh <- struct{}{}
}()
<-lockCh
entry, err := sc.backend.Get(ctx, key)
if err != nil {
return result, fmt.Errorf("cache backend failed for key '%s': %w", key, err)
}
switch {
// Cached data is stale or missing, fetchFunc has to be called immmediately.
case entry == nil || entry.IsExpired(sc.config.secondaryTTL):
result.Type = Miss
result.Age = 0
fetchCtx, cancel := sc.newForegroundContext(ctx)
defer cancel()
item, err := sc.fetchToCacheEntry(fetchCtx, key, fetchFunc)
if err != nil {
return result, err
}
if err := sc.backend.Set(ctx, key, sc.config.secondaryTTL, item); err != nil {
return result, fmt.Errorf("failed to update cache for key '%s': %w", key, err)
}
result.Data = item.Data
return result, item.Err
// Cached data is fresh.
case !entry.IsExpired(sc.config.primaryTTL):
result.Type = HotHit
result.Data = entry.Data
result.Age = time.Since(entry.Created)
return result, entry.Err
// Cached data can be returned, but needs a refresh in the background.
default:
result.Type = WarmHit
result.Data = entry.Data
result.Age = time.Since(entry.Created)
requests := <-sc.requests
defer func() { sc.requests <- requests }()
if requests[key].updatePending {
// There's already a background update pending,
// the data can be returned immediately.
return result, entry.Err
}
// Initiate data refresh in the background.
requests[key].updatePending = true
requests[key].requests++
sc.wg.Add(1)
go func() {
defer sc.wg.Done()
bkgCtx, cancel := sc.newBackgroundContext()
defer cancel()
item, err := sc.fetchToCacheEntry(bkgCtx, key, fetchFunc)
if err != nil {
sc.config.backgroundErrorHandler(err)
} else {
if err = sc.backend.Set(bkgCtx, key, sc.config.secondaryTTL, item); err != nil {
sc.config.backgroundErrorHandler(fmt.Errorf("failed to update cache for key '%s': %w", key, err))
}
}
requests := <-sc.requests
requests[key].requests--
requests[key].updatePending = false
if requests[key].requests == 0 {
delete(requests, key)
}
sc.requests <- requests
}()
return result, entry.Err
}
}
func (sc *Cache[T]) fetchToCacheEntry(ctx context.Context, key string, fetchFunc FetchFunc[T]) (*CacheEntry[T], error) {
data, err := fetchFunc(ctx, key)
if err != nil {
errTTL := sc.config.errorTTLFunc(err)
if errTTL == 0 {
return newEmptyExpiredCacheEntry[T](), err
}
return newErrCacheEntry[T](err, errTTL), nil
}
created := data.CreatedAt
if created.IsZero() {
created = time.Now()
}
return newOKCacheEntry(data.Data, created), nil
}
func (sc *Cache[T]) newBackgroundContext() (ctx context.Context, cancel func()) {
if sc.config.backgroundFetchTimeout > 0 {
return context.WithTimeout(sc.ctx, sc.config.backgroundFetchTimeout)
}
return context.WithCancel(sc.ctx)
}
func (sc *Cache[T]) newForegroundContext(ctx context.Context) (fgCtx context.Context, cancel func()) {
fgCtx, cancel = context.WithCancel(ctx)
go func() {
select {
// If the cache context was cancelled, returned context should also be cancelled.
case <-sc.ctx.Done():
cancel()
case <-fgCtx.Done():
}
}()
return fgCtx, cancel
}