-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacher.go
339 lines (266 loc) · 9.23 KB
/
cacher.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
/*
* Author: fasion
* Created time: 2023-11-24 14:46:12
* Last Modified by: fasion
* Last Modified time: 2024-01-30 11:40:28
*/
package stl
import (
"context"
"sync"
"time"
"go.uber.org/zap"
)
type CachedDataFetcherFetchFunc[Data any] func(ctx context.Context, expires time.Duration) (Data, time.Time, error)
type CachedDataFetcherFetchFuncLite[Data any] func(ctx context.Context) (Data, error)
type CachedDataFetcherCallbackFunc[Data any] func(context.Context, Data, time.Time)
type CachedDataFetcherCallback[Data any] struct {
callback CachedDataFetcherCallbackFunc[Data]
timeout time.Duration
}
func NewCachedDataFetcherCallback[Data any](callback func(context.Context, Data, time.Time), timeout time.Duration) *CachedDataFetcherCallback[Data] {
return &CachedDataFetcherCallback[Data]{
callback: callback,
timeout: timeout,
}
}
func (callback *CachedDataFetcherCallback[Data]) Call(data Data, lastFetchTime time.Time) {
go callback.call(data, lastFetchTime)
}
func (callback *CachedDataFetcherCallback[Data]) call(data Data, lastFetchTime time.Time) {
var cancel context.CancelFunc
ctx := context.Background()
if callback.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, callback.timeout)
defer cancel()
}
callback.callback(ctx, data, lastFetchTime)
return
}
type CachedDataFetcherCallbacks[Data any] []*CachedDataFetcherCallback[Data]
func (callbacks CachedDataFetcherCallbacks[Data]) Append(others ...*CachedDataFetcherCallback[Data]) CachedDataFetcherCallbacks[Data] {
return append(callbacks, others...)
}
func (callbacks CachedDataFetcherCallbacks[Data]) Call(data Data, lastFetchTime time.Time) {
for _, callback := range callbacks {
callback.Call(data, lastFetchTime)
}
}
type TimedValue[Value any] struct {
value Value
t time.Time
}
func NewTimedValue[Value any](value Value, t time.Time) *TimedValue[Value] {
return &TimedValue[Value]{
value: value,
t: t,
}
}
func (timed *TimedValue[Value]) Value() (value Value) {
if timed == nil {
return
}
return timed.value
}
func (timed *TimedValue[Value]) Time() (t time.Time) {
if timed == nil {
return
}
return timed.t
}
func (timed *TimedValue[Value]) ValueAndTime() (value Value, t time.Time) {
if timed == nil {
return
}
return timed.value, timed.t
}
type CachedDataFetcher[Data any] struct {
*zap.Logger
fetcher CachedDataFetcherFetchFunc[Data]
expiresDuration time.Duration
callbacks CachedDataFetcherCallbacks[Data]
data *TimedValue[Data]
mutex sync.Mutex
}
func NewCachedDataFetcher[Data any](fetcher CachedDataFetcherFetchFunc[Data]) *CachedDataFetcher[Data] {
return &CachedDataFetcher[Data]{
Logger: zap.NewNop(),
fetcher: fetcher,
}
}
func NewCachedDataFetcherLite[Data any](fetcher CachedDataFetcherFetchFuncLite[Data]) *CachedDataFetcher[Data] {
return NewCachedDataFetcher(func(ctx context.Context, expires time.Duration) (data Data, t time.Time, err error) {
data, err = fetcher(ctx)
if err == nil {
t = time.Now()
}
return
})
}
func (fetcher *CachedDataFetcher[Data]) Dup() *CachedDataFetcher[Data] {
return &CachedDataFetcher[Data]{
fetcher: fetcher.fetcher,
expiresDuration: fetcher.expiresDuration,
}
}
func (fetcher *CachedDataFetcher[Data]) WithCachedDataPurged() *CachedDataFetcher[Data] {
fetcher.data = nil
return fetcher
}
func (fetcher *CachedDataFetcher[Data]) PurgeCachedData() {
fetcher.WithCachedDataPurged()
}
func (fetcher *CachedDataFetcher[Data]) SinceTimeFromExpiresDuration(expiresDuration time.Duration) (since time.Time) {
if expiresDuration <= 0 {
expiresDuration = fetcher.expiresDuration
}
if expiresDuration > 0 {
since = time.Now().Add(-expiresDuration)
}
return
}
func (fetcher *CachedDataFetcher[Data]) EnsureExpiresDuration(expiresDuration time.Duration) *CachedDataFetcher[Data] {
if expiresDuration <= 0 {
return fetcher
}
if fetcher.expiresDuration <= 0 {
fetcher.expiresDuration = expiresDuration
return fetcher
}
if expiresDuration < fetcher.expiresDuration {
fetcher.expiresDuration = expiresDuration
}
return fetcher
}
func (fetcher *CachedDataFetcher[Data]) WithFetcher(fetcherFunc CachedDataFetcherFetchFunc[Data]) *CachedDataFetcher[Data] {
fetcher.fetcher = fetcherFunc
return fetcher
}
func (fetcher *CachedDataFetcher[Data]) WithExpiresDuration(duration time.Duration) *CachedDataFetcher[Data] {
fetcher.expiresDuration = duration
return fetcher
}
func (fetcher *CachedDataFetcher[Data]) WithOthersSubscribed(timeout time.Duration, others ...interface {
RegisterCallbackFuncLite(func(context.Context), time.Duration)
}) *CachedDataFetcher[Data] {
for _, other := range others {
other.RegisterCallbackFuncLite(fetcher.TriggerRefresh, timeout)
}
return fetcher
}
func (fetcher *CachedDataFetcher[Data]) RegisterCallbackFuncLite(callback func(context.Context), timeout time.Duration) {
fetcher.NewCallbackLite(callback, timeout)
}
func (fetcher *CachedDataFetcher[Data]) NewCallbackLite(callback func(context.Context), timeout time.Duration) *CachedDataFetcherCallback[Data] {
return fetcher.RegisterCallback(NewCachedDataFetcherCallback(func(ctx context.Context, data Data, t time.Time) {
callback(ctx)
}, timeout))
}
func (fetcher *CachedDataFetcher[Data]) NewCallback(callback CachedDataFetcherCallbackFunc[Data], timeout time.Duration) *CachedDataFetcherCallback[Data] {
return fetcher.RegisterCallback(NewCachedDataFetcherCallback(callback, timeout))
}
func (fetcher *CachedDataFetcher[Data]) RegisterCallback(callback *CachedDataFetcherCallback[Data]) *CachedDataFetcherCallback[Data] {
fetcher.mutex.Lock()
defer fetcher.mutex.Unlock()
return fetcher.registerCallback(callback)
}
func (fetcher *CachedDataFetcher[Data]) registerCallback(callback *CachedDataFetcherCallback[Data]) *CachedDataFetcherCallback[Data] {
fetcher.callbacks = fetcher.callbacks.Append(callback)
return callback
}
func (fetcher *CachedDataFetcher[Data]) GetCached() (Data, time.Time) {
return fetcher.getCached()
}
func (fetcher *CachedDataFetcher[Data]) Get() (Data, time.Time, bool) {
return fetcher.GetWithExpires(0)
}
func (fetcher *CachedDataFetcher[Data]) GetWithExpires(expiresDuration time.Duration) (Data, time.Time, bool) {
return fetcher.getWithExpires(expiresDuration)
}
func (fetcher *CachedDataFetcher[Data]) GetWithSince(since time.Time) (Data, time.Time, bool) {
return fetcher.getWithSince(since)
}
func (fetcher *CachedDataFetcher[Data]) GetFetchLite() func(ctx context.Context) (Data, error) {
return fetcher.FetchLite
}
func (fetcher *CachedDataFetcher[Data]) FetchLite(ctx context.Context) (data Data, err error) {
data, _, err = fetcher.Fetch(ctx)
return
}
func (fetcher *CachedDataFetcher[Data]) GetFetch() func(ctx context.Context) (Data, time.Time, error) {
return fetcher.Fetch
}
func (fetcher *CachedDataFetcher[Data]) Fetch(ctx context.Context) (Data, time.Time, error) {
return fetcher.FetchWithExpires(ctx, 0)
}
func (fetcher *CachedDataFetcher[Data]) FetchWithExpires(ctx context.Context, expiresDuration time.Duration) (data Data, t time.Time, err error) {
return fetcher.FetchWithSince(ctx, fetcher.SinceTimeFromExpiresDuration(expiresDuration))
}
func (fetcher *CachedDataFetcher[Data]) FetchWithSince(ctx context.Context, since time.Time) (data Data, t time.Time, err error) {
data, t, ok := fetcher.getWithSince(since)
if ok {
return
}
fetcher.mutex.Lock()
defer fetcher.mutex.Unlock()
data, t, ok = fetcher.getWithSince(since)
if ok {
return
}
data, t, err = fetcher.refetch(ctx)
if err == nil {
return
}
data, t = fetcher.getCached()
return
}
func (fetcher *CachedDataFetcher[Data]) TriggerRefresh(ctx context.Context) {
fetcher.Refresh(ctx)
}
func (fetcher *CachedDataFetcher[Data]) GetRefresh() func(ctx context.Context) (Data, time.Time, error) {
return fetcher.Refresh
}
func (fetcher *CachedDataFetcher[Data]) Refresh(ctx context.Context) (data Data, t time.Time, err error) {
fetcher.mutex.Lock()
defer fetcher.mutex.Unlock()
return fetcher.refetch(ctx)
}
func (fetcher *CachedDataFetcher[Data]) refetch(ctx context.Context) (data Data, t time.Time, err error) {
fetcher.Info("Referching",
zap.Duration("ExpiresDuration", fetcher.expiresDuration),
zap.Time("LastFetchedTime", fetcher.data.Time()),
)
data, t, err = fetcher.fetcher(ctx, fetcher.expiresDuration)
if err != nil {
fetcher.Error("ReferchFailed",
zap.Error(err),
zap.Duration("ExpiresDuration", fetcher.expiresDuration),
zap.Time("LastFetchedTime", fetcher.data.Time()),
)
return
}
fetcher.Info("Referched",
zap.Time("FetchedTime", t),
zap.Duration("ExpiresDuration", fetcher.expiresDuration),
zap.Time("LastFetchedTime", fetcher.data.Time()),
)
if t.IsZero() {
t = time.Now()
}
fetcher.data = NewTimedValue(data, t)
// call it asynchronously to avoid dead lock
// in case that callbacks may call fetcher method again
go fetcher.callbacks.Call(data, t)
return
}
func (fetcher *CachedDataFetcher[Data]) getWithExpires(expiresDuration time.Duration) (Data, time.Time, bool) {
return fetcher.getWithSince(fetcher.SinceTimeFromExpiresDuration(expiresDuration))
}
func (fetcher *CachedDataFetcher[Data]) getWithSince(since time.Time) (data Data, t time.Time, ok bool) {
data, t = fetcher.getCached()
ok = t.After(since)
return
}
func (fetcher *CachedDataFetcher[Data]) getCached() (data Data, t time.Time) {
return fetcher.data.ValueAndTime()
}