Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions backend/routes/journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ const DEVIATION_THRESHOLD_METRES = Number(
);
const ROUTE_RADIUS_ATTEMPTS_METRES = [null, 1000, 2500, 5000];
const FAST_ROUTE_RADIUS_ATTEMPTS_METRES = [null, 1000];
const CACHE_MAX_ENTRIES = Number(process.env.CACHE_MAX_ENTRIES || 200);

const recentRouteCache = new Map();
const recentGeocodeCache = new Map();

const evictOldestEntries = (cache) => {
if (cache.size <= CACHE_MAX_ENTRIES) return;
const entriesToDelete = cache.size - CACHE_MAX_ENTRIES;
let count = 0;
for (const key of cache.keys()) {
if (count >= entriesToDelete) break;
cache.delete(key);
count++;
}
};

const createTimeoutError = () => {
const error = new Error('Journey provider request timed out.');
error.code = 'JOURNEY_TIMEOUT';
Expand Down Expand Up @@ -168,6 +181,7 @@ const setCachedRoute = (cacheKey, data) => {
data,
cachedAt: Date.now(),
});
evictOldestEntries(recentRouteCache);
};

const getGeocodeCacheKey = (query) => String(query || '').trim().toLowerCase();
Expand All @@ -191,6 +205,7 @@ const setCachedGeocode = (cacheKey, data) => {
data,
cachedAt: Date.now(),
});
evictOldestEntries(recentGeocodeCache);
};

const buildFallbackRoute = ({ originLat, originLng, destLat, destLng, mode = 'vehicle' }) => {
Expand Down