flush next previous lot id cache
parent
e1f766f712
commit
616a9ed0c6
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<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({
|
||||
success
|
||||
})
|
||||
|
||||
response.on('finish', () => {
|
||||
clearNextPreviousLotIdCache(lotId)
|
||||
})
|
||||
}
|
||||
|
||||
export default handler
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
export declare function getNextLotId(lotId: number): Promise<number | undefined>;
|
||||
export declare function getPreviousLotId(lotId: number): Promise<number | undefined>;
|
||||
export declare function clearNextPreviousLotIdCache(lotId?: number): void;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<number | undefined> {
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue