Skip to content

Commit

Permalink
fix: add cache for developers
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenme committed Nov 11, 2019
1 parent 9ba0488 commit 9a7f143
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ app.get('/developers', async (req, res) => {
try {
const { language, since } = req.query;
const cacheKey = `developers::${language || 'nolang'}::${since || 'daily'}`;
const cacheKeyPerm = `perm::${cacheKey}`;
const cached = cache.get(cacheKey);
const cachedPerm = cache.get(cacheKeyPerm);

res.cacheControl = {
maxAge: 120,
Expand All @@ -78,8 +80,13 @@ app.get('/developers', async (req, res) => {
return res.json(cached);
}
const data = await fetchDevelopers({ language, since });
cache.put(cacheKey, data, 1000 * 3600); // Store for a hour
res.json(data);
if (data && data.length > 0) {
cache.put(cacheKey, data, 1000 * 3600); // Store for a hour
cache.put(cacheKeyPerm, data);
return res.json(data);
} else {
return res.json(cachedPerm || data);
}
} catch (err) {
console.error(err);
res.status(500).send(err.toJSON());
Expand Down

0 comments on commit 9a7f143

Please sign in to comment.