2022-02-08 14:07:59 +01:00
|
|
|
const logger = require('./logger.js');
|
2022-02-10 23:20:55 +01:00
|
|
|
const constants = require('./constants.js');
|
2022-03-10 00:32:38 +01:00
|
|
|
const timeDiff = require('./util.js').timeDiff;
|
2022-02-08 14:07:59 +01:00
|
|
|
|
2022-03-24 12:50:16 +01:00
|
|
|
let active;
|
|
|
|
let lifetime;
|
2022-02-10 23:20:55 +01:00
|
|
|
|
2022-03-10 00:32:38 +01:00
|
|
|
let modep;
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
const cache = new Map();
|
|
|
|
|
|
|
|
function isActive() {
|
2022-03-24 12:50:16 +01:00
|
|
|
if (active === undefined) {
|
|
|
|
active = global.config.cache.active;
|
|
|
|
}
|
2022-02-10 23:20:55 +01:00
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLifetime() {
|
2022-03-24 12:50:16 +01:00
|
|
|
if (lifetime === undefined) {
|
|
|
|
lifetime = global.config.cache.lifetime * 60000
|
|
|
|
}
|
2022-02-10 23:20:55 +01:00
|
|
|
return lifetime;
|
|
|
|
}
|
|
|
|
|
2022-03-10 00:32:38 +01:00
|
|
|
async function fill() {
|
|
|
|
if (!isActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const timestamp = new Date();
|
|
|
|
logger.debug('filling cache...');
|
|
|
|
clear();
|
2022-03-10 00:36:59 +01:00
|
|
|
try {
|
|
|
|
if (modep === undefined) {
|
|
|
|
modep = require('./modep.js');
|
|
|
|
}
|
|
|
|
await modep.getBanks();
|
|
|
|
await modep.getPedalboards();
|
|
|
|
await modep.getDefaultPedalboard();
|
|
|
|
await modep.getCurrentPedalboard();
|
|
|
|
await modep.getCurrentPedals();
|
|
|
|
logger.debug('cache filled after ' + timeDiff(timestamp) + 'ms');
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('encountered an error while filling the cache after ' + timeDiff(timestamp) + 'ms');
|
2022-03-24 13:00:19 +01:00
|
|
|
logger.error(err);
|
2022-03-10 00:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function clear() {
|
2022-03-10 00:32:38 +01:00
|
|
|
cache.clear();
|
2022-02-10 23:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function validLifetime(timestamp) {
|
2022-03-08 00:51:58 +01:00
|
|
|
if (timestamp === undefined) {
|
2022-02-10 23:20:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
if (lifetime === 0) {
|
|
|
|
return true;
|
2022-02-10 23:20:55 +01:00
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
return (new Date().getTime() - timestamp) <= lifetime;
|
2022-02-10 23:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getValue(key) {
|
2022-03-24 16:30:00 +01:00
|
|
|
let value = cache.get(key);
|
2022-03-08 00:51:58 +01:00
|
|
|
if (value === undefined) {
|
2022-02-10 23:20:55 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (!validLifetime(value.timestamp)) {
|
|
|
|
resetValue(key)
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return cache.get(key).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue(key, value) {
|
2022-03-10 00:32:38 +01:00
|
|
|
if (!isActive) {
|
2022-02-10 23:20:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.debug('caching \'' + key + '\'...');
|
|
|
|
cache.set(key, { data: value, timestamp: new Date().getTime() });
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetValue(key) {
|
|
|
|
cache.delete(key);
|
|
|
|
}
|
2022-02-08 14:07:59 +01:00
|
|
|
|
|
|
|
function getBanks() {
|
2022-02-10 23:20:55 +01:00
|
|
|
return getValue(constants.CACHE_BANKS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearBanks() {
|
2022-02-10 23:20:55 +01:00
|
|
|
resetValue(constants.CACHE_BANKS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setBanks(value) {
|
2022-02-10 23:20:55 +01:00
|
|
|
setValue(constants.CACHE_BANKS, value);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPedalboards() {
|
2022-02-10 23:20:55 +01:00
|
|
|
return getValue(constants.CACHE_PEDALBOARDS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearPedalboards() {
|
2022-02-10 23:20:55 +01:00
|
|
|
resetValue(constants.CACHE_PEDALBOARDS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setPedalboards(value) {
|
2022-02-10 23:20:55 +01:00
|
|
|
setValue(constants.CACHE_PEDALBOARDS, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentPedalboard() {
|
|
|
|
return getValue(constants.CACHE_PEDALBOARD_CURRENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearCurrentPedalboard() {
|
|
|
|
resetValue(constants.CACHE_PEDALBOARD_CURRENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCurrentPedalboard(value) {
|
|
|
|
setValue(constants.CACHE_PEDALBOARD_CURRENT, value);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultPedalboard() {
|
2022-02-10 23:20:55 +01:00
|
|
|
return getValue(constants.CACHE_PEDALBOARD_DEFAULT);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearDefaultPedalboard() {
|
2022-02-10 23:20:55 +01:00
|
|
|
resetValue(constants.CACHE_PEDALBOARD_DEFAULT);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setDefaultPedalboard(value) {
|
2022-02-10 23:20:55 +01:00
|
|
|
setValue(constants.CACHE_PEDALBOARD_DEFAULT, value);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function getCurrentPedals() {
|
|
|
|
return getValue(constants.CACHE_PEDALS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function clearCurrentPedals() {
|
|
|
|
resetValue(constants.CACHE_PEDALS);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function setCurrentPedals(value) {
|
|
|
|
setValue(constants.CACHE_PEDALS, value);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function getInfo() {
|
|
|
|
return getValue(constants.CACHE_INFO);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function clearInfo() {
|
|
|
|
resetValue(constants.CACHE_INFO);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
function setInfo(value) {
|
|
|
|
setValue(constants.CACHE_INFO, value);
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:30:00 +01:00
|
|
|
function setBypassedPedal(pedalId) {
|
|
|
|
if (isPedalBypassed(pedalId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED);
|
|
|
|
if (bypassedPedals === undefined) {
|
|
|
|
bypassedPedals = [];
|
|
|
|
}
|
|
|
|
bypassedPedals.push(pedalId);
|
|
|
|
cache.set(constants.CACHE_PEDALS_BYPASSED, bypassedPedals);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPedalBypassed(pedalId) {
|
|
|
|
let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED);
|
|
|
|
if (bypassedPedals === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return bypassedPedals.includes(pedalId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeBypassedPedal(pedalId) {
|
|
|
|
if (!isPedalBypassed(pedalId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED);
|
|
|
|
bypassedPedals.splice(bypassedPedals.indexOf(pedalId), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearBypassedPedals() {
|
|
|
|
cache.delete(constants.CACHE_PEDALS_BYPASSED);
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:07:59 +01:00
|
|
|
module.exports = {
|
2022-02-10 23:20:55 +01:00
|
|
|
isActive,
|
|
|
|
getLifetime,
|
2022-03-10 00:32:38 +01:00
|
|
|
fill,
|
2022-02-10 23:20:55 +01:00
|
|
|
clear,
|
2022-02-08 14:07:59 +01:00
|
|
|
getBanks,
|
|
|
|
clearBanks,
|
|
|
|
setBanks,
|
|
|
|
getPedalboards,
|
|
|
|
clearPedalboards,
|
|
|
|
setPedalboards,
|
|
|
|
getDefaultPedalboard,
|
|
|
|
clearDefaultPedalboard,
|
|
|
|
setDefaultPedalboard,
|
|
|
|
getCurrentPedalboard,
|
|
|
|
clearCurrentPedalboard,
|
|
|
|
setCurrentPedalboard,
|
|
|
|
getCurrentPedals,
|
2022-02-10 23:20:55 +01:00
|
|
|
clearCurrentPedals,
|
|
|
|
setCurrentPedals,
|
|
|
|
getInfo,
|
|
|
|
clearInfo,
|
2022-03-24 16:30:00 +01:00
|
|
|
setInfo,
|
|
|
|
setBypassedPedal,
|
|
|
|
isPedalBypassed,
|
|
|
|
removeBypassedPedal,
|
|
|
|
clearBypassedPedals
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|