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 @@ ...@@ -2,9 +2,9 @@
const fs = require('fs'); const fs = require('fs');
const cache = { const cache = {
node: [], node: {},
way: [], way: {},
relation: [], relation: {},
}; };
function loadFromFile() { function loadFromFile() {
...@@ -15,9 +15,9 @@ function loadFromFile() { ...@@ -15,9 +15,9 @@ function loadFromFile() {
console.warn('Could not read element cache. Ignorning cache.'); console.warn('Could not read element cache. Ignorning cache.');
console.warn(error); console.warn(error);
} }
cache.node = data.node || []; cache.node = data.node || {};
cache.way = data.way || []; cache.way = data.way || {};
cache.relation = data.relation || []; cache.relation = data.relation || {};
} }
function saveToFile() { function saveToFile() {
...@@ -25,11 +25,11 @@ function saveToFile() { ...@@ -25,11 +25,11 @@ function saveToFile() {
} }
function getFromCache(type, id) { function getFromCache(type, id) {
return cache[type].find((element) => element.id === id); return cache[type][id];
} }
function saveToCache(type, element) { function saveToCache(type, id, element) {
cache[type].push(element); cache[type][id] = element;
saveToFile(); saveToFile();
} }
......
...@@ -5,7 +5,7 @@ describe('can store and receive', () => { ...@@ -5,7 +5,7 @@ describe('can store and receive', () => {
id: 34, id: 34,
}; };
beforeAll(() => { beforeAll(() => {
cache.saveToCache('node', element); cache.saveToCache('node', element.id, element);
}); });
test('can receive the element', () => { test('can receive the element', () => {
......
...@@ -25,7 +25,7 @@ async function getOsmElement(type, id) { ...@@ -25,7 +25,7 @@ async function getOsmElement(type, id) {
nominatimObject, nominatimObject,
osmObject, osmObject,
}; };
if (element) saveToCache(type, element); if (element) saveToCache(type, id, element);
} }
return 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