Skip to content

Commit 4b8d765

Browse files
authored
Cache failed registry lookups to avoid hammering APIs (#315)
1 parent 0950b75 commit 4b8d765

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/registries/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ const cache = require('../utils/cache');
99
const log = require('../utils/log');
1010
const { prioritiesHost } = require('../utils/url');
1111

12+
const ERR_PACKAGE_NOT_FOUND = 'ERR_PACKAGE_NOT_FOUND';
13+
1214
async function resolve(type, packageName) {
1315
const cacheKey = `${type}_${packageName}`;
1416

1517
const cacheValue = await cache.get(cacheKey);
1618

1719
if (cacheValue) {
18-
return cacheValue;
20+
if (cacheValue !== ERR_PACKAGE_NOT_FOUND) {
21+
return cacheValue;
22+
}
23+
24+
return undefined;
1925
}
2026

2127
const config = registryConfig[type];
@@ -34,7 +40,9 @@ async function resolve(type, packageName) {
3440
response = await got.get(requestUrl);
3541
} catch (err) {
3642
if (err.statusCode === 404) {
37-
return log('Package not found', packageName, type);
43+
log('Package not found', packageName, type);
44+
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
45+
return;
3846
}
3947

4048
return log(err);
@@ -45,6 +53,7 @@ async function resolve(type, packageName) {
4553
json = JSON.parse(response.body);
4654
} catch (err) {
4755
log('Parsing response failed');
56+
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
4857
return;
4958
}
5059

@@ -94,6 +103,7 @@ async function resolve(type, packageName) {
94103

95104
if (!reachableUrl) {
96105
log('No URL for package found');
106+
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
97107
return;
98108
}
99109

0 commit comments

Comments
 (0)