cache next and previous lots to speed up paging
parent
fcdd8f950e
commit
e1f766f712
|
|
@ -1,7 +1,7 @@
|
|||
import * as configFunctions from '../../helpers/functions.config.js';
|
||||
import { getNextLotId } from '../../helpers/lotOccupancyDB/getNextLotId.js';
|
||||
import { getNextLotId } from '../../helpers/functions.lots.js';
|
||||
export async function handler(request, response) {
|
||||
const lotId = request.params.lotId;
|
||||
const lotId = Number.parseInt(request.params.lotId, 10);
|
||||
const nextLotId = await getNextLotId(lotId);
|
||||
if (!nextLotId) {
|
||||
response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') +
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ import type { Request, Response } from 'express'
|
|||
|
||||
import * as configFunctions from '../../helpers/functions.config.js'
|
||||
|
||||
import { getNextLotId } from '../../helpers/lotOccupancyDB/getNextLotId.js'
|
||||
import { getNextLotId } from '../../helpers/functions.lots.js'
|
||||
|
||||
export async function handler(
|
||||
request: Request,
|
||||
response: Response
|
||||
): Promise<void> {
|
||||
const lotId = request.params.lotId
|
||||
const lotId = Number.parseInt(request.params.lotId, 10)
|
||||
|
||||
const nextLotId = await getNextLotId(lotId)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import * as configFunctions from '../../helpers/functions.config.js';
|
||||
import { getPreviousLotId } from '../../helpers/lotOccupancyDB/getPreviousLotId.js';
|
||||
import { getPreviousLotId } from '../../helpers/functions.lots.js';
|
||||
export async function handler(request, response) {
|
||||
const lotId = request.params.lotId;
|
||||
const lotId = Number.parseInt(request.params.lotId, 10);
|
||||
const previousLotId = await getPreviousLotId(lotId);
|
||||
if (!previousLotId) {
|
||||
response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') +
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ import type { Request, Response } from 'express'
|
|||
|
||||
import * as configFunctions from '../../helpers/functions.config.js'
|
||||
|
||||
import { getPreviousLotId } from '../../helpers/lotOccupancyDB/getPreviousLotId.js'
|
||||
import { getPreviousLotId } from '../../helpers/functions.lots.js'
|
||||
|
||||
export async function handler(
|
||||
request: Request,
|
||||
response: Response
|
||||
): Promise<void> {
|
||||
const lotId = request.params.lotId
|
||||
const lotId = Number.parseInt(request.params.lotId, 10)
|
||||
|
||||
const previousLotId = await getPreviousLotId(lotId)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import * as configFunctions from '../../helpers/functions.config.js';
|
||||
import { getNextLotId, getPreviousLotId } from '../../helpers/functions.lots.js';
|
||||
import { getLot } from '../../helpers/lotOccupancyDB/getLot.js';
|
||||
export async function handler(request, response) {
|
||||
const lot = await getLot(request.params.lotId);
|
||||
if (!lot) {
|
||||
if (lot === undefined) {
|
||||
response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') +
|
||||
'/lots/?error=lotIdNotFound');
|
||||
return;
|
||||
|
|
@ -11,5 +12,9 @@ export async function handler(request, response) {
|
|||
headTitle: lot.lotName,
|
||||
lot
|
||||
});
|
||||
response.on('finish', () => {
|
||||
void getNextLotId(lot.lotId);
|
||||
void getPreviousLotId(lot.lotId);
|
||||
});
|
||||
}
|
||||
export default handler;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import type { Request, Response } from 'express'
|
||||
|
||||
import * as configFunctions from '../../helpers/functions.config.js'
|
||||
import { getNextLotId, getPreviousLotId } from '../../helpers/functions.lots.js'
|
||||
|
||||
import { getLot } from '../../helpers/lotOccupancyDB/getLot.js'
|
||||
|
||||
|
|
@ -10,7 +11,7 @@ export async function handler(
|
|||
): Promise<void> {
|
||||
const lot = await getLot(request.params.lotId)
|
||||
|
||||
if (!lot) {
|
||||
if (lot === undefined) {
|
||||
response.redirect(
|
||||
configFunctions.getProperty('reverseProxy.urlPrefix') +
|
||||
'/lots/?error=lotIdNotFound'
|
||||
|
|
@ -22,6 +23,11 @@ export async function handler(
|
|||
headTitle: lot.lotName,
|
||||
lot
|
||||
})
|
||||
|
||||
response.on('finish', () => {
|
||||
void getNextLotId(lot.lotId)
|
||||
void getPreviousLotId(lot.lotId)
|
||||
})
|
||||
}
|
||||
|
||||
export default handler
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
export declare function getNextLotId(lotId: number): Promise<number | undefined>;
|
||||
export declare function getPreviousLotId(lotId: number): Promise<number | undefined>;
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import NodeCache from 'node-cache';
|
||||
import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js';
|
||||
import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js';
|
||||
const previousLotIdCache = new NodeCache({
|
||||
stdTTL: 2 * 60
|
||||
});
|
||||
const nextLotIdCache = new NodeCache({
|
||||
stdTTL: 2 * 60
|
||||
});
|
||||
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);
|
||||
}
|
||||
}
|
||||
return nextLotId;
|
||||
}
|
||||
export async function getPreviousLotId(lotId) {
|
||||
let previousLotId = previousLotIdCache.get(lotId);
|
||||
if (previousLotId === undefined) {
|
||||
previousLotId = await getPreviousLotIdFromDatabase(lotId);
|
||||
if (previousLotId !== undefined) {
|
||||
previousLotIdCache.set(lotId, previousLotId);
|
||||
nextLotIdCache.set(previousLotId, lotId);
|
||||
}
|
||||
}
|
||||
return previousLotId;
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
import NodeCache from 'node-cache'
|
||||
|
||||
import getPreviousLotIdFromDatabase from './lotOccupancyDB/getPreviousLotId.js'
|
||||
import getNextLotIdFromDatabase from './lotOccupancyDB/getNextLotId.js'
|
||||
|
||||
const previousLotIdCache = new NodeCache({
|
||||
stdTTL: 2 * 60 // two minutes
|
||||
})
|
||||
|
||||
const nextLotIdCache = new NodeCache({
|
||||
stdTTL: 2 * 60 // two minutes
|
||||
})
|
||||
|
||||
export async function getNextLotId(lotId: number): Promise<number | undefined> {
|
||||
let nextLotId: number | undefined = nextLotIdCache.get(lotId)
|
||||
|
||||
if (nextLotId === undefined) {
|
||||
nextLotId = await getNextLotIdFromDatabase(lotId)
|
||||
|
||||
if (nextLotId !== undefined) {
|
||||
previousLotIdCache.set(nextLotId, lotId)
|
||||
nextLotIdCache.set(lotId, nextLotId)
|
||||
}
|
||||
}
|
||||
|
||||
return nextLotId
|
||||
}
|
||||
|
||||
export async function getPreviousLotId(
|
||||
lotId: number
|
||||
): Promise<number | undefined> {
|
||||
let previousLotId: number | undefined = previousLotIdCache.get(lotId)
|
||||
|
||||
if (previousLotId === undefined) {
|
||||
previousLotId = await getPreviousLotIdFromDatabase(lotId)
|
||||
|
||||
if (previousLotId !== undefined) {
|
||||
previousLotIdCache.set(lotId, previousLotId)
|
||||
nextLotIdCache.set(previousLotId, lotId)
|
||||
}
|
||||
}
|
||||
|
||||
return previousLotId
|
||||
}
|
||||
|
|
@ -35,6 +35,7 @@
|
|||
"http-errors": "^2.0.0",
|
||||
"ical-generator": "^3.6.1",
|
||||
"leaflet": "^1.9.3",
|
||||
"node-cache": "^5.1.2",
|
||||
"papaparse": "^5.3.2",
|
||||
"randomcolor": "^0.6.2",
|
||||
"session-file-store": "^1.5.0",
|
||||
|
|
@ -2915,7 +2916,6 @@
|
|||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
}
|
||||
|
|
@ -8741,6 +8741,17 @@
|
|||
"integrity": "sha512-/ujIVxthRs+7q6hsdjHMaj8hRG9NuWmwrz+JdRwZ14jdFoKSkm+vDsCbF9PLpnSqjaWQJuTmVtcWHNLr+vrOFw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/node-cache": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||
"dependencies": {
|
||||
"clone": "2.x"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/node-domexception": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
|
||||
|
|
@ -14873,8 +14884,7 @@
|
|||
"clone": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
|
||||
"dev": true
|
||||
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w=="
|
||||
},
|
||||
"clone-buffer": {
|
||||
"version": "1.0.0",
|
||||
|
|
@ -19340,6 +19350,14 @@
|
|||
"integrity": "sha512-/ujIVxthRs+7q6hsdjHMaj8hRG9NuWmwrz+JdRwZ14jdFoKSkm+vDsCbF9PLpnSqjaWQJuTmVtcWHNLr+vrOFw==",
|
||||
"dev": true
|
||||
},
|
||||
"node-cache": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||
"requires": {
|
||||
"clone": "2.x"
|
||||
}
|
||||
},
|
||||
"node-domexception": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@
|
|||
"http-errors": "^2.0.0",
|
||||
"ical-generator": "^3.6.1",
|
||||
"leaflet": "^1.9.3",
|
||||
"node-cache": "^5.1.2",
|
||||
"papaparse": "^5.3.2",
|
||||
"randomcolor": "^0.6.2",
|
||||
"session-file-store": "^1.5.0",
|
||||
|
|
|
|||
Loading…
Reference in New Issue