Function Caching
Answered
Taken posted this in #help-forum
TakenOP
How can I cache the result of this function with cache life since multiple inputs can return a single output.
querying with player can return a guild same with name and id
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
Answered by Great Gray Owl
import { cacheLife } from "next/cache"
import { guildSchema } from "../../schema/guild"
import { hypixelApi } from "./axios"
type GuildLookupType = "id" | "player" | "name"
async function resolveGuildId(
id: string,
type: GuildLookupType
): Promise<string | null> {
"use cache"
cacheLife("hypixelApi")
if (type === "id") return id
const res = await hypixelApi.auth.get("/guild", {
params: { [type]: id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild?._id ?? null
}
async function getGuildById(id: string) {
"use cache"
cacheLife("hypixelApi")
const res = await hypixelApi.auth.get("/guild", {
params: { id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild
}
export async function getGuild(
id: string,
type: GuildLookupType = "player"
) {
const guildId = await resolveGuildId(id, type)
if (!guildId) return null
return getGuildById(guildId)
}
import { guildSchema } from "../../schema/guild"
import { hypixelApi } from "./axios"
type GuildLookupType = "id" | "player" | "name"
async function resolveGuildId(
id: string,
type: GuildLookupType
): Promise<string | null> {
"use cache"
cacheLife("hypixelApi")
if (type === "id") return id
const res = await hypixelApi.auth.get("/guild", {
params: { [type]: id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild?._id ?? null
}
async function getGuildById(id: string) {
"use cache"
cacheLife("hypixelApi")
const res = await hypixelApi.auth.get("/guild", {
params: { id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild
}
export async function getGuild(
id: string,
type: GuildLookupType = "player"
) {
const guildId = await resolveGuildId(id, type)
if (!guildId) return null
return getGuildById(guildId)
}
4 Replies
Great Gray Owl
import { cacheLife } from "next/cache"
import { guildSchema } from "../../schema/guild"
import { hypixelApi } from "./axios"
type GuildLookupType = "id" | "player" | "name"
async function resolveGuildId(
id: string,
type: GuildLookupType
): Promise<string | null> {
"use cache"
cacheLife("hypixelApi")
if (type === "id") return id
const res = await hypixelApi.auth.get("/guild", {
params: { [type]: id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild?._id ?? null
}
async function getGuildById(id: string) {
"use cache"
cacheLife("hypixelApi")
const res = await hypixelApi.auth.get("/guild", {
params: { id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild
}
export async function getGuild(
id: string,
type: GuildLookupType = "player"
) {
const guildId = await resolveGuildId(id, type)
if (!guildId) return null
return getGuildById(guildId)
}
import { guildSchema } from "../../schema/guild"
import { hypixelApi } from "./axios"
type GuildLookupType = "id" | "player" | "name"
async function resolveGuildId(
id: string,
type: GuildLookupType
): Promise<string | null> {
"use cache"
cacheLife("hypixelApi")
if (type === "id") return id
const res = await hypixelApi.auth.get("/guild", {
params: { [type]: id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild?._id ?? null
}
async function getGuildById(id: string) {
"use cache"
cacheLife("hypixelApi")
const res = await hypixelApi.auth.get("/guild", {
params: { id },
})
if (res.status !== 200) return null
const parsed = guildSchema.safeParse(res.data)
if (!parsed.success) return null
return parsed.data.guild
}
export async function getGuild(
id: string,
type: GuildLookupType = "player"
) {
const guildId = await resolveGuildId(id, type)
if (!guildId) return null
return getGuildById(guildId)
}
Answer
Great Gray Owl
you can catch your result with this code.
Please try again and let me know if you still have some problems.
TakenOP
I will try that later when