WeakCache provides duplicate function call suppression combined with a weakly referenced cache for storing results.
This means:
- Concurrent calls with the same key will be collapsed into a single execution making sure that only one execution is in-flight for a given key at a time.
- Results are cached and reused if available — but only as long as they are still held by a weak pointer. Once garbage collected, the result is gone from cache.
WeakCache use modified version of Go’s x/sync/singleflight. The key differences of weakCache singleflight are:
- The main difference is that
Groupis now generic. This means that the function passed toDohas to return a type which was used to createGroup. - The
DoChanmethod is removed. I might add it back in the future as a generic version.
w := NewWeakCache[int]()
r, err := w.Do("key", func() (int, error) {
time.Sleep(10 * time.Microsecond)
return 1, nil
})- If another goroutine calls
Do("key", ...)while the first one is still running, it will block and reuse the results from the first one. This issingleflightbehavior. - Later calls to
Do("key", ...)will reuse the cached result, as long as it hasn’t been collected by the GC.