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 various scenarios, including Client-Side Rendering (CSR), Server-Side Rendering (SSR), and API services (BFF).
fetchData
: The fetchData
function 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 the stale-while-revalidate
feature 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:
The cache
function returns a new fetchData
function with caching capabilities. Calling this new function multiple times will not result in repeated execution.
This function is only supported for use within a DataLoader.
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 provided by Modern.js with getKey
to generate cache keys:
The generateKey
function in Modern.js ensures that a consistent, unique key is generated even if the order of object properties changes, guaranteeing stable caching.
customKey
The customKey
parameter is used to customize the cache key. It is a function that receives an object with the following properties and must return a string or Symbol to be used as the cache key:
params
: An array of arguments passed to the cached function.fn
: A reference to the original cached function.generatedKey
: The original cache key automatically generated by the framework based on the input parameters.Generally, the cache will be invalidated in the following scenarios:
maxAge
condition is not met.revalidateTag
is called.customKey
can be used in scenarios where you want to share a cache even if the function reference is different. If you only need to customize the cache key, it is recommended to use getKey
.
This is very useful in certain scenarios, such as when the function reference changes, but you still want to return cached data.
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 of customKey
or 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:
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: