Created
November 29, 2023 17:10
-
-
Save mhinton/9625a993a83ecdedbe8cf73a0c237019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { consola } from "consola"; | |
export default defineCachedEventHandler(async (event) => { | |
const query = getQuery(event); | |
const menuName = query.menuName as string; | |
const key = `cache:menu:${menuName}`; | |
const storage = useStorage(); | |
let menu = await storage.getItem(key); | |
if (menu) { | |
consola.info(`return cached response for ${menuName}`); | |
return menu; | |
} else { | |
consola.info(`return new response for ${menuName}`); | |
await storage.setItem(key, { menu: { foo: "bar" }, ts: Date.now() }); | |
menu = await storage.getItem(key); | |
return menu; | |
} | |
}, { | |
maxAge: 1, // cache for 1 second | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment