Skip to content

Caching

When caching is enabled, results with the same hash are reused within the TTL and auto-reloaded after expiry.

Basic usage

typescript
const loader = new AsyncLoader(
    (args) => fetch("/api/data", { signal: args.abortSignal }).then((r) => r.json()),
    { cache: 60_000, hash: "data" } // cache for 60 seconds
);

await loader.get(); // first load, writes to cache
await loader.get(); // cache hit, underlying not called

loader.clear(); // clear current instance's cache entry
loader.clearAll(); // clear all cache entries (shared storage)

Key points

  • cache is the TTL in milliseconds. =0 disables caching (default); >0 enables it.
  • hash is the cache key. When cache>0 but no hash is provided, an instance-level hash is auto-generated.
  • Cache hits do not trigger onPending / onFulfilled callbacks.
  • The storage option lets you swap the cache backend; the default is MapStorage.

Clearing the cache

MethodDescription
clear()Clear the current instance's cache entry
clearAll()Clear all cache entries in the shared storage

vs. invalidation

clear() only makes sense with caching enabled; to mark data stale even without a cache, use invalidate() from Refresh & Invalidate.

Released under the MIT License.