| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log" |
| 6 |
"math/rand/v2" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/viccon/sturdyc" |
| 10 |
) |
| 11 |
|
| 12 |
// Basic configuration for the cache. |
| 13 |
const ( |
| 14 |
capacity = 100_000 |
| 15 |
numberOfShards = 100 |
| 16 |
ttl = 5 * time.Minute |
| 17 |
percentageOfRecordsToEvictWhenFull = 25 |
| 18 |
) |
| 19 |
|
| 20 |
// Configuration for the early in-memory refreshes. |
| 21 |
const ( |
| 22 |
minRefreshTime = 2 * time.Second |
| 23 |
maxRefreshTime = 4 * time.Second |
| 24 |
synchronousRefreshTime = 30 * time.Second |
| 25 |
retryBaseDelay = 5 * time.Second |
| 26 |
) |
| 27 |
|
| 28 |
// Configuration for the refresh coalescing. |
| 29 |
const ( |
| 30 |
idealBufferSize = 3 |
| 31 |
bufferTimeout = 2 * time.Second |
| 32 |
) |
| 33 |
|
| 34 |
// Configuration for how early we want to refresh records in the distributed storage. |
| 35 |
const refreshAfter = time.Second |
| 36 |
|
| 37 |
func newAPIClient(distributedStorage sturdyc.DistributedStorageWithDeletions) *apiClient { |
| 38 |
return &apiClient{ |
| 39 |
cache: sturdyc.New[any](capacity, numberOfShards, ttl, percentageOfRecordsToEvictWhenFull, |
| 40 |
sturdyc.WithEarlyRefreshes(minRefreshTime, maxRefreshTime, synchronousRefreshTime, retryBaseDelay), |
| 41 |
sturdyc.WithRefreshCoalescing(idealBufferSize, bufferTimeout), |
| 42 |
sturdyc.WithDistributedStorageEarlyRefreshes(distributedStorage, refreshAfter), |
| 43 |
// NOTE: Uncommenting this line will make the cache mark the records as |
| 44 |
// missing rather than delete them. It will prevent the cache from making |
| 45 |
// any outgoing requests until it's due for a refresh. |
| 46 |
// sturdyc.WithMissingRecordStorage(), |
| 47 |
), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
type apiClient struct { |
| 52 |
cache *sturdyc.Client[any] |
| 53 |
} |
| 54 |
|
| 55 |
type options struct { |
| 56 |
SortOrder string |
| 57 |
} |
| 58 |
|
| 59 |
func (c *apiClient) GetShippingOptions(ctx context.Context, containerIndex int, ids []string, sortOrder string) (map[string][]string, error) { |
| 60 |
cacheKeyFn := c.cache.PermutatedBatchKeyFn("shipping-options", options{sortOrder}) |
| 61 |
fetchFn := func(_ context.Context, ids []string) (map[string][]string, error) { |
| 62 |
response := make(map[string][]string, len(ids)) |
| 63 |
|
| 64 |
for _, id := range ids { |
| 65 |
// Excluding an ID will make it get delete if it has been cached before. |
| 66 |
// Following this log statament, you should see that it gets deleted, and the |
| 67 |
// next time it's requested it leads to a "outgoing" request which means that |
| 68 |
// it wasn't found in the distributed key-value store. |
| 69 |
if rand.IntN(2) == 0 { |
| 70 |
log.Printf("Excluding ID: %s from the response\n", id) |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
log.Printf("ID: %s was fetched by container %d\n", id, containerIndex) |
| 75 |
response[id] = []string{"standard", "express", "next-day"} |
| 76 |
} |
| 77 |
|
| 78 |
return response, nil |
| 79 |
} |
| 80 |
return sturdyc.GetOrFetchBatch(ctx, c.cache, ids, cacheKeyFn, fetchFn) |
| 81 |
} |