TypeScript
Public
Next.js Cached Data with use cache
Cache a server data function and attach a reusable invalidation tag.
#nextjs
#caching
#server-components
#typescript
TypeScript
// app/lib/posts.ts
import {
cacheLife,
cacheTag,
} from 'next/cache';
export async function getPublishedPosts() {
'use cache';
cacheLife('hours');
cacheTag('posts');
return db.post.findMany({
where: {
published: true,
},
orderBy: {
createdAt: 'desc',
},
take: 20,
});
}