Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down