extended / optimized caching

This commit is contained in:
Daniel Sommer 2022-02-10 23:20:55 +01:00
parent ce81de43e6
commit c2793c7e02
5 changed files with 118 additions and 44 deletions

View file

@ -4,6 +4,10 @@
"port": 3000, "port": 3000,
"timestamp": "DD.MM.YYYY HH:mm:ss:SS" "timestamp": "DD.MM.YYYY HH:mm:ss:SS"
}, },
"cache": {
"active": true,
"lifetime": 15
},
"log": { "log": {
"level": "debug" "level": "debug"
}, },

View file

@ -1,82 +1,134 @@
const config = require('../config.json');
const logger = require('./logger.js'); const logger = require('./logger.js');
const constants = require('./constants.js');
var banks = undefined; const active = config.cache.active;
var pedalboards = undefined; const lifetime = config.cache.lifetime * 60000;
var defaultPedalboard = undefined;
var currentPedalboard = undefined; const cache = new Map();
var currentPedals = undefined;
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);
}
function getBanks() { function getBanks() {
return banks; return getValue(constants.CACHE_BANKS);
} }
function clearBanks() { function clearBanks() {
logger.debug('clearing bank(s)'); resetValue(constants.CACHE_BANKS);
setBanks(undefined);
} }
function setBanks(value) { function setBanks(value) {
logger.debug('caching ' + value.length + ' bank(s)'); setValue(constants.CACHE_BANKS, value);
banks = value;
} }
function getPedalboards() { function getPedalboards() {
return pedalboards; return getValue(constants.CACHE_PEDALBOARDS);
} }
function clearPedalboards() { function clearPedalboards() {
logger.debug('clearing pedalboard(s)'); resetValue(constants.CACHE_PEDALBOARDS);
setPedalboards(undefined);
} }
function setPedalboards(value) { function setPedalboards(value) {
logger.debug('caching ' + value.length + ' pedalboard(s)'); setValue(constants.CACHE_PEDALBOARDS, value);
pedalboards = value;
}
function getDefaultPedalboard() {
return defaultPedalboard;
}
function clearDefaultPedalboard() {
logger.debug('clearing default pedalboard');
setDefaultPedalboard(undefined);
}
function setDefaultPedalboard(value) {
logger.debug('caching default pedalboard');
defaultPedalboard = value;
} }
function getCurrentPedalboard() { function getCurrentPedalboard() {
return currentPedalboard; return getValue(constants.CACHE_PEDALBOARD_CURRENT);
} }
function clearCurrentPedalboard() { function clearCurrentPedalboard() {
logger.debug('clearing current pedalboard'); resetValue(constants.CACHE_PEDALBOARD_CURRENT);
setCurrentPedalboard(undefined);
} }
function setCurrentPedalboard(value) { function setCurrentPedalboard(value) {
logger.debug('caching current pedalboard'); setValue(constants.CACHE_PEDALBOARD_CURRENT, value);
currentPedalboard = value; }
function getDefaultPedalboard() {
return getValue(constants.CACHE_PEDALBOARD_DEFAULT);
}
function clearDefaultPedalboard() {
resetValue(constants.CACHE_PEDALBOARD_DEFAULT);
}
function setDefaultPedalboard(value) {
setValue(constants.CACHE_PEDALBOARD_DEFAULT, value);
} }
function getCurrentPedals() { function getCurrentPedals() {
return currentPedals; return getValue(constants.CACHE_PEDALS);
} }
function clearCurrenPedals() { function clearCurrentPedals() {
logger.debug('clearing current pedal(s))'); resetValue(constants.CACHE_PEDALS);
setCurrentPedals(undefined);
} }
function setCurrentPedals(value) { function setCurrentPedals(value) {
logger.debug('caching ' + value.length + ' current pedal(s)'); setValue(constants.CACHE_PEDALS, value);
currentPedals = value; }
function getInfo() {
return getValue(constants.CACHE_INFO);
}
function clearInfo() {
resetValue(constants.CACHE_INFO);
}
function setInfo(value) {
setValue(constants.CACHE_INFO, value);
} }
module.exports = { module.exports = {
isActive,
getLifetime,
clear,
getBanks, getBanks,
clearBanks, clearBanks,
setBanks, setBanks,
@ -90,6 +142,9 @@ module.exports = {
clearCurrentPedalboard, clearCurrentPedalboard,
setCurrentPedalboard, setCurrentPedalboard,
getCurrentPedals, getCurrentPedals,
clearCurrenPedals, clearCurrentPedals,
setCurrentPedals setCurrentPedals,
getInfo,
clearInfo,
setInfo
} }

View file

@ -8,4 +8,11 @@ exports.API_PEDALBOARDS_DEFAULT = '/pedalboards/default';
exports.API_PEDALBOARDS_CURRENT = '/pedalboards/current'; exports.API_PEDALBOARDS_CURRENT = '/pedalboards/current';
exports.API_PEDALS = '/pedals'; exports.API_PEDALS = '/pedals';
exports.CACHE_INFO = "info";
exports.CACHE_BANKS = "banks";
exports.CACHE_PEDALBOARDS = "pedalboards";
exports.CACHE_PEDALBOARD_DEFAULT = "pedalboard_default";
exports.CACHE_PEDALBOARD_CURRENT = "pedalboard_current";
exports.CACHE_PEDALS = "pedals";
exports.PEDALBOARD_DEFAULT = '/var/modep/pedalboards/default.pedalboard'; exports.PEDALBOARD_DEFAULT = '/var/modep/pedalboards/default.pedalboard';

View file

@ -1,4 +1,5 @@
const logger = require('./logger.js'); const logger = require('./logger.js');
const cache = require('./cache');
const os = require('os'); const os = require('os');
function getHostname() { function getHostname() {
@ -52,7 +53,11 @@ function getMemoryInfo() {
} }
function getHostInfo() { function getHostInfo() {
return { var info = cache.getInfo();
if (info != undefined) {
return info;
}
info = {
hostname: getHostname(), hostname: getHostname(),
uptime: getUptime(), uptime: getUptime(),
load: getLoadAverage(), load: getLoadAverage(),
@ -64,6 +69,8 @@ function getHostInfo() {
cpu: getCpuInfo(), cpu: getCpuInfo(),
memory: getMemoryInfo() memory: getMemoryInfo()
} }
cache.setInfo(info);
return info;
} }
module.exports = { module.exports = {

View file

@ -22,6 +22,7 @@ function getBanks() {
if (banks != undefined) { if (banks != undefined) {
return resolve(banks); return resolve(banks);
} }
// FAKE DATA // FAKE DATA
// var fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }]; // var fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }];
// for (var index = 0; index < fake.length; index++) { // for (var index = 0; index < fake.length; index++) {