diff --git a/helpers/functions.lots.js b/helpers/functions.lots.js index 488c5015..a86bc559 100644 --- a/helpers/functions.lots.js +++ b/helpers/functions.lots.js @@ -3,18 +3,22 @@ import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js'; import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'; const timeToLiveMinutes = 2; const previousLotIdCache = new NodeCache({ - stdTTL: timeToLiveMinutes * 60 + stdTTL: timeToLiveMinutes * 60, + useClones: false }); const nextLotIdCache = new NodeCache({ stdTTL: timeToLiveMinutes * 60 }); +function cacheLotIds(lotId, nextLotId) { + previousLotIdCache.set(nextLotId, lotId); + nextLotIdCache.set(lotId, nextLotId); +} export async function getNextLotId(lotId) { let nextLotId = nextLotIdCache.get(lotId); if (nextLotId === undefined) { nextLotId = await getNextLotIdFromDatabase(lotId); if (nextLotId !== undefined) { - previousLotIdCache.set(nextLotId, lotId); - nextLotIdCache.set(lotId, nextLotId); + cacheLotIds(lotId, nextLotId); } } return nextLotId; @@ -24,8 +28,7 @@ export async function getPreviousLotId(lotId) { if (previousLotId === undefined) { previousLotId = await getPreviousLotIdFromDatabase(lotId); if (previousLotId !== undefined) { - previousLotIdCache.set(lotId, previousLotId); - nextLotIdCache.set(previousLotId, lotId); + cacheLotIds(previousLotId, lotId); } } return previousLotId; @@ -44,6 +47,6 @@ export function clearNextPreviousLotIdCache(lotId) { const nextLotId = nextLotIdCache.get(lotId); if (nextLotId !== undefined) { previousLotIdCache.del(nextLotId); - nextLotIdCache.del(nextLotId); + nextLotIdCache.del(lotId); } } diff --git a/helpers/functions.lots.ts b/helpers/functions.lots.ts index 5dc115ac..21328d9a 100644 --- a/helpers/functions.lots.ts +++ b/helpers/functions.lots.ts @@ -6,13 +6,19 @@ import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js' const timeToLiveMinutes = 2 const previousLotIdCache = new NodeCache({ - stdTTL: timeToLiveMinutes * 60 + stdTTL: timeToLiveMinutes * 60, + useClones: false }) const nextLotIdCache = new NodeCache({ stdTTL: timeToLiveMinutes * 60 }) +function cacheLotIds(lotId: number, nextLotId: number): void { + previousLotIdCache.set(nextLotId, lotId) + nextLotIdCache.set(lotId, nextLotId) +} + export async function getNextLotId(lotId: number): Promise { let nextLotId: number | undefined = nextLotIdCache.get(lotId) @@ -20,8 +26,7 @@ export async function getNextLotId(lotId: number): Promise { nextLotId = await getNextLotIdFromDatabase(lotId) if (nextLotId !== undefined) { - previousLotIdCache.set(nextLotId, lotId) - nextLotIdCache.set(lotId, nextLotId) + cacheLotIds(lotId, nextLotId) } } @@ -37,8 +42,7 @@ export async function getPreviousLotId( previousLotId = await getPreviousLotIdFromDatabase(lotId) if (previousLotId !== undefined) { - previousLotIdCache.set(lotId, previousLotId) - nextLotIdCache.set(previousLotId, lotId) + cacheLotIds(previousLotId, lotId) } } @@ -63,6 +67,6 @@ export function clearNextPreviousLotIdCache(lotId?: number): void { if (nextLotId !== undefined) { previousLotIdCache.del(nextLotId) - nextLotIdCache.del(nextLotId) + nextLotIdCache.del(lotId) } }