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