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",
|
||||
"invariant": "^2.2.2",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"lru-cache": "^4.0.2",
|
||||
"mime": "^1.3.6",
|
||||
"mkdirp": "^0.5.1",
|
||||
"morgan": "^1.8.1",
|
||||
|
|
|
@ -106,7 +106,6 @@ const defaultServerConfig = {
|
|||
registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org',
|
||||
redirectTTL: process.env.REDIRECT_TTL || 500,
|
||||
autoIndex: !process.env.DISABLE_INDEX,
|
||||
redisURL: process.env.REDIS_URL,
|
||||
blacklist: require('./package-blacklist').blacklist
|
||||
}
|
||||
|
||||
|
|
|
@ -1,49 +1,33 @@
|
|||
const redis = require('redis')
|
||||
const createLRUCache = require('lru-cache')
|
||||
const invariant = require('invariant')
|
||||
|
||||
const createRedisCache = (redisURL) => {
|
||||
const client = redis.createClient(redisURL)
|
||||
const RedisURL = process.env.REDIS_URL
|
||||
|
||||
const createKey = (key) => 'registry:' + key
|
||||
invariant(
|
||||
RedisURL,
|
||||
'Missing $REDIS_URL environment variable'
|
||||
)
|
||||
|
||||
const set = (key, value, expiry) => {
|
||||
client.set(createKey(key), JSON.stringify(value))
|
||||
client.pexpire(createKey(key), expiry)
|
||||
}
|
||||
const db = redis.createClient(RedisURL)
|
||||
|
||||
const get = (key, callback) => {
|
||||
client.get(createKey(key), (error, value) => {
|
||||
callback(error, value && JSON.parse(value))
|
||||
})
|
||||
}
|
||||
const createKey = (key) => 'registryCache-' + key
|
||||
|
||||
const del = (key) => {
|
||||
client.del(createKey(key))
|
||||
}
|
||||
|
||||
return { set, get, del }
|
||||
const set = (key, value, expiry) => {
|
||||
db.setex(createKey(key), expiry, JSON.stringify(value))
|
||||
}
|
||||
|
||||
const createMemoryCache = (options) => {
|
||||
const cache = createLRUCache(options)
|
||||
|
||||
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 get = (key, callback) => {
|
||||
db.get(createKey(key), (error, value) => {
|
||||
callback(error, value && JSON.parse(value))
|
||||
})
|
||||
}
|
||||
|
||||
const RegistryCache = process.env.REDIS_URL
|
||||
? createRedisCache(process.env.REDIS_URL)
|
||||
: createMemoryCache({ max: 1000 })
|
||||
const del = (key) => {
|
||||
db.del(createKey(key))
|
||||
}
|
||||
|
||||
module.exports = RegistryCache
|
||||
module.exports = {
|
||||
set,
|
||||
get,
|
||||
del
|
||||
}
|
||||
|
|
|
@ -21,13 +21,10 @@ const getPackageInfoFromRegistry = (registryURL, packageName) => {
|
|||
))
|
||||
}
|
||||
|
||||
const OneMinute = 60 * 1000
|
||||
const PackageNotFound = 'PackageNotFound'
|
||||
|
||||
const getPackageInfo = (registryURL, packageName, callback) => {
|
||||
const cacheKey = registryURL + packageName
|
||||
|
||||
RegistryCache.get(cacheKey, (error, value) => {
|
||||
RegistryCache.get(packageName, (error, value) => {
|
||||
if (error) {
|
||||
callback(error)
|
||||
} else if (value) {
|
||||
|
@ -39,15 +36,16 @@ const getPackageInfo = (registryURL, packageName, callback) => {
|
|||
// 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.
|
||||
RegistryCache.set(cacheKey, PackageNotFound, OneMinute * 5)
|
||||
RegistryCache.set(packageName, PackageNotFound, 300)
|
||||
} else {
|
||||
RegistryCache.set(cacheKey, value, OneMinute)
|
||||
// Keep package.json in the cache for a minute.
|
||||
RegistryCache.set(packageName, value, 60)
|
||||
}
|
||||
|
||||
callback(null, value)
|
||||
}, error => {
|
||||
// Do not cache errors.
|
||||
RegistryCache.del(cacheKey)
|
||||
RegistryCache.del(packageName)
|
||||
callback(error)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1529,7 +1529,7 @@ debug@2.6.7:
|
|||
dependencies:
|
||||
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"
|
||||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
|
||||
dependencies:
|
||||
|
@ -3383,7 +3383,7 @@ lower-case@^1.1.1:
|
|||
version "1.1.4"
|
||||
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"
|
||||
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e"
|
||||
dependencies:
|
||||
|
|
Loading…
Reference in New Issue