banner
Tenifs

Tenifs

雄关漫道真如铁,而今迈步从头越。
github
follow
zhihu
email

Next.js uses caching to handle performance-intensive operations

If your application has highly performance-intensive data operations, such as complex database queries or API requests, using caching to improve performance is very important.

import { getUser } from './data';
import { unstable_cache } from 'next/cache';
 
const getCachedUser = unstable_cache(
  async (id) => getUser(id),
  ['my-app-user']
);
 
export default async function Component({ userID }) {
  const user = await getCachedUser(userID);
  // ...
}

unstable_cache returns a function to handle cached data. If the data is not in the cache, it will call the data request function passed in to fetch the data, cache the retrieved data, and return it.

Note: Do not cache data that requires authentication. A good practice is to only cache public data.

unstable_cache is an unstable API that may change in future versions. For specific usage, refer to the official documentation.

Next.js Version: v14.0.0

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.