| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"log" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/viccon/sturdyc" |
| 10 |
) |
| 11 |
|
| 12 |
type API struct { |
| 13 |
*sturdyc.Client[string] |
| 14 |
count int |
| 15 |
} |
| 16 |
|
| 17 |
func NewAPI(c *sturdyc.Client[string]) *API { |
| 18 |
return &API{c, 0} |
| 19 |
} |
| 20 |
|
| 21 |
func (a *API) Get(ctx context.Context, key string) (string, error) { |
| 22 |
fetchFn := func(_ context.Context) (string, error) { |
| 23 |
a.count++ |
| 24 |
log.Printf("Fetching value for key: %s\n", key) |
| 25 |
if a.count > 3 { |
| 26 |
return "value", nil |
| 27 |
} |
| 28 |
return "", sturdyc.ErrNotFound |
| 29 |
} |
| 30 |
return a.GetOrFetch(ctx, key, fetchFn) |
| 31 |
} |
| 32 |
|
| 33 |
func main() { |
| 34 |
// =========================================================== |
| 35 |
// ===================== Basic configuration ================= |
| 36 |
// =========================================================== |
| 37 |
// Maximum number of entries in the sturdyc. |
| 38 |
capacity := 10000 |
| 39 |
// Number of shards to use for the sturdyc. |
| 40 |
numShards := 10 |
| 41 |
// Time-to-live for cache entries. |
| 42 |
ttl := 2 * time.Hour |
| 43 |
// Percentage of entries to evict when the cache is full. Setting this |
| 44 |
// to 0 will make set a no-op if the cache has reached its capacity. |
| 45 |
evictionPercentage := 10 |
| 46 |
|
| 47 |
// =========================================================== |
| 48 |
// =================== Stampede protection =================== |
| 49 |
// =========================================================== |
| 50 |
// Set a minimum and maximum refresh delay for the sturdyc. This is |
| 51 |
// used to spread out the refreshes for entries evenly over time. |
| 52 |
minRefreshDelay := time.Millisecond * 10 |
| 53 |
maxRefreshDelay := time.Millisecond * 30 |
| 54 |
// Set a synchronous refresh delay for when we want a refresh to happen synchronously. |
| 55 |
synchronousRefreshDelay := time.Second * 30 |
| 56 |
// The base for exponential backoff when retrying a refresh. |
| 57 |
retryBaseDelay := time.Millisecond * 10 |
| 58 |
|
| 59 |
// Create a cache client with the specified configuration. |
| 60 |
cacheClient := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage, |
| 61 |
sturdyc.WithEarlyRefreshes(minRefreshDelay, maxRefreshDelay, synchronousRefreshDelay, retryBaseDelay), |
| 62 |
sturdyc.WithMissingRecordStorage(), |
| 63 |
) |
| 64 |
|
| 65 |
// Create a new API instance with the cache client. |
| 66 |
api := NewAPI(cacheClient) |
| 67 |
|
| 68 |
// We are going to retrieve the values every 10 milliseconds, however the |
| 69 |
// logs will reveal that actual refreshes fluctuate randomly within a 10-30 |
| 70 |
// millisecond range. Even if this loop is executed across multiple |
| 71 |
// goroutines, the API call frequency will maintain this variability, |
| 72 |
// ensuring we avoid overloading the API with requests. |
| 73 |
for i := 0; i < 100; i++ { |
| 74 |
val, err := api.Get(context.Background(), "key") |
| 75 |
if errors.Is(err, sturdyc.ErrMissingRecord) { |
| 76 |
log.Println("Record does not exist.") |
| 77 |
} |
| 78 |
if err == nil { |
| 79 |
log.Printf("Value: %s\n", val) |
| 80 |
} |
| 81 |
time.Sleep(minRefreshDelay) |
| 82 |
} |
| 83 |
} |