lot id cache tweaks
parent
c2b24b9493
commit
c23206a23f
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<number | undefined> {
|
||||
let nextLotId: number | undefined = nextLotIdCache.get(lotId)
|
||||
|
||||
|
|
@ -20,8 +26,7 @@ export async function getNextLotId(lotId: number): Promise<number | undefined> {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue