Skip to content

Instantly share code, notes, and snippets.

@mhinton
Created November 29, 2023 17:10
Show Gist options
  • Save mhinton/9625a993a83ecdedbe8cf73a0c237019 to your computer and use it in GitHub Desktop.
Save mhinton/9625a993a83ecdedbe8cf73a0c237019 to your computer and use it in GitHub Desktop.
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