In this section, we’ll build a concurrent non-blocking cache, an abstraction that solves a problem that arises often in real-world concurrent programs but is not well addressed by existing libraries. This is the problem of memoizing a function, that is, caching the result of a function so that it need be computed only once. Our solution will be concurrency-safe and will avoid the contention associated with designs based on a single lock for the whole cache.
We’ll use the
httpGetBody function below as an example of the
type of function we might want to memoize.
It makes an HTTP GET request and reads the response body.
Calls to this function are relatively expensive, so we’d like to avoid repeating ...