| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/viccon/sturdyc" |
| 10 |
) |
| 11 |
|
| 12 |
type OrderOptions struct { |
| 13 |
CarrierName string |
| 14 |
LatestDeliveryTime string |
| 15 |
} |
| 16 |
|
| 17 |
type OrderAPI struct { |
| 18 |
*sturdyc.Client[string] |
| 19 |
} |
| 20 |
|
| 21 |
func NewOrderAPI(c *sturdyc.Client[string]) *OrderAPI { |
| 22 |
return &OrderAPI{c} |
| 23 |
} |
| 24 |
|
| 25 |
func (a *OrderAPI) OrderStatus(ctx context.Context, ids []string, opts OrderOptions) (map[string]string, error) { |
| 26 |
// We use the PermutedBatchKeyFn when an ID isn't enough to uniquely identify a |
| 27 |
// record. The cache is going to store each id once per set of options. In a more |
| 28 |
// realistic scenario, the opts would be query params or arguments to a DB query. |
| 29 |
cacheKeyFn := a.PermutatedBatchKeyFn("key", opts) |
| 30 |
|
| 31 |
// We'll create a fetchFn with a closure that captures the options. For this |
| 32 |
// simple example, it logs and returns the status for each order, but you could |
| 33 |
// just as easily have called an external API. |
| 34 |
fetchFn := func(_ context.Context, cacheMisses []string) (map[string]string, error) { |
| 35 |
log.Printf("Fetching: %v, carrier: %s, delivery time: %s\n", cacheMisses, opts.CarrierName, opts.LatestDeliveryTime) |
| 36 |
response := map[string]string{} |
| 37 |
for _, id := range cacheMisses { |
| 38 |
response[id] = fmt.Sprintf("Available for %s", opts.CarrierName) |
| 39 |
} |
| 40 |
return response, nil |
| 41 |
} |
| 42 |
return a.GetOrFetchBatch(ctx, ids, cacheKeyFn, fetchFn) |
| 43 |
} |
| 44 |
|
| 45 |
func main() { |
| 46 |
// Maximum number of entries in the sturdyc. |
| 47 |
capacity := 10000 |
| 48 |
// Number of shards to use for the sturdyc. |
| 49 |
numShards := 10 |
| 50 |
// Time-to-live for cache entries. |
| 51 |
ttl := 2 * time.Hour |
| 52 |
// Percentage of entries to evict when the cache is full. Setting this |
| 53 |
// to 0 will make set a no-op if the cache has reached its capacity. |
| 54 |
evictionPercentage := 10 |
| 55 |
// Set a minimum and maximum refresh delay for the sturdyc. This is |
| 56 |
// used to spread out the refreshes for entries evenly over time. |
| 57 |
minRefreshDelay := time.Second |
| 58 |
maxRefreshDelay := time.Second * 2 |
| 59 |
// Set a synchronous refresh delay for when we want a refresh to happen synchronously. |
| 60 |
synchronousRefreshDelay := time.Second * 30 |
| 61 |
// The base for exponential backoff when retrying a refresh. |
| 62 |
retryBaseDelay := time.Millisecond * 10 |
| 63 |
|
| 64 |
// Create a new cache client with the specified configuration. |
| 65 |
cacheClient := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage, |
| 66 |
sturdyc.WithEarlyRefreshes(minRefreshDelay, maxRefreshDelay, synchronousRefreshDelay, retryBaseDelay), |
| 67 |
) |
| 68 |
|
| 69 |
// We will fetch these IDs using various option sets, meaning that the ID alone |
| 70 |
// won't be enough to uniquely identify a record. Instead, the cache is going to |
| 71 |
// store each record by combining the ID with the permutations of the options. |
| 72 |
ids := []string{"id1", "id2", "id3"} |
| 73 |
optionSetOne := OrderOptions{CarrierName: "FEDEX", LatestDeliveryTime: "2024-04-06"} |
| 74 |
optionSetTwo := OrderOptions{CarrierName: "DHL", LatestDeliveryTime: "2024-04-07"} |
| 75 |
optionSetThree := OrderOptions{CarrierName: "UPS", LatestDeliveryTime: "2024-04-08"} |
| 76 |
|
| 77 |
orderClient := NewOrderAPI(cacheClient) |
| 78 |
ctx := context.Background() |
| 79 |
|
| 80 |
// Next, we'll fetch the entire list of IDs for all options sets. |
| 81 |
log.Println("Filling the cache with all IDs for all option sets") |
| 82 |
orderClient.OrderStatus(ctx, ids, optionSetOne) |
| 83 |
orderClient.OrderStatus(ctx, ids, optionSetTwo) |
| 84 |
orderClient.OrderStatus(ctx, ids, optionSetThree) |
| 85 |
log.Println("Cache filled") |
| 86 |
|
| 87 |
// At this point, the cache has stored each record individually for each option set: |
| 88 |
// FEDEX-2024-04-06-id1 |
| 89 |
// DHL-2024-04-07-id1 |
| 90 |
// UPS-2024-04-08-id1 |
| 91 |
// etc.. |
| 92 |
|
| 93 |
// Sleep to make sure that all records are due for a refresh. |
| 94 |
time.Sleep(maxRefreshDelay + 1) |
| 95 |
|
| 96 |
// Fetch each id for each option set. |
| 97 |
for i := 0; i < len(ids); i++ { |
| 98 |
orderClient.OrderStatus(ctx, []string{ids[i]}, optionSetOne) |
| 99 |
orderClient.OrderStatus(ctx, []string{ids[i]}, optionSetTwo) |
| 100 |
orderClient.OrderStatus(ctx, []string{ids[i]}, optionSetThree) |
| 101 |
} |
| 102 |
|
| 103 |
// Sleep for a second to allow the refresh logs to print. |
| 104 |
time.Sleep(time.Second) |
| 105 |
} |