diff --git a/handlers/lots-post/doCreateLot.js b/handlers/lots-post/doCreateLot.js index 0695757d..9010eb34 100644 --- a/handlers/lots-post/doCreateLot.js +++ b/handlers/lots-post/doCreateLot.js @@ -1,3 +1,4 @@ +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'; import { addLot } from '../../helpers/lotOccupancyDB/addLot.js'; export async function handler(request, response) { const lotId = await addLot(request.body, request.session); @@ -5,5 +6,8 @@ export async function handler(request, response) { success: true, lotId }); + response.on('finish', () => { + clearNextPreviousLotIdCache(); + }); } export default handler; diff --git a/handlers/lots-post/doCreateLot.ts b/handlers/lots-post/doCreateLot.ts index 77f4a9fc..d643cda0 100644 --- a/handlers/lots-post/doCreateLot.ts +++ b/handlers/lots-post/doCreateLot.ts @@ -1,4 +1,5 @@ import type { Request, Response } from 'express' +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js' import { addLot } from '../../helpers/lotOccupancyDB/addLot.js' @@ -12,6 +13,10 @@ export async function handler( success: true, lotId }) + + response.on('finish', () => { + clearNextPreviousLotIdCache() + }) } export default handler diff --git a/handlers/lots-post/doDeleteLot.js b/handlers/lots-post/doDeleteLot.js index 0d448f32..73559933 100644 --- a/handlers/lots-post/doDeleteLot.js +++ b/handlers/lots-post/doDeleteLot.js @@ -1,8 +1,13 @@ +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'; import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js'; export async function handler(request, response) { - const success = await deleteRecord('Lots', request.body.lotId, request.session); + const lotId = Number.parseInt(request.body.lotId, 10); + const success = await deleteRecord('Lots', lotId, request.session); response.json({ success }); + response.on('finish', () => { + clearNextPreviousLotIdCache(lotId); + }); } export default handler; diff --git a/handlers/lots-post/doDeleteLot.ts b/handlers/lots-post/doDeleteLot.ts index b14c7650..7a8311ca 100644 --- a/handlers/lots-post/doDeleteLot.ts +++ b/handlers/lots-post/doDeleteLot.ts @@ -1,4 +1,5 @@ import type { Request, Response } from 'express' +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js' import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js' @@ -6,11 +7,17 @@ export async function handler( request: Request, response: Response ): Promise { - const success = await deleteRecord('Lots', request.body.lotId, request.session) + const lotId = Number.parseInt(request.body.lotId, 10) + + const success = await deleteRecord('Lots', lotId, request.session) response.json({ success }) + + response.on('finish', () => { + clearNextPreviousLotIdCache(lotId) + }) } export default handler diff --git a/handlers/lots-post/doUpdateLot.js b/handlers/lots-post/doUpdateLot.js index eb48f734..4cfafde9 100644 --- a/handlers/lots-post/doUpdateLot.js +++ b/handlers/lots-post/doUpdateLot.js @@ -1,9 +1,14 @@ +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'; import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js'; export async function handler(request, response) { + const lotId = Number.parseInt(request.body.lotId, 10); const success = await updateLot(request.body, request.session); response.json({ success, lotId: request.body.lotId }); + response.on('finish', () => { + clearNextPreviousLotIdCache(lotId); + }); } export default handler; diff --git a/handlers/lots-post/doUpdateLot.ts b/handlers/lots-post/doUpdateLot.ts index c3479cfc..95337c36 100644 --- a/handlers/lots-post/doUpdateLot.ts +++ b/handlers/lots-post/doUpdateLot.ts @@ -1,4 +1,5 @@ import type { Request, Response } from 'express' +import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js' import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js' @@ -6,12 +7,18 @@ export async function handler( request: Request, response: Response ): Promise { + const lotId = Number.parseInt(request.body.lotId, 10) + const success = await updateLot(request.body, request.session) response.json({ success, lotId: request.body.lotId }) + + response.on('finish', () => { + clearNextPreviousLotIdCache(lotId) + }) } export default handler diff --git a/helpers/functions.lots.d.ts b/helpers/functions.lots.d.ts index 31830e3b..69f0b584 100644 --- a/helpers/functions.lots.d.ts +++ b/helpers/functions.lots.d.ts @@ -1,2 +1,3 @@ export declare function getNextLotId(lotId: number): Promise; export declare function getPreviousLotId(lotId: number): Promise; +export declare function clearNextPreviousLotIdCache(lotId?: number): void; diff --git a/helpers/functions.lots.js b/helpers/functions.lots.js index bd6754cf..488c5015 100644 --- a/helpers/functions.lots.js +++ b/helpers/functions.lots.js @@ -1,11 +1,12 @@ import NodeCache from 'node-cache'; import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js'; import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'; +const timeToLiveMinutes = 2; const previousLotIdCache = new NodeCache({ - stdTTL: 2 * 60 + stdTTL: timeToLiveMinutes * 60 }); const nextLotIdCache = new NodeCache({ - stdTTL: 2 * 60 + stdTTL: timeToLiveMinutes * 60 }); export async function getNextLotId(lotId) { let nextLotId = nextLotIdCache.get(lotId); @@ -29,3 +30,20 @@ export async function getPreviousLotId(lotId) { } return previousLotId; } +export function clearNextPreviousLotIdCache(lotId) { + if (lotId === undefined) { + previousLotIdCache.flushAll(); + nextLotIdCache.flushAll(); + return; + } + const previousLotId = previousLotIdCache.get(lotId); + if (previousLotId !== undefined) { + nextLotIdCache.del(previousLotId); + previousLotIdCache.del(lotId); + } + const nextLotId = nextLotIdCache.get(lotId); + if (nextLotId !== undefined) { + previousLotIdCache.del(nextLotId); + nextLotIdCache.del(nextLotId); + } +} diff --git a/helpers/functions.lots.ts b/helpers/functions.lots.ts index 4002ee05..5dc115ac 100644 --- a/helpers/functions.lots.ts +++ b/helpers/functions.lots.ts @@ -3,12 +3,14 @@ import NodeCache from 'node-cache' import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js' import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js' +const timeToLiveMinutes = 2 + const previousLotIdCache = new NodeCache({ - stdTTL: 2 * 60 // two minutes + stdTTL: timeToLiveMinutes * 60 }) const nextLotIdCache = new NodeCache({ - stdTTL: 2 * 60 // two minutes + stdTTL: timeToLiveMinutes * 60 }) export async function getNextLotId(lotId: number): Promise { @@ -42,3 +44,25 @@ export async function getPreviousLotId( return previousLotId } + +export function clearNextPreviousLotIdCache(lotId?: number): void { + if (lotId === undefined) { + previousLotIdCache.flushAll() + nextLotIdCache.flushAll() + return + } + + const previousLotId: number | undefined = previousLotIdCache.get(lotId) + + if (previousLotId !== undefined) { + nextLotIdCache.del(previousLotId) + previousLotIdCache.del(lotId) + } + + const nextLotId: number | undefined = nextLotIdCache.get(lotId) + + if (nextLotId !== undefined) { + previousLotIdCache.del(nextLotId) + nextLotIdCache.del(nextLotId) + } +}