flush next previous lot id cache

deepsource-autofix-76c6eb20
Dan Gowans 2023-01-20 10:05:22 -05:00
parent e1f766f712
commit 616a9ed0c6
9 changed files with 82 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js';
import { addLot } from '../../helpers/lotOccupancyDB/addLot.js'; import { addLot } from '../../helpers/lotOccupancyDB/addLot.js';
export async function handler(request, response) { export async function handler(request, response) {
const lotId = await addLot(request.body, request.session); const lotId = await addLot(request.body, request.session);
@ -5,5 +6,8 @@ export async function handler(request, response) {
success: true, success: true,
lotId lotId
}); });
response.on('finish', () => {
clearNextPreviousLotIdCache();
});
} }
export default handler; export default handler;

View File

@ -1,4 +1,5 @@
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'
import { addLot } from '../../helpers/lotOccupancyDB/addLot.js' import { addLot } from '../../helpers/lotOccupancyDB/addLot.js'
@ -12,6 +13,10 @@ export async function handler(
success: true, success: true,
lotId lotId
}) })
response.on('finish', () => {
clearNextPreviousLotIdCache()
})
} }
export default handler export default handler

View File

@ -1,8 +1,13 @@
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js';
import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js'; import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js';
export async function handler(request, response) { 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({ response.json({
success success
}); });
response.on('finish', () => {
clearNextPreviousLotIdCache(lotId);
});
} }
export default handler; export default handler;

View File

@ -1,4 +1,5 @@
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'
import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js' import { deleteRecord } from '../../helpers/lotOccupancyDB/deleteRecord.js'
@ -6,11 +7,17 @@ export async function handler(
request: Request, request: Request,
response: Response response: Response
): Promise<void> { ): Promise<void> {
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({ response.json({
success success
}) })
response.on('finish', () => {
clearNextPreviousLotIdCache(lotId)
})
} }
export default handler export default handler

View File

@ -1,9 +1,14 @@
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js';
import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js'; import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js';
export async function handler(request, response) { export async function handler(request, response) {
const lotId = Number.parseInt(request.body.lotId, 10);
const success = await updateLot(request.body, request.session); const success = await updateLot(request.body, request.session);
response.json({ response.json({
success, success,
lotId: request.body.lotId lotId: request.body.lotId
}); });
response.on('finish', () => {
clearNextPreviousLotIdCache(lotId);
});
} }
export default handler; export default handler;

View File

@ -1,4 +1,5 @@
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { clearNextPreviousLotIdCache } from '../../helpers/functions.lots.js'
import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js' import { updateLot } from '../../helpers/lotOccupancyDB/updateLot.js'
@ -6,12 +7,18 @@ export async function handler(
request: Request, request: Request,
response: Response response: Response
): Promise<void> { ): Promise<void> {
const lotId = Number.parseInt(request.body.lotId, 10)
const success = await updateLot(request.body, request.session) const success = await updateLot(request.body, request.session)
response.json({ response.json({
success, success,
lotId: request.body.lotId lotId: request.body.lotId
}) })
response.on('finish', () => {
clearNextPreviousLotIdCache(lotId)
})
} }
export default handler export default handler

View File

@ -1,2 +1,3 @@
export declare function getNextLotId(lotId: number): Promise<number | undefined>; export declare function getNextLotId(lotId: number): Promise<number | undefined>;
export declare function getPreviousLotId(lotId: number): Promise<number | undefined>; export declare function getPreviousLotId(lotId: number): Promise<number | undefined>;
export declare function clearNextPreviousLotIdCache(lotId?: number): void;

View File

@ -1,11 +1,12 @@
import NodeCache from 'node-cache'; import NodeCache from 'node-cache';
import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js'; import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js';
import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'; import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js';
const timeToLiveMinutes = 2;
const previousLotIdCache = new NodeCache({ const previousLotIdCache = new NodeCache({
stdTTL: 2 * 60 stdTTL: timeToLiveMinutes * 60
}); });
const nextLotIdCache = new NodeCache({ const nextLotIdCache = new NodeCache({
stdTTL: 2 * 60 stdTTL: timeToLiveMinutes * 60
}); });
export async function getNextLotId(lotId) { export async function getNextLotId(lotId) {
let nextLotId = nextLotIdCache.get(lotId); let nextLotId = nextLotIdCache.get(lotId);
@ -29,3 +30,20 @@ export async function getPreviousLotId(lotId) {
} }
return previousLotId; 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);
}
}

View File

@ -3,12 +3,14 @@ import NodeCache from 'node-cache'
import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js' import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js'
import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js' import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'
const timeToLiveMinutes = 2
const previousLotIdCache = new NodeCache({ const previousLotIdCache = new NodeCache({
stdTTL: 2 * 60 // two minutes stdTTL: timeToLiveMinutes * 60
}) })
const nextLotIdCache = new NodeCache({ const nextLotIdCache = new NodeCache({
stdTTL: 2 * 60 // two minutes stdTTL: timeToLiveMinutes * 60
}) })
export async function getNextLotId(lotId: number): Promise<number | undefined> { export async function getNextLotId(lotId: number): Promise<number | undefined> {
@ -42,3 +44,25 @@ export async function getPreviousLotId(
return previousLotId 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)
}
}