| 1 |
package sturdyc |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
) |
| 7 |
|
| 8 |
func (c *Client[T]) refresh(key string, fetchFn FetchFn[T]) { |
| 9 |
response, err := fetchFn(context.Background()) |
| 10 |
if err != nil && !errors.Is(err, errOnlyDistributedRecords) { |
| 11 |
if c.storeMissingRecords && errors.Is(err, ErrNotFound) { |
| 12 |
c.StoreMissingRecord(key) |
| 13 |
} |
| 14 |
if !c.storeMissingRecords && errors.Is(err, ErrNotFound) { |
| 15 |
c.Delete(key) |
| 16 |
} |
| 17 |
return |
| 18 |
} |
| 19 |
c.Set(key, response) |
| 20 |
} |
| 21 |
|
| 22 |
func (c *Client[T]) refreshBatch(ids []string, keyFn KeyFn, fetchFn BatchFetchFn[T]) { |
| 23 |
c.reportBatchRefreshSize(len(ids)) |
| 24 |
response, err := fetchFn(context.Background(), ids) |
| 25 |
if err != nil && !errors.Is(err, errOnlyDistributedRecords) { |
| 26 |
return |
| 27 |
} |
| 28 |
|
| 29 |
// Check if any of the records have been deleted at the data source. |
| 30 |
for _, id := range ids { |
| 31 |
_, okCache, _, _, _ := c.getWithState(keyFn(id)) |
| 32 |
_, okResponse := response[id] |
| 33 |
|
| 34 |
if okResponse { |
| 35 |
continue |
| 36 |
} |
| 37 |
|
| 38 |
if !c.storeMissingRecords && !okResponse && okCache { |
| 39 |
c.Delete(keyFn(id)) |
| 40 |
} |
| 41 |
|
| 42 |
// If we're only getting records from the distributed storage, it means that we weren't able to get |
| 43 |
// the remaining IDs for the batch from the underlying data source. We don't want to store these |
| 44 |
// as missing records because we don't know if they're missing or not. |
| 45 |
if c.storeMissingRecords && !okResponse && !errors.Is(err, errOnlyDistributedRecords) { |
| 46 |
c.StoreMissingRecord(keyFn(id)) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Cache the refreshed records. |
| 51 |
for id, record := range response { |
| 52 |
c.Set(keyFn(id), record) |
| 53 |
} |
| 54 |
} |