On this page

A Cache Store is responsible for storing and retrieving cached responses. It is also responsible for deciding which specific response to use based off of a response's Vary header (if present). It is expected to be compliant with RFC-9111.

The MemoryCacheStore stores the responses in-memory.

Options

  • maxSize - The maximum total size in bytes of all stored responses. Default 104857600 (100MB).
  • maxCount - The maximum amount of responses to store. Default 1024.
  • maxEntrySize - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default 5242880 (5MB).

Returns the current total size in bytes of all stored responses.

MemoryCacheStore.isFull(): void

Returns a boolean indicating whether the cache has reached its maximum size or count.

Emitted when the cache exceeds its maximum size or count limits. The event payload contains size, maxSize, count, and maxCount properties.

The SqliteCacheStore stores the responses in a SQLite database. Under the hood, it uses Node.js' node:sqlite api. The SqliteCacheStore is only exposed if the node:sqlite api is present.

Options

  • location - The location of the SQLite database to use. Default :memory:.
  • maxCount - The maximum number of entries to store in the database. Default Infinity.
  • maxEntrySize - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default Infinity.

The store must implement the following functions:

Optional. This tells the cache interceptor if the store is full or not. If this is true, the cache interceptor will not attempt to cache the response.

Parameters:

  • req Dispatcher.RequestOptions - Incoming request

Returns: GetResult | Promise<GetResult | undefined> | undefined - If the request is cached, the cached response is returned. If the request's method is anything other than HEAD, the response is also returned. If the request isn't cached, undefined is returned.

The get method may return a Promise for async cache stores (e.g. Redis-backed or remote stores). The cache interceptor handles both synchronous and asynchronous return values, including in revalidation paths (304 Not Modified handling and stale-while-revalidate background revalidation).

Response properties:

  • response CacheValue - The cached response data.
  • body Readable | Iterable<Buffer> | undefined - The response's body. This can be an array of Buffer chunks (with a .values() method) or a Readable stream. Both formats are supported in all code paths, including 304 revalidation.

Parameters:

  • req Dispatcher.RequestOptions - Incoming request
  • value CacheValue - Response to store

Returns: Writable | undefined - If the store is full, return undefined. Otherwise, return a writable so that the cache interceptor can stream the body and trailers to the store.

This is an interface containing the majority of a response's data (minus the body).

number - The response's HTTP status code.

string - The response's HTTP status message.

Buffer[] - The response's headers.

Record<string, string | string[] | null> | undefined - The headers defined by the response's Vary header and their respective values for later comparison. Values are null when the header specified in Vary was not present in the original request. These null values are automatically filtered out during revalidation so they are not sent as request headers.

For example, for a response like

Vary: content-encoding, accepts
content-encoding: utf8
accepts: application/json

This would be

{
  'content-encoding': 'utf8',
  accepts: 'application/json'
}

If the original request did not include the accepts header:

{
  'content-encoding': 'utf8',
  accepts: null
}

number - Time in millis that this value was cached.

number - Time in millis that this value is considered stale.

number - Time in millis that this value is to be deleted from the cache. This is either the same sa staleAt or the max-stale caching directive.

The store must not return a response after the time defined in this property.

This extends Node's Readable and defines extra properties relevant to the cache interceptor.

The response's CacheStoreValue

This extends Node's Writable and defines extra properties relevant to the cache interceptor.

If the response has trailers, the cache interceptor will pass them to the cache interceptor through this method.