Skip to content

API Reference

createCache(config)

The main entry point for initializing the caching layer.

OptionTypeDefaultDescription
adapterCacheClientRequired. Storage adapter (Memory, Redis, Memcached).
staleTimenumber60Seconds data stays fresh (TanStack: staleTime).
gcTimenumber300Seconds stale data stays in cache (TanStack: gcTime).
swrbooleanfalseEnable background revalidation.
stampedebooleantruePrevent "thundering herd" via request coalescing.
varystring[][]Headers to namespace caches (e.g., ['Authorization']).
sortQuerybooleanfalseSort query params alphabetically for higher hit rates.
maxBodySizenumber2097152Max response size in bytes (default: 2MB).
autoInvalidatebooleanfalseAuto-invalidate route patterns on successful mutations.
retrynumber0Number of retries with exponential backoff (fetch only).
keyPrefixstring"erc:"Global prefix for all cache keys in Redis/Memcached.
enabledbooleantrueToggle caching globally.
keystring | fn(Route only) Manual key override (string or (req) => string).

Returns

  • middleware(): Global Express middleware.
  • route(options): Per-route middleware with overrides.
  • fetch(key, fetcher, options): Standalone data caching method.
  • invalidate(...patterns): Middleware to trigger invalidation.
  • invalidateRoute(...patterns): Programmatic invalidation method.
  • adapter: The underlying storage adapter instance.

cache.route(overrides)

Use this to apply specific caching rules to individual routes.

ts
app.get('/heavy-report', cache.route({ 
  staleTime: 3600, // 1 hour
  swr: true 
}), handler);

cache.fetch(key, fetcher, options)

Standalone method for manual data caching. Includes full SWR, Stampede Protection, and Retries.

ts
const data = await cache.fetch('my-key', async () => {
  return await db.users.findMany();
}, { 
  staleTime: 60, 
  swr: true, 
  retry: 3 
});

cache.invalidate(...patterns)

Express middleware that increments the epoch version for specific patterns upon successful (2xx) response.

ts
app.post('/api/posts', cache.invalidate('/api/posts'), createPost);

cache.invalidateRoute(...patterns)

A programmatic way to invalidate routes from outside an Express request context.

ts
await cache.invalidateRoute('/api/users/123');

Released under the MIT License.