Build unarycache package
The functional proposal was nicely outlined by Patrick in #4559 (comment 1163759755):
We should be able to get away with a simple
internal/unarycache
module that:
- Gets as input a key that deterministically allows us to compute the result.
- Gets a function that computes
$key -> $result
.- Has some configuration: maximum number of cache entries, eviction period, cache "name" to discern different caches.
Everything else should ideally be handled by the module. This could even be our first use of generics.
This file will have all the license details and the blob OID of the
license_path
. Each request we'll read that file, check the current blob ID of that license path and if they are the same we'll return the stored values in the json.We should allow for the use-case where there's multiple cache entries and make this configurable. So you'd e.g. have
$repo/caches/licenses/$oid
, which is per-key that we want to cache.Because different clients can call
FindLicense
concurrently. To avoid each of them to go compute the license on their own, we can use theinternal/safe
to make sure only one is able to do the license computation. If a call comes in while the file is locked we can read it, and return the license info, even though it might contain stale info. If there wasn't a cachedinfo/license-info.json
file we'll returnundefined
.I'd say we should block if a cache entry with the same key as ours is ongoing, but then return the result that's concurrently getting computed when it finishes. We avoid serving stale data, and avoid computing the same thing multiple times.