APICacheclass

new APICache<P, R>(provider: APIProvider<P, R>)
ParamType
providerAPIProvider<P, R>
The APIProvider used to fetch results for every cached endpoint. required
Return
APICache<P, R>
Cache of EndpointCache objects keyed by Endpoint, providing memoised API results across many endpoints.
PropertyType
.providerAPIProvider<P, R>
The underlying APIProvider that backs every cached endpoint. required readonly

Cache of EndpointCache objects keyed by Endpoint, providing memoised API results across many endpoints.

  • Use get(endpoint) to retrieve or create the EndpointCache for a given endpoint, then get(payload) on that to get a specific EndpointStore.
  • Disposing the cache disposes every nested EndpointCache and clears the map.

Examples

const cache = new APICache(provider);
const user = await cache.call(getUser, { id: "abc" });

Methods

Go

APICache.get()method

Get (or create) the EndpointCache for the given endpoint.

get(endpoint: Endpoint<PP, RR>): EndpointCache<PP, RR>
get(endpoint: Endpoint): EndpointCache
Go

APICache.call()method

Fetch (or return a cached result) for the given endpoint and payload.

call(endpoint: Endpoint<PP, RR>, payload: PP, maxAge: number = AVOID_REFRESH, caller: AnyCaller = this.call): Promise<RR>
Go

APICache.invalidate()method

Invalidate a specific store for an endpoint so the next read refetches.

invalidate(endpoint: Endpoint<PP, RR>, payload: PP): void
Go

APICache.invalidateAll()method

Invalidate all stores for an endpoint so the next read of any payload refetches.

invalidateAll(endpoint: Endpoint<PP, RR>): void
Go

APICache.refresh()method

Trigger a refetch on a specific store for an endpoint.

refresh(endpoint: Endpoint<PP, RR>, payload: PP, maxAge?: number): void
Go

APICache.refreshAll()method

Trigger a refetch on all stores for an endpoint.

refreshAll(endpoint: Endpoint<PP, RR>, maxAge?: number): void