Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 24 lines · 683 B · at 74947de

export class LoadError extends Error {  readonly kind = "load_failed" as const;  constructor(public readonly reason: string) {    super(reason);  }}export class CacheError extends Error {  readonly kind = "invalid_ttl" as const;  constructor() {    super("invalid_ttl");  }}export type Cache<V> = {  /** Rejects with the loader's LoadError when the load fails. */  get(key: string): Promise<V>;  invalidate(key: string): void;};/** Throws CacheError when ttlMs is not a positive integer. `loader` rejects with a LoadError on failure. */export function createCache<V>(loader: (key: string) => Promise<V>, ttlMs: number, now: () => number): Cache<V> {  // TODO}