forked from creativecreature/sturdyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.go
51 lines (43 loc) · 1.05 KB
/
refresh.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
package sturdyc
import (
"context"
"errors"
)
func (c *Client[T]) refresh(key string, fetchFn FetchFn[T]) {
response, err := fetchFn(context.Background())
if err != nil {
if c.storeMissingRecords && errors.Is(err, ErrNotFound) {
c.StoreMissingRecord(key)
}
if !c.storeMissingRecords && errors.Is(err, ErrNotFound) {
c.Delete(key)
}
return
}
c.Set(key, response)
}
func (c *Client[T]) refreshBatch(ids []string, keyFn KeyFn, fetchFn BatchFetchFn[T]) {
c.reportBatchRefreshSize(len(ids))
response, err := fetchFn(context.Background(), ids)
if err != nil {
return
}
// Check if any of the records have been deleted at the data source.
for _, id := range ids {
_, okCache, _, _ := c.getWithState(keyFn(id))
_, okResponse := response[id]
if okResponse {
continue
}
if !c.storeMissingRecords && !okResponse && okCache {
c.Delete(keyFn(id))
}
if c.storeMissingRecords && !okResponse {
c.StoreMissingRecord(keyFn(id))
}
}
// Cache the refreshed records.
for id, record := range response {
c.Set(keyFn(id), record)
}
}