-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,87 @@ | ||
package main | ||
|
||
func main() {} | ||
import ( | ||
"context" | ||
"log" | ||
"math/rand/v2" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/creativecreature/sturdyc" | ||
) | ||
|
||
type API struct { | ||
cacheClient *sturdyc.Client | ||
} | ||
|
||
func NewAPI(c *sturdyc.Client) *API { | ||
return &API{c} | ||
} | ||
|
||
func (a *API) GetBatch(ctx context.Context, ids []string) (map[string]string, error) { | ||
// We are going to pass the cache a key function that prefixes each id. | ||
// This makes it possible to save the same id for different data sources. | ||
cacheKeyFn := a.cacheClient.BatchKeyFn("some-prefix") | ||
|
||
// The fetchFn is only going to retrieve the IDs that are not in the cache. | ||
fetchFn := func(_ context.Context, cacheMisses []string) (map[string]string, error) { | ||
log.Printf("Cache miss. Fetching ids: %s\n", strings.Join(cacheMisses, ", ")) | ||
response := make(map[string]string, len(cacheMisses)) | ||
for _, id := range cacheMisses { | ||
response[id] = "value" | ||
} | ||
return response, nil | ||
} | ||
|
||
return sturdyc.GetFetchBatch(ctx, a.cacheClient, ids, cacheKeyFn, fetchFn) | ||
} | ||
|
||
func main() { | ||
// Maximum number of entries in the sturdyc. | ||
capacity := 10000 | ||
// Number of shards to use for the sturdyc. | ||
numShards := 10 | ||
// Time-to-live for cache entries. | ||
ttl := 2 * time.Hour | ||
// Percentage of entries to evict when the cache is full. Setting this | ||
// to 0 will make set a no-op if the cache has reached its capacity. | ||
evictionPercentage := 10 | ||
// Set a minimum and maximum refresh delay for the sturdyc. This is | ||
// used to spread out the refreshes for entries evenly over time. | ||
minRefreshDelay := time.Second | ||
maxRefreshDelay := time.Second * 2 | ||
// The base for exponential backoff when retrying a refresh. | ||
retryBaseDelay := time.Millisecond * 10 | ||
// Tell the cache to store missing records. | ||
storeMisses := true | ||
|
||
// Create a cache client with the specified configuration. | ||
cacheClient := sturdyc.New(capacity, numShards, ttl, evictionPercentage, | ||
sturdyc.WithStampedeProtection(minRefreshDelay, maxRefreshDelay, retryBaseDelay, storeMisses), | ||
) | ||
|
||
// Create a new API instance with the cache client. | ||
api := NewAPI(cacheClient) | ||
|
||
// Seed the cache with ids 1-10. | ||
log.Println("Seeding ids 1-10") | ||
ids := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"} | ||
api.GetBatch(context.Background(), ids) | ||
log.Println("Seed completed") | ||
|
||
// Each record has been cached individually. To illustrate this, we can keep | ||
// fetching a random number of records from the original batch, plus a new ID. | ||
// Looking at the looks, we'll see that the cache only fetches the id that | ||
// wasn't in the original batch. | ||
for i := 1; i <= 100; i++ { | ||
// Get N ids from the original batch. | ||
recordsToFetch := rand.IntN(10) + 1 | ||
batch := make([]string, recordsToFetch) | ||
copy(batch, ids[:recordsToFetch]) | ||
// Add a random ID between 1 and 100 to the batch. | ||
batch = append(batch, strconv.Itoa(rand.IntN(1000)+10)) | ||
values, _ := api.GetBatch(context.Background(), batch) | ||
log.Println(values) | ||
} | ||
} |