Created
August 1, 2023 01:41
-
-
Save willie-hung/87c16fbda81836791d9d3176342e3e76 to your computer and use it in GitHub Desktop.
post_30
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
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