LookupCache with a dict-based cache and a method get(self, key, compute_fn). On the first call for a given key, call compute_fn(key), store the result in the cache, and return it. On subsequent calls with the same key, return the cached value without calling compute_fn again.c = LookupCache(); c.get(4, lambda k: k * k) # calls compute_fn c.get(4, lambda k: 1/0) # cache hit, no call, no error
16 then 16
Second call reuses the cached 16, even though the second compute_fn would raise.
Store an internal `self._cache = {}`.
Use `if key not in self._cache: self._cache[key] = compute_fn(key)` then return `self._cache[key]`.