Use SETNX for caching package info
This commit is contained in:
parent
7543295e4e
commit
c7231b4c5e
@ -1,32 +0,0 @@
|
|||||||
const db = require("./cache");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A persistent cache for JSON values, using Redis.
|
|
||||||
*/
|
|
||||||
function createCache(keyPrefix) {
|
|
||||||
function createKey(key) {
|
|
||||||
return keyPrefix + "-" + key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function set(key, value, expiry, callback) {
|
|
||||||
db.setex(createKey(key), expiry, JSON.stringify(value), callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
function get(key, callback) {
|
|
||||||
db.get(createKey(key), (error, value) => {
|
|
||||||
callback(error, value && JSON.parse(value));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function del(key, callback) {
|
|
||||||
db.del(createKey(key), callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
set,
|
|
||||||
get,
|
|
||||||
del
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = createCache;
|
|
@ -1,16 +1,17 @@
|
|||||||
const createCache = require("./createCache");
|
const cache = require("./cache");
|
||||||
const createMutex = require("./createMutex");
|
const createMutex = require("./createMutex");
|
||||||
const fetchNpmPackageInfo = require("./fetchNpmPackageInfo");
|
const fetchNpmPackageInfo = require("./fetchNpmPackageInfo");
|
||||||
|
|
||||||
const cache = createCache("packageInfo");
|
|
||||||
const notFound = "PackageNotFound";
|
const notFound = "PackageNotFound";
|
||||||
|
|
||||||
const mutex = createMutex((packageName, callback) => {
|
const mutex = createMutex((packageName, callback) => {
|
||||||
cache.get(packageName, (error, value) => {
|
const key = `packageInfo2-${packageName}`;
|
||||||
|
|
||||||
|
cache.get(key, (error, value) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else if (value != null) {
|
} else if (value != null) {
|
||||||
callback(null, value === notFound ? null : value);
|
callback(null, value === notFound ? null : JSON.parse(value));
|
||||||
} else {
|
} else {
|
||||||
fetchNpmPackageInfo(packageName).then(
|
fetchNpmPackageInfo(packageName).then(
|
||||||
value => {
|
value => {
|
||||||
@ -19,21 +20,22 @@ const mutex = createMutex((packageName, callback) => {
|
|||||||
// 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.set(packageName, notFound, 300, () => {
|
cache.setex(key, 300, notFound, () => {
|
||||||
callback(null, value);
|
callback(null, null);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Cache valid package info for 1 minute.
|
// Cache valid package info for 1 minute. In the worst case,
|
||||||
cache.set(packageName, value, 60, () => {
|
// new versions won't be available for 1 minute.
|
||||||
|
cache.setnx(key, JSON.stringify(value), (error, reply) => {
|
||||||
|
if (reply === 1) cache.expire(key, 60);
|
||||||
callback(null, value);
|
callback(null, value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
// Do not cache errors.
|
// Do not cache errors.
|
||||||
cache.del(packageName, () => {
|
cache.del(key);
|
||||||
callback(error);
|
callback(error);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user