Switch back to in-memory cache

This commit is contained in:
Michael Jackson 2018-12-17 15:57:44 -08:00
parent aa99d11068
commit 59a5acbe08
7 changed files with 91 additions and 44 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
/node_modules/ /node_modules/
/public/_assets/ /public/_assets/
/dump.rdb
/stats.json /stats.json
npm-debug.log* npm-debug.log*

View File

@ -1,8 +1,6 @@
const cache = require('../../utils/cache');
const data = require('../../utils/data'); const data = require('../../utils/data');
function closeDatabase() { function closeDatabase() {
cache.quit();
data.quit(); data.quit();
} }

View File

@ -1,9 +1,23 @@
const redis = require('redis'); const LRUCache = require('lru-cache');
redis.debug_mode = process.env.DEBUG_REDIS != null; const maxMegabytes = 40; // Cap the cache at 40 MB
const maxLength = maxMegabytes * 1024 * 1024;
const client = redis.createClient( const maxSeconds = 60;
process.env.CACHE_URL || process.env.OPENREDIS_URL || 'redis://localhost:6379' const maxAge = maxSeconds * 1000;
);
module.exports = client; const cache = new LRUCache({
max: maxLength,
maxAge: maxAge,
length: Buffer.byteLength
});
function get(key) {
return cache.get(key);
}
function setex(key, ttlSeconds, value) {
return cache.set(key, value, ttlSeconds * 1000);
}
module.exports = { get, setex };

View File

@ -9,7 +9,7 @@ const logging = require('./logging');
function fetchNpmPackage(packageConfig) { function fetchNpmPackage(packageConfig) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const tarballURL = packageConfig.dist.tarball; const tarballURL = packageConfig.tarballURL;
logging.debug( logging.debug(
'Fetching package for %s from %s', 'Fetching package for %s from %s',

View File

@ -1,15 +1,18 @@
const cache = require('./cache'); const cache = require('./cache');
const fetchNpmPackageInfo = require('./fetchNpmPackageInfo'); const fetchNpmPackageInfo = require('./fetchNpmPackageInfo');
const notFound = 0; const notFound = '';
function cleanPackageConfig(packageConfig) { function cleanPackageConfig(packageConfig) {
return { return {
name: packageConfig.name, name: packageConfig.name,
version: packageConfig.version, version: packageConfig.version,
dependencies: packageConfig.dependencies, dependencies: Object.assign(
peerDependencies: packageConfig.peerDependencies, {},
dist: packageConfig.dist packageConfig.dependencies,
packageConfig.peerDependencies
),
tarballURL: packageConfig.dist.tarball
}; };
} }
@ -26,32 +29,31 @@ function cleanPackageInfo(packageInfo) {
function getNpmPackageInfo(packageName) { function getNpmPackageInfo(packageName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const key = `npmPackageInfo-${packageName}`; const key = `npmPackageInfo-${packageName}`;
const value = cache.get(key);
cache.get(key, (error, value) => { if (value != null) {
if (error) { console.log('GOT VALUE');
reject(error); resolve(value === notFound ? null : JSON.parse(value));
} else if (value != null) { } else {
resolve(value === notFound ? null : JSON.parse(value)); console.log('NO VALUE');
} else { fetchNpmPackageInfo(packageName).then(value => {
fetchNpmPackageInfo(packageName).then(value => { if (value == null) {
if (value == 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);
resolve(null); } else {
} else { value = cleanPackageInfo(value);
const cachedValue = JSON.stringify(cleanPackageInfo(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.setex(key, 60, cachedValue); cache.setex(key, 60, JSON.stringify(value));
resolve(value); resolve(value);
} }
}, reject); }, reject);
} }
});
}); });
} }

45
package-lock.json generated
View File

@ -4254,11 +4254,27 @@
"y18n": "^4.0.0" "y18n": "^4.0.0"
}, },
"dependencies": { "dependencies": {
"lru-cache": {
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz",
"integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==",
"dev": true,
"requires": {
"pseudomap": "^1.0.2",
"yallist": "^2.1.2"
}
},
"y18n": { "y18n": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz",
"integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==",
"dev": true "dev": true
},
"yallist": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=",
"dev": true
} }
} }
}, },
@ -5036,6 +5052,24 @@
"lru-cache": "^4.0.1", "lru-cache": "^4.0.1",
"shebang-command": "^1.2.0", "shebang-command": "^1.2.0",
"which": "^1.2.9" "which": "^1.2.9"
},
"dependencies": {
"lru-cache": {
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz",
"integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==",
"dev": true,
"requires": {
"pseudomap": "^1.0.2",
"yallist": "^2.1.2"
}
},
"yallist": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=",
"dev": true
}
} }
}, },
"crypt": { "crypt": {
@ -9989,12 +10023,10 @@
"dev": true "dev": true
}, },
"lru-cache": { "lru-cache": {
"version": "4.1.4", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.4.tgz", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-EPstzZ23znHUVLKj+lcXO1KvZkrlw+ZirdwvOmnAnA/1PB4ggyXJ77LRkCqkff+ShQ+cqoxCxLQOh4cKITO5iA==", "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==",
"dev": true,
"requires": { "requires": {
"pseudomap": "^1.0.2",
"yallist": "^3.0.2" "yallist": "^3.0.2"
} }
}, },
@ -16416,8 +16448,7 @@
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz",
"integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==", "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A=="
"dev": true
}, },
"yargs": { "yargs": {
"version": "10.1.2", "version": "10.1.2",

View File

@ -27,6 +27,7 @@
"invariant": "^2.2.2", "invariant": "^2.2.2",
"isomorphic-fetch": "^2.2.1", "isomorphic-fetch": "^2.2.1",
"jsonwebtoken": "^8.1.0", "jsonwebtoken": "^8.1.0",
"lru-cache": "^5.1.1",
"mime": "^1.4.0", "mime": "^1.4.0",
"morgan": "^1.8.1", "morgan": "^1.8.1",
"ndjson": "^1.5.0", "ndjson": "^1.5.0",