[next.js] should i cache this query?

i have a function that queries my database for a list of stories. if a user is logged in, the stories shown are automatically filtered based on categories and authors they have chosen to hide. should i cache the query results? i think that if i wrap the entire thing in unstable_cache, those query results will be shown to every user, but i'm not 100% sure.
1 Reply
.❀ tablet ❀.
.❀ tablet ❀.OP2w ago
here's the query code, for anyone interested. getCurrentUser grabs the currently logged in user for that request based on cookies.
export default async function (
query: any,
limit: number = 0,
json: boolean = false,
page?: number,
sort?: any,
): Promise<{ stories: IStory[]; total: number }> {

let skipAmt = limit * ((page || 1) - 1);
if (skipAmt < 0) skipAmt = 0;
query["chapters.hidden"] = false;
const currentUser = await getCurrentUser()
const ficmasArray = await getFicmasData();
if (currentUser) {
if (!query.author) query.author = {};
if (!query["chapters.bands"]) query["chapters.bands"] = {};
query["chapters.bands"]["$nin"] = currentUser.hiddenBands;
query["author"]["$nin"] = currentUser.hiddenAuthors;
}
query["ficmas"] = {
$nin: ficmasArray!.map((a) => a._id),
};
let stories = await Story.find(query, null)
.collation({ locale: "en" })
.sort(sort ? sort : { "chapters.posted": -1 })
.populate("ficmas")
.populate("ficmas.wisher", "username _id", "User",)
.populate("coAuthor", "username _id profile")
.populate("chapters.bands")
.populate("challenge")
.populate("author", "username _id profile")
.skip(skipAmt)
.limit(limit || 0)
.exec();
let res = stories.filter((a) => a.author != null);
log.debug(JSON.stringify(query), { label: "list query" });
return {
total: await Story.countDocuments(query).exec(),
stories: res.map(x => (!json ? x.toObject({flattenObjectIds: true}) : x.toJSON({flattenObjectIds: true})) as IStory),
};
}
export default async function (
query: any,
limit: number = 0,
json: boolean = false,
page?: number,
sort?: any,
): Promise<{ stories: IStory[]; total: number }> {

let skipAmt = limit * ((page || 1) - 1);
if (skipAmt < 0) skipAmt = 0;
query["chapters.hidden"] = false;
const currentUser = await getCurrentUser()
const ficmasArray = await getFicmasData();
if (currentUser) {
if (!query.author) query.author = {};
if (!query["chapters.bands"]) query["chapters.bands"] = {};
query["chapters.bands"]["$nin"] = currentUser.hiddenBands;
query["author"]["$nin"] = currentUser.hiddenAuthors;
}
query["ficmas"] = {
$nin: ficmasArray!.map((a) => a._id),
};
let stories = await Story.find(query, null)
.collation({ locale: "en" })
.sort(sort ? sort : { "chapters.posted": -1 })
.populate("ficmas")
.populate("ficmas.wisher", "username _id", "User",)
.populate("coAuthor", "username _id profile")
.populate("chapters.bands")
.populate("challenge")
.populate("author", "username _id profile")
.skip(skipAmt)
.limit(limit || 0)
.exec();
let res = stories.filter((a) => a.author != null);
log.debug(JSON.stringify(query), { label: "list query" });
return {
total: await Story.countDocuments(query).exec(),
stories: res.map(x => (!json ? x.toObject({flattenObjectIds: true}) : x.toJSON({flattenObjectIds: true})) as IStory),
};
}
Want results from more Discord servers?
Add your server