| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/viccon/sturdyc" |
| 9 |
) |
| 10 |
|
| 11 |
type OrderAPI struct { |
| 12 |
cacheClient *sturdyc.Client[any] |
| 13 |
} |
| 14 |
|
| 15 |
func NewOrderAPI(c *sturdyc.Client[any]) *OrderAPI { |
| 16 |
return &OrderAPI{cacheClient: c} |
| 17 |
} |
| 18 |
|
| 19 |
func (a *OrderAPI) OrderStatus(ctx context.Context, ids []string) (map[string]string, error) { |
| 20 |
cacheKeyFn := a.cacheClient.BatchKeyFn("order-status") |
| 21 |
fetchFn := func(_ context.Context, cacheMisses []string) (map[string]string, error) { |
| 22 |
response := make(map[string]string, len(ids)) |
| 23 |
for _, id := range cacheMisses { |
| 24 |
response[id] = "Order status: pending" |
| 25 |
} |
| 26 |
return response, nil |
| 27 |
} |
| 28 |
return sturdyc.GetOrFetchBatch(ctx, a.cacheClient, ids, cacheKeyFn, fetchFn) |
| 29 |
} |
| 30 |
|
| 31 |
func (a *OrderAPI) DeliveryTime(ctx context.Context, ids []string) (map[string]time.Time, error) { |
| 32 |
cacheKeyFn := a.cacheClient.BatchKeyFn("delivery-time") |
| 33 |
fetchFn := func(_ context.Context, cacheMisses []string) (map[string]time.Time, error) { |
| 34 |
response := make(map[string]time.Time, len(ids)) |
| 35 |
for _, id := range cacheMisses { |
| 36 |
response[id] = time.Now() |
| 37 |
} |
| 38 |
return response, nil |
| 39 |
} |
| 40 |
return sturdyc.GetOrFetchBatch(ctx, a.cacheClient, ids, cacheKeyFn, fetchFn) |
| 41 |
} |
| 42 |
|
| 43 |
func main() { |
| 44 |
// Maximum number of entries in the sturdyc. |
| 45 |
capacity := 10000 |
| 46 |
// Number of shards to use for the sturdyc. |
| 47 |
numShards := 10 |
| 48 |
// Time-to-live for cache entries. |
| 49 |
ttl := 2 * time.Hour |
| 50 |
// Percentage of entries to evict when the cache is full. Setting this |
| 51 |
// to 0 will make set a no-op if the cache has reached its capacity. |
| 52 |
evictionPercentage := 10 |
| 53 |
// Set a minimum and maximum refresh delay for the sturdyc. This is |
| 54 |
// used to spread out the refreshes for entries evenly over time. |
| 55 |
minRefreshDelay := time.Second |
| 56 |
maxRefreshDelay := time.Second * 2 |
| 57 |
// Set a synchronous refresh delay for when we want a refresh to happen synchronously. |
| 58 |
synchronousRefreshDelay := time.Second * 30 |
| 59 |
// The base for exponential backoff when retrying a refresh. |
| 60 |
retryBaseDelay := time.Millisecond * 10 |
| 61 |
|
| 62 |
// Create a new cache client with the specified configuration. |
| 63 |
cacheClient := sturdyc.New[any](capacity, numShards, ttl, evictionPercentage, |
| 64 |
sturdyc.WithEarlyRefreshes(minRefreshDelay, maxRefreshDelay, synchronousRefreshDelay, retryBaseDelay), |
| 65 |
sturdyc.WithRefreshCoalescing(10, time.Second*15), |
| 66 |
) |
| 67 |
|
| 68 |
api := NewOrderAPI(cacheClient) |
| 69 |
ids := []string{"1", "2", "3"} |
| 70 |
ctx := context.Background() |
| 71 |
if orders, err := api.OrderStatus(ctx, ids); err == nil { |
| 72 |
log.Println(orders) |
| 73 |
} |
| 74 |
if deliveryTimes, err := api.DeliveryTime(ctx, ids); err == nil { |
| 75 |
log.Println(deliveryTimes) |
| 76 |
} |
| 77 |
} |