Next.js Discord

Discord Forum

Function Caching

Unanswered
Taken posted this in #help-forum
Open in Discord
How can I cache the result of this function with cache life since multiple inputs can return a single output.

import { cacheLife } from "next/cache"
import { guildSchema } from "../../schema/guild"
import { hypixelApi } from "./axios"

export async function getGuild(id: string, type: "id" | "player" | "name" = "player") {
    "use cache"

    if (process.env.NODE_ENV === "production") {
        cacheLife("hypixelApi")
    }

    const res = await hypixelApi.auth.get(`/guild`, { params: { [type]: id } })
    if (res.status !== 200) return null

    const { success, data } = guildSchema.safeParse(await res.data)
    if (!success) return null

    return data.guild
}


querying with player can return a guild same with name and id

0 Replies