lot id cache tweaks

deepsource-autofix-76c6eb20
Dan Gowans 2023-01-25 12:57:32 -05:00
parent c2b24b9493
commit c23206a23f
2 changed files with 19 additions and 12 deletions

View File

@ -3,18 +3,22 @@ import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js';
import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'; import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js';
const timeToLiveMinutes = 2; const timeToLiveMinutes = 2;
const previousLotIdCache = new NodeCache({ const previousLotIdCache = new NodeCache({
stdTTL: timeToLiveMinutes * 60 stdTTL: timeToLiveMinutes * 60,
useClones: false
}); });
const nextLotIdCache = new NodeCache({ const nextLotIdCache = new NodeCache({
stdTTL: timeToLiveMinutes * 60 stdTTL: timeToLiveMinutes * 60
}); });
function cacheLotIds(lotId, nextLotId) {
previousLotIdCache.set(nextLotId, lotId);
nextLotIdCache.set(lotId, nextLotId);
}
export async function getNextLotId(lotId) { export async function getNextLotId(lotId) {
let nextLotId = nextLotIdCache.get(lotId); let nextLotId = nextLotIdCache.get(lotId);
if (nextLotId === undefined) { if (nextLotId === undefined) {
nextLotId = await getNextLotIdFromDatabase(lotId); nextLotId = await getNextLotIdFromDatabase(lotId);
if (nextLotId !== undefined) { if (nextLotId !== undefined) {
previousLotIdCache.set(nextLotId, lotId); cacheLotIds(lotId, nextLotId);
nextLotIdCache.set(lotId, nextLotId);
} }
} }
return nextLotId; return nextLotId;
@ -24,8 +28,7 @@ export async function getPreviousLotId(lotId) {
if (previousLotId === undefined) { if (previousLotId === undefined) {
previousLotId = await getPreviousLotIdFromDatabase(lotId); previousLotId = await getPreviousLotIdFromDatabase(lotId);
if (previousLotId !== undefined) { if (previousLotId !== undefined) {
previousLotIdCache.set(lotId, previousLotId); cacheLotIds(previousLotId, lotId);
nextLotIdCache.set(previousLotId, lotId);
} }
} }
return previousLotId; return previousLotId;
@ -44,6 +47,6 @@ export function clearNextPreviousLotIdCache(lotId) {
const nextLotId = nextLotIdCache.get(lotId); const nextLotId = nextLotIdCache.get(lotId);
if (nextLotId !== undefined) { if (nextLotId !== undefined) {
previousLotIdCache.del(nextLotId); previousLotIdCache.del(nextLotId);
nextLotIdCache.del(nextLotId); nextLotIdCache.del(lotId);
} }
} }

View File

@ -6,13 +6,19 @@ import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'
const timeToLiveMinutes = 2 const timeToLiveMinutes = 2
const previousLotIdCache = new NodeCache({ const previousLotIdCache = new NodeCache({
stdTTL: timeToLiveMinutes * 60 stdTTL: timeToLiveMinutes * 60,
useClones: false
}) })
const nextLotIdCache = new NodeCache({ const nextLotIdCache = new NodeCache({
stdTTL: timeToLiveMinutes * 60 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> { export async function getNextLotId(lotId: number): Promise<number | undefined> {
let nextLotId: number | undefined = nextLotIdCache.get(lotId) let nextLotId: number | undefined = nextLotIdCache.get(lotId)
@ -20,8 +26,7 @@ export async function getNextLotId(lotId: number): Promise<number | undefined> {
nextLotId = await getNextLotIdFromDatabase(lotId) nextLotId = await getNextLotIdFromDatabase(lotId)
if (nextLotId !== undefined) { if (nextLotId !== undefined) {
previousLotIdCache.set(nextLotId, lotId) cacheLotIds(lotId, nextLotId)
nextLotIdCache.set(lotId, nextLotId)
} }
} }
@ -37,8 +42,7 @@ export async function getPreviousLotId(
previousLotId = await getPreviousLotIdFromDatabase(lotId) previousLotId = await getPreviousLotIdFromDatabase(lotId)
if (previousLotId !== undefined) { if (previousLotId !== undefined) {
previousLotIdCache.set(lotId, previousLotId) cacheLotIds(previousLotId, lotId)
nextLotIdCache.set(previousLotId, lotId)
} }
} }
@ -63,6 +67,6 @@ export function clearNextPreviousLotIdCache(lotId?: number): void {
if (nextLotId !== undefined) { if (nextLotId !== undefined) {
previousLotIdCache.del(nextLotId) previousLotIdCache.del(nextLotId)
nextLotIdCache.del(nextLotId) nextLotIdCache.del(lotId)
} }
} }