build with
obcache-go
Memoize a Go function in one line
obcache.Wrap(cache, fn) returns a function of the same type. A repeat call
with the same arguments returns the stored result without running fn.
Concurrent callers with the same arguments, in one cache, share one call: stampede
protection.
The defaults are 1000 entries, a 5 minute TTL and LRU eviction, with LFU and FIFO as options. The store is memory or Redis. Prometheus metrics sit in a separate package, so the cache alone does not link them.
A leading context.Context is not part of the key: anything that varies by
user belongs in the arguments.
why it exists
Caching an expensive call by hand means a get, a set and an invalidation around every call site, usually for a query or an API request. obcache-go puts that behind the function, set up once.
what it taught
Memoizing a function is mostly deciding when two calls are the same call. The key is the wrapped function plus each argument after a leading context, encoded in full.
Wrap refuses a func or chan parameter, which has no stable encoding, until
a key function is given.