Data Caching
The cache function allows you to cache the results of data fetching or computations. It provides fine-grained control over data and is suitable for scenarios such as Client-Side Rendering (CSR) and Server-Side Rendering (SSR).
Basic Usage
Parameters
fetchData: ThefetchDatafunction in a DataLoader.options(optional): Cache configuration.tag: A tag to identify the cache, which can be used to invalidate it.maxAge: The cache's validity period (in milliseconds).revalidate: A time window for revalidating the cache (in milliseconds), similar to thestale-while-revalidatefeature of HTTP Cache-Control.getKey: A simplified cache key generation function that creates a key based on the function's arguments.onCache: A callback function for when data is cached, used for custom handling of cached data.
The type for the options parameter is as follows:
Return Value
The cache function returns a new fetchData function with caching capabilities. Calling this new function multiple times will not result in repeated execution.
Scope of Use
This function is only supported for use within a DataLoader.
Detailed Usage
maxAge
After each computation, the framework records the time the data was written to the cache. When the function is called again, it checks if the cache has expired based on the maxAge parameter. If it has, the fn function is re-executed; otherwise, the cached data is returned.
revalidate
The revalidate parameter sets a time window for revalidating the cache after it has expired. It can be used with the maxAge parameter, similar to the stale-while-revalidate model of HTTP Cache-Control.
In the following example, if getDashboardStats is called within the 2-minute non-expired window, it returns cached data. If the cache is expired (between 2 and 3 minutes), incoming requests will first receive the old data, and then a new request will be made in the background to update the cache.
tag
The tag parameter is used to identify a cache with a label, which can be a string or an array of strings. This tag can be used to invalidate the cache, and multiple cache functions can share the same tag.
getKey
The getKey parameter allows you to customize how cache keys are generated. For example, you might only need to rely on a subset of the function's parameters to differentiate caches. It is a function that receives the same arguments as the original function and returns a string as the cache key:
You can also use the generateKey function with getKey to generate cache keys:
The generateKey function ensures that a consistent, unique key is generated even if the order of object properties changes, guaranteeing stable caching.
customKey
Both customKey and getKey let you customize cache keys; the difference is scope:
getKeyonly overrides the "arguments" portion of the key — the function itself still participates in the cache-key computation. Two different functions whosegetKeyreturns the same value will not share the cache.customKeyreturns the entire final cache key, replacing the framework's default "function + arguments" key. It can therefore let different functions share the same cache.
If you only want to derive the cache key from some arguments, use getKey. Use customKey only when you need to share caches across functions or similar advanced cases.
customKey receives an object with the following properties and must return a string or Symbol — that value is used directly as the final cache key:
params: The arguments passed to the cached function.fn: The original cached function.generatedKey: The original cache key automatically generated by the framework based on the input parameters.
The example below demonstrates how customKey enables cache sharing across functions:
onCache
The onCache parameter allows you to track cache statistics, such as hit rates. It is a callback function that receives information about each cache operation, including its status, key, parameters, and result. You can return false from onCache to prevent a cache hit.
The onCache callback receives an object with the following properties:
status: The status of the cache operation, which can be:hit: Cache hit, returns cached content.miss: Cache miss, executes the function and caches the result.stale: Cache hit but the data is stale, returns cached content and revalidates in the background.
key: The cache key, which may be the result ofcustomKeyor the default generated key.params: The arguments passed to the cached function.result: The result data (either from the cache or newly computed).
This callback is only called when the options parameter is provided. When using a cache function without options, the onCache callback is not invoked.
The onCache callback is very useful for:
- Monitoring cache performance.
- Calculating hit rates.
- Logging cache operations.
- Implementing custom metrics.
Storage
Currently, whether on the client or server, the cache is stored in memory. By default, the shared storage limit for all cache functions is 1GB. When this limit is reached, the LRU (Least Recently Used) algorithm is used to remove old caches.
Considering that the content cached by the cache function is not expected to be very large, it is currently stored in memory by default.
You can specify the cache storage limit using the configureCache function: