do non-fetch requests have request memoization?
Unanswered
piscopancer posted this in #help-forum
I use an async function to query the DB (turso, drizzle) in 3 non-client components
Will it make 3 requests to the DB or only one? I know request memoization applies to
// src/user.ts
export function getUser() {
// check auth, cookies/whatever, get their id
// query a db using their id like so: db.users.findFirst...
}Page
Header (await getUser)
ComponentOne (await getUser)
ComponentTwo (await getUser) Will it make 3 requests to the DB or only one? I know request memoization applies to
fetch by default, so much I know5 Replies
I checked Kyle's fine explanation on caching and learned about
unstable_cache function, which, as stated, puts data in a Data Cache, so I doubt the below is correct as I do not need any Data Caching, only request memoization...import { unstable_cache as cache} from 'next/cache';
import { auth } from './auth';
import { db } from './db';
export const getUser = cache(async ()=>{
const authUser = await auth().then((s) => s?.user)
const userId = authUser?.accountId
const dbUser = userId
? await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.id, userId),
})
: undefined
if (!authUser || !dbUser) return
return {authUser, dbUser}
})should I use react's
cache function? I think that's the right wayand next's
unstable_cache function is specifically for Data Caching where, let's say, time based revalidation can be usedtell me if I accidentally answered my own question
American Crow
i think you are looking for this info:
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-third-party-libraries
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-third-party-libraries
In cases where you're using a third-party library that doesn't support or expose fetch (for example, a database, CMS, or ORM client), you can configure the caching and revalidating behavior of those requests using the Route Segment Config Option and React's cache function.
Whether the data is cached or not will depend on whether the route segment is statically or dynamically rendered. If the segment is static (default), the output of the request will be cached and revalidated as part of the route segment. If the segment is dynamic, the output of the request will not be cached and will be re-fetched on every request when the segment is rendered.
You can also use the experimental unstable_cache API.
...