Fix caching issue

Fixes #138
This commit is contained in:
Michael Jackson 2018-10-18 17:41:10 -07:00
parent 9dbe412783
commit 9b9ded57e7
1 changed files with 3 additions and 7 deletions

View File

@ -15,21 +15,17 @@ function getNpmPackageInfo(packageName) {
} else {
fetchNpmPackageInfo(packageName).then(value => {
if (value == null) {
resolve(null);
// Cache 404s for 5 minutes. This prevents us from making
// unnecessary requests to the registry for bad package names.
// In the worst case, a brand new package's info will be
// available within 5 minutes.
cache.setex(key, 300, notFound);
resolve(null);
} else {
resolve(value);
// Cache valid package info for 1 minute. In the worst case,
// new versions won't be available for 1 minute.
cache.setnx(key, JSON.stringify(value), (error, reply) => {
if (reply === 1) cache.expire(key, 60);
});
cache.setex(key, 60, JSON.stringify(value));
resolve(value);
}
}, reject);
}