Skip to content

Instantly share code, notes, and snippets.

@willie-hung
Created August 1, 2023 01:41
Show Gist options
  • Save willie-hung/87c16fbda81836791d9d3176342e3e76 to your computer and use it in GitHub Desktop.
Save willie-hung/87c16fbda81836791d9d3176342e3e76 to your computer and use it in GitHub Desktop.
post_30
app.get("/photos", async (req: Request, res: Response) => {
const albumId = req.query.albumId;
// Check the cache
const value = await client.get(`photos?albumId=${albumId}`);
if (value) {
console.log("Cache hit!");
res.json(JSON.parse(value));
} else {
console.log("Cache miss!");
const data = await axios.get(
"https://jsonplaceholder.typicode.com/photos",
{
params: {
albumId,
},
}
);
client.setEx(
`photos?albumId=${albumId}`,
DEFAULT_EXPIRATION,
JSON.stringify(data.data)
);
res.json(data.data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment