The cache
function allows you to cache the results of data fetching or computation.
fn
: The data fetching or computation function to be cachedoptions
(optional): Cache configuration
tag
: Tag to identify the cache, which can be used to invalidate the cachemaxAge
: Cache validity period (milliseconds)revalidate
: Time window for revalidating the cache (milliseconds), similar to HTTP Cache-Control's stale-while-revalidate functionalityThe type of the options
parameter is as follows:
The cache
function returns a new function with caching capabilities. Multiple calls to this function will not re-execute the fn
function.
Unlike React's cache function which can only be used in server components,
EdenX's cache
function can be used in any frontend or server-side code.
options
ParameterWhen no options
parameter is provided, it's primarily useful in SSR projects, the cache lifecycle is limited to a single SSR rendering request. For example, when the same cachedFn is called in multiple data loaders, the cachedFn function won't be executed repeatedly. This allows data sharing between different data loaders while avoiding duplicate requests. EdenX will re-execute the fn
function with each server request.
Without the options
parameter, it can be considered a replacement for React's cache
function and can be used in any server-side code (such as in data loaders of SSR projects), not limited to server components.
options
ParametermaxAge
ParameterAfter each computation, the framework records the time when the cache is written.
When the function is called again, it checks if the cache has expired based on the maxAge
parameter.
If expired, the fn
function is re-executed; otherwise, the cached data is returned.
revalidate
ParameterThe revalidate
parameter sets a time window for revalidating the cache after it expires. It can be used together with the maxAge
parameter, similar to HTTP Cache-Control's stale-while-revalidate mode.
In the following example, within the 2-minute period before the cache expires, calls to getDashboardStats
will return cached data. If the cache has expired (between 2 and 3 minutes), requests will first return the old data, then refresh the data in the background and update the cache.
tag
ParameterThe tag
parameter identifies the cache with a tag, which can be a string or an array of strings.
You can invalidate caches based on this tag, and multiple cache functions can use the same tag.
Currently, both client and server caches are stored in memory. The default storage limit for all cached functions is 1GB. When this limit is reached, the oldest cache is removed using an LRU algorithm.
Considering that the results of cache
function caching are not large, they are currently stored in memory by default.
You can specify the storage limit using the configureCache
function: