Effectively using caches
Unanswered
Gouty oak gall posted this in #help-forum
Gouty oak gallOP
Hi, I have a list of about 50 objects that come from a database that need to be looked up and referenced A LOT. I had originally created a function like this and wrapped it in the unstable cache to minimize database traffic.
Now, I am seeing that my usage data is blowing up with Data Cache Reads, which I assume (but can't confirm) is related to this. Given that this data is pretty static, is there a better more efficient and cost-effective way to do this?
I was thinking of creating a static /api/getShowMapByUrl page that would get built during build time. But I think that would also be cached and cost against Data Cache Reads, is that correct?
Open to any ideas! Thanks.
export const getShowMapByUrl = unstable_cache(async (): Promise<string> =>
{
console.log("getShowMapByUrl called (cached)");
const client = await clientPromise;
const db = client.db(MAINDB);
const collection = db.collection(SHOW_COLL);
const showsMap = new Map<string, ShowType>();
const cursor = collection.find({},{projection:{}});
await cursor.forEach((show: ShowType) => {
if (show.internalUrl) {
showsMap.set(show.internalUrl.toLowerCase(), show);
}
})
return JSON.stringify(Array.from(showsMap.entries()));;
},[],{revalidate:false,tags:["showlist"]});Now, I am seeing that my usage data is blowing up with Data Cache Reads, which I assume (but can't confirm) is related to this. Given that this data is pretty static, is there a better more efficient and cost-effective way to do this?
I was thinking of creating a static /api/getShowMapByUrl page that would get built during build time. But I think that would also be cached and cost against Data Cache Reads, is that correct?
Open to any ideas! Thanks.
10 Replies
bump
Gouty oak gallOP
For the record, based on experimentation, the use of unstable_cache here was the culprit to my high Data Cache Read count. I did a hacky workaround that seems to balance performance and cost, but its silly in my eyes. I wish there was some more clarity on this.
Gouty oak gallOP
So at first what I did was a create an api endpoint that made the DB call but setup the endpoint so that it would use static generation. The call to the endpoint was marked with no-cache so that it didn't cause data cache reads from the client side. This worked to reduce data cache usage. However, it caused a spike in my serverless function invocation count.
So now I literally created a JS file with the hardcoded response from the database in JSON. This works since the data is not too volatile, but its not a real long term solution.
So now I literally created a JS file with the hardcoded response from the database in JSON. This works since the data is not too volatile, but its not a real long term solution.
Gouty oak gallOP
Yea, I wish I had a better one. Paying for cache reads makes this hard, because you pay if its cached, pay for invocations and duration if its not cached. So the only cheap option is if its static.
@Gouty oak gall Yea, I wish I had a better one. Paying for cache reads makes this hard, because you pay if its cached, pay for invocations and duration if its not cached. So the only cheap option is if its static.
Are there no better solutions? Like using a different caching tool? Like instead of using the data cache then using idk: redis cache, or whatever?
@Gouty oak gall ?
Gouty oak gallOP
I dunno. That’s partly why I asked originally
yea, I guess