Commit 46642786 authored by Christian Paul's avatar Christian Paul 🦊

Change cache format from arrays to objects with IDs as their keys

parent 29e7eefd
Pipeline #63379069 failed with stage
in 1 minute and 5 seconds
......@@ -2,9 +2,9 @@
const fs = require('fs');
const cache = {
node: [],
way: [],
relation: [],
node: {},
way: {},
relation: {},
};
function loadFromFile() {
......@@ -15,9 +15,9 @@ function loadFromFile() {
console.warn('Could not read element cache. Ignorning cache.');
console.warn(error);
}
cache.node = data.node || [];
cache.way = data.way || [];
cache.relation = data.relation || [];
cache.node = data.node || {};
cache.way = data.way || {};
cache.relation = data.relation || {};
}
function saveToFile() {
......@@ -25,11 +25,11 @@ function saveToFile() {
}
function getFromCache(type, id) {
return cache[type].find((element) => element.id === id);
return cache[type][id];
}
function saveToCache(type, element) {
cache[type].push(element);
function saveToCache(type, id, element) {
cache[type][id] = element;
saveToFile();
}
......
......@@ -5,7 +5,7 @@ describe('can store and receive', () => {
id: 34,
};
beforeAll(() => {
cache.saveToCache('node', element);
cache.saveToCache('node', element.id, element);
});
test('can receive the element', () => {
......
......@@ -25,7 +25,7 @@ async function getOsmElement(type, id) {
nominatimObject,
osmObject,
};
if (element) saveToCache(type, element);
if (element) saveToCache(type, id, element);
}
return element;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment