Cache etherpad informations for one hour

This commit is contained in:
Stefan Müller 2023-09-24 19:59:31 +02:00
parent 94ad1a594d
commit 43a7f53ddc

View file

@ -5,10 +5,17 @@ const axios = require('axios');
const headers = { const headers = {
'User-Agent': 'Etherpad ' + settings.getEpVersion(), 'User-Agent': 'Etherpad ' + settings.getEpVersion(),
} }
let infos;
const loadEtherpadInformations = () => const updateInterval = 60 * 60 * 1000; // 1 hour
axios.get('https://static.etherpad.org/info.json', {headers: headers}) let infos;
let lastLoadingTime = null;
const loadEtherpadInformations = () => {
if (lastLoadingTime !== null && Date.now() - lastLoadingTime < updateInterval) {
return Promise.resolve(infos);
}
return axios.get('https://static.etherpad.org/info.json', {headers: headers})
.then(async resp => { .then(async resp => {
try { try {
infos = await resp.data; infos = await resp.data;
@ -16,11 +23,14 @@ const loadEtherpadInformations = () =>
await Promise.reject("Could not retrieve current version") await Promise.reject("Could not retrieve current version")
return return
} }
lastLoadingTime = Date.now();
return await Promise.resolve(infos); return await Promise.resolve(infos);
} catch (err) { } catch (err) {
return await Promise.reject(err); return await Promise.reject(err);
} }
}) })
}
exports.getLatestVersion = () => { exports.getLatestVersion = () => {