Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.4 KiB · at eb4a6ea

Write createCache(loader, ttlMs, now), an async read-through cache.

  • loader(key) loads the value for a key. It may fail with { kind: "load_failed", reason }.
  • ttlMs must be a positive integer; otherwise createCache itself fails with { kind: "invalid_ttl" }.
  • now() returns the current time in milliseconds. Never read the real clock.

The cache has two operations. Keys are arbitrary strings.

get(key) returns the value for key, or fails with the loader's failure (the same error the loader produced):

  • A cached value is fresh while now() - loadedAt < ttlMs, where loadedAt is the value of now() at the moment its load finished. A fresh value is returned without calling the loader. An expired value is never returned.
  • Otherwise, if a load for that key is already in progress, the call waits for that same load instead of starting another one.
  • Otherwise it calls loader(key) once. When the load succeeds, the value is cached (whatever the value is) and every caller waiting on that load receives it. When it fails, every caller waiting on that load receives the failure and nothing is cached, so the next get calls the loader again.

invalidate(key) removes any cached value for key. If a load for key is in progress, its callers still receive its outcome, but its result is not cached, and the next get(key) starts a new load rather than joining it. Invalidating a key that is not cached does nothing.