2018-09-03 05:27:22 +00:00
|
|
|
const cacache = require('cacache/en');
|
|
|
|
const os = require('os');
|
|
|
|
const { DateTime } = require('luxon');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init,
|
|
|
|
};
|
|
|
|
|
|
|
|
function getKey(namespace, key) {
|
|
|
|
return `${namespace}-${key}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const renovateCache =
|
|
|
|
(process.env.RENOVATE_TMPDIR || os.tmpdir()) + '/renovate-cache-v1';
|
|
|
|
|
|
|
|
async function get(namespace, key) {
|
|
|
|
try {
|
|
|
|
const res = await cacache.get(renovateCache, getKey(namespace, key));
|
|
|
|
const cachedValue = JSON.parse(res.data.toString());
|
|
|
|
if (cachedValue) {
|
|
|
|
if (DateTime.local() < DateTime.fromISO(cachedValue.expiry)) {
|
|
|
|
logger.debug({ namespace, key }, 'Returning cached value');
|
|
|
|
return cachedValue.value;
|
|
|
|
}
|
|
|
|
// istanbul ignore next
|
|
|
|
await rm(namespace, key);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug({ namespace, key }, 'Cache miss');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-03 07:59:27 +00:00
|
|
|
async function set(namespace, key, value, ttlMinutes = 5) {
|
|
|
|
logger.debug({ namespace, key, ttlMinutes }, 'Saving cached value');
|
2018-09-03 05:27:22 +00:00
|
|
|
await cacache.put(
|
|
|
|
renovateCache,
|
|
|
|
getKey(namespace, key),
|
|
|
|
JSON.stringify({
|
|
|
|
value,
|
|
|
|
expiry: DateTime.local().plus({ minutes: ttlMinutes }),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// istanbul ignore next
|
|
|
|
async function rm(namespace, key) {
|
|
|
|
logger.debug({ namespace, key }, 'Removing cache entry');
|
|
|
|
await cacache.rm.entry(renovateCache, getKey(namespace, key));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function rmAll() {
|
|
|
|
await cacache.rm.all(renovateCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
global.renovateCache = global.renovateCache || { get, set, rm, rmAll };
|
|
|
|
}
|