Use SETEX for storing package.json configs
Also, remove option for in-memory cache.
This commit is contained in:
parent
f297ba2217
commit
84f297b400
@ -19,7 +19,6 @@
|
|||||||
"http-client": "^4.3.1",
|
"http-client": "^4.3.1",
|
||||||
"invariant": "^2.2.2",
|
"invariant": "^2.2.2",
|
||||||
"isomorphic-fetch": "^2.2.1",
|
"isomorphic-fetch": "^2.2.1",
|
||||||
"lru-cache": "^4.0.2",
|
|
||||||
"mime": "^1.3.6",
|
"mime": "^1.3.6",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
"morgan": "^1.8.1",
|
"morgan": "^1.8.1",
|
||||||
|
@ -106,7 +106,6 @@ const defaultServerConfig = {
|
|||||||
registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org',
|
registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org',
|
||||||
redirectTTL: process.env.REDIRECT_TTL || 500,
|
redirectTTL: process.env.REDIRECT_TTL || 500,
|
||||||
autoIndex: !process.env.DISABLE_INDEX,
|
autoIndex: !process.env.DISABLE_INDEX,
|
||||||
redisURL: process.env.REDIS_URL,
|
|
||||||
blacklist: require('./package-blacklist').blacklist
|
blacklist: require('./package-blacklist').blacklist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,49 +1,33 @@
|
|||||||
const redis = require('redis')
|
const redis = require('redis')
|
||||||
const createLRUCache = require('lru-cache')
|
const invariant = require('invariant')
|
||||||
|
|
||||||
const createRedisCache = (redisURL) => {
|
const RedisURL = process.env.REDIS_URL
|
||||||
const client = redis.createClient(redisURL)
|
|
||||||
|
|
||||||
const createKey = (key) => 'registry:' + key
|
invariant(
|
||||||
|
RedisURL,
|
||||||
|
'Missing $REDIS_URL environment variable'
|
||||||
|
)
|
||||||
|
|
||||||
const set = (key, value, expiry) => {
|
const db = redis.createClient(RedisURL)
|
||||||
client.set(createKey(key), JSON.stringify(value))
|
|
||||||
client.pexpire(createKey(key), expiry)
|
|
||||||
}
|
|
||||||
|
|
||||||
const get = (key, callback) => {
|
const createKey = (key) => 'registryCache-' + key
|
||||||
client.get(createKey(key), (error, value) => {
|
|
||||||
callback(error, value && JSON.parse(value))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const del = (key) => {
|
const set = (key, value, expiry) => {
|
||||||
client.del(createKey(key))
|
db.setex(createKey(key), expiry, JSON.stringify(value))
|
||||||
}
|
|
||||||
|
|
||||||
return { set, get, del }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const createMemoryCache = (options) => {
|
const get = (key, callback) => {
|
||||||
const cache = createLRUCache(options)
|
db.get(createKey(key), (error, value) => {
|
||||||
|
callback(error, value && JSON.parse(value))
|
||||||
const set = (key, value, expiry) => {
|
})
|
||||||
cache.set(key, value, expiry)
|
|
||||||
}
|
|
||||||
|
|
||||||
const get = (key, callback) => {
|
|
||||||
callback(null, cache.get(key))
|
|
||||||
}
|
|
||||||
|
|
||||||
const del = (key) => {
|
|
||||||
cache.del(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
return { set, get, del }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const RegistryCache = process.env.REDIS_URL
|
const del = (key) => {
|
||||||
? createRedisCache(process.env.REDIS_URL)
|
db.del(createKey(key))
|
||||||
: createMemoryCache({ max: 1000 })
|
}
|
||||||
|
|
||||||
module.exports = RegistryCache
|
module.exports = {
|
||||||
|
set,
|
||||||
|
get,
|
||||||
|
del
|
||||||
|
}
|
||||||
|
@ -21,13 +21,10 @@ const getPackageInfoFromRegistry = (registryURL, packageName) => {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
const OneMinute = 60 * 1000
|
|
||||||
const PackageNotFound = 'PackageNotFound'
|
const PackageNotFound = 'PackageNotFound'
|
||||||
|
|
||||||
const getPackageInfo = (registryURL, packageName, callback) => {
|
const getPackageInfo = (registryURL, packageName, callback) => {
|
||||||
const cacheKey = registryURL + packageName
|
RegistryCache.get(packageName, (error, value) => {
|
||||||
|
|
||||||
RegistryCache.get(cacheKey, (error, value) => {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
callback(error)
|
callback(error)
|
||||||
} else if (value) {
|
} else if (value) {
|
||||||
@ -39,15 +36,16 @@ const getPackageInfo = (registryURL, packageName, callback) => {
|
|||||||
// from making unnecessary requests to the registry for
|
// from making unnecessary requests to the registry for
|
||||||
// bad package names. In the worst case, a brand new
|
// bad package names. In the worst case, a brand new
|
||||||
// package's info will be available within 5 minutes.
|
// package's info will be available within 5 minutes.
|
||||||
RegistryCache.set(cacheKey, PackageNotFound, OneMinute * 5)
|
RegistryCache.set(packageName, PackageNotFound, 300)
|
||||||
} else {
|
} else {
|
||||||
RegistryCache.set(cacheKey, value, OneMinute)
|
// Keep package.json in the cache for a minute.
|
||||||
|
RegistryCache.set(packageName, value, 60)
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, value)
|
callback(null, value)
|
||||||
}, error => {
|
}, error => {
|
||||||
// Do not cache errors.
|
// Do not cache errors.
|
||||||
RegistryCache.del(cacheKey)
|
RegistryCache.del(packageName)
|
||||||
callback(error)
|
callback(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1529,7 +1529,7 @@ debug@2.6.7:
|
|||||||
dependencies:
|
dependencies:
|
||||||
ms "2.0.0"
|
ms "2.0.0"
|
||||||
|
|
||||||
debug@2.6.8, debug@^2.1.0, debug@^2.1.1, debug@^2.2.0, debug@^2.6.0, debug@^2.6.3, debug@^2.6.6, debug@^2.6.8:
|
debug@2.6.8, debug@^2.1.0, debug@^2.1.1, debug@^2.2.0, debug@^2.6.0, debug@^2.6.3, debug@^2.6.6:
|
||||||
version "2.6.8"
|
version "2.6.8"
|
||||||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
|
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
|
||||||
dependencies:
|
dependencies:
|
||||||
@ -3383,7 +3383,7 @@ lower-case@^1.1.1:
|
|||||||
version "1.1.4"
|
version "1.1.4"
|
||||||
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"
|
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"
|
||||||
|
|
||||||
lru-cache@^4.0.1, lru-cache@^4.0.2:
|
lru-cache@^4.0.1:
|
||||||
version "4.0.2"
|
version "4.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e"
|
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user