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

View File

@ -1,8 +1,6 @@
const cache = require('../../utils/cache');
const data = require('../../utils/data');
function closeDatabase() {
cache.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(
process.env.CACHE_URL || process.env.OPENREDIS_URL || 'redis://localhost:6379'
);
const maxSeconds = 60;
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) {
return new Promise((resolve, reject) => {
const tarballURL = packageConfig.dist.tarball;
const tarballURL = packageConfig.tarballURL;
logging.debug(
'Fetching package for %s from %s',

View File

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