Use SETEX for storing package.json configs
Also, remove option for in-memory cache.
This commit is contained in:
@ -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)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user