2022-02-10 23:20:55 +01:00
|
|
|
const config = require('../config.json');
|
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-02-08 14:07:59 +01:00
|
|
|
|
2022-02-10 23:20:55 +01:00
|
|
|
const active = config.cache.active;
|
|
|
|
const lifetime = config.cache.lifetime * 60000;
|
|
|
|
|
|
|
|
const cache = new Map();
|
|
|
|
|
|
|
|
function isActive() {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLifetime() {
|
|
|
|
return lifetime;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
cache = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
function validLifetime(timestamp) {
|
|
|
|
if (timestamp == undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((new Date().getTime() - timestamp) >= lifetime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValue(key) {
|
|
|
|
var value = cache.get(key);
|
|
|
|
if (value == undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (!validLifetime(value.timestamp)) {
|
|
|
|
resetValue(key)
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return cache.get(key).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue(key, value) {
|
|
|
|
if (!active) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2022-02-10 23:20:55 +01:00
|
|
|
isActive,
|
|
|
|
getLifetime,
|
|
|
|
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,
|
|
|
|
setInfo
|
2022-02-08 14:07:59 +01:00
|
|
|
}
|