From 89796024beead9c6ab14ee5ef35ec61dc9173d05 Mon Sep 17 00:00:00 2001 From: Victor Conner Date: Tue, 11 Mar 2025 08:54:49 +0100 Subject: [PATCH] Update the basic example --- README.md | 8 +++++++- examples/basic/main.go | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c05d2a0..ecde059 100644 --- a/README.md +++ b/README.md @@ -407,11 +407,17 @@ request, and another from the second or third. // [4,11] // [14,2] // [6,15] + +func pickRandomValue(batches [][]string) string { + batch := batches[rand.IntN(len(batches))] + return batch[rand.IntN(len(batch))] +} + var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { - ids := []string{batches[rand.IntN(2)][rand.IntN(4)], batches[rand.IntN(2)][rand.IntN(4)]} + ids := []string{pickRandomValue(batches), pickRandomValue(batches)} res, _ := cacheClient.GetOrFetchBatch(context.Background(), ids, keyPrefixFn, fetchFn) log.Printf("got batch: %v\n", res) wg.Done() diff --git a/examples/basic/main.go b/examples/basic/main.go index 69bbfac..3b10a6c 100644 --- a/examples/basic/main.go +++ b/examples/basic/main.go @@ -12,6 +12,11 @@ import ( "github.com/viccon/sturdyc" ) +func pickRandomValue(batches [][]string) string { + batch := batches[rand.IntN(len(batches))] + return batch[rand.IntN(len(batch))] +} + func demonstrateGetOrFetch(cacheClient *sturdyc.Client[int]) { // The cache has built-in stampede protection where it tracks in-flight // requests for every key. @@ -82,7 +87,7 @@ func demonstrateGetOrFetchBatch(cacheClient *sturdyc.Client[int]) { for i := 0; i < 5; i++ { wg.Add(1) go func() { - ids := []string{batches[rand.IntN(2)][rand.IntN(4)], batches[rand.IntN(2)][rand.IntN(4)]} + ids := []string{pickRandomValue(batches), pickRandomValue(batches)} res, _ := cacheClient.GetOrFetchBatch(context.Background(), ids, keyPrefixFn, fetchFn) log.Printf("got batch: %v\n", res) wg.Done()