#Effectively using caches

11 messages · Page 1 of 1 (latest)

dreamy stag
#

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.

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.

austere condorBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

bright sleet
#

bump

dreamy stag
#

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.

dreamy stag
#

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.

bright sleet
dreamy stag
#

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.

bright sleet
bright sleet
#

@dreamy stag ?

dreamy stag
#

I dunno. That’s partly why I asked originally