Skip to content

Commit

Permalink
parking bylaws and locations cache
Browse files Browse the repository at this point in the history
closes #610
  • Loading branch information
dangowans committed Apr 23, 2024
1 parent d0d2e25 commit 3dfe531
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 17 deletions.
3 changes: 3 additions & 0 deletions database/parkingDB/addParkingBylaw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function addParkingBylaw(requestBody) {
const database = sqlite(databasePath);
const bylawRecord = database
Expand All @@ -21,6 +22,7 @@ export default function addParkingBylaw(requestBody) {
where bylawNumber = ?`)
.run(requestBody.bylawNumber);
database.close();
clearCacheByTableName('ParkingBylaws');
return {
success: info.changes > 0,
message: `By-law number "${requestBody.bylawNumber}" is associated with a previously removed record.
Expand All @@ -34,6 +36,7 @@ export default function addParkingBylaw(requestBody) {
values (?, ?, 0, 1)`)
.run(requestBody.bylawNumber, requestBody.bylawDescription);
database.close();
clearCacheByTableName('ParkingBylaws');
return {
success: info.changes > 0
};
Expand Down
3 changes: 3 additions & 0 deletions database/parkingDB/addParkingBylaw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'
import type { ParkingBylaw } from '../../types/recordTypes.js'

import type { AddUpdateParkingBylawReturn } from './getParkingBylaws.js'
Expand Down Expand Up @@ -41,6 +42,7 @@ export default function addParkingBylaw(
.run(requestBody.bylawNumber)

database.close()
clearCacheByTableName('ParkingBylaws')

return {
success: info.changes > 0,
Expand All @@ -61,6 +63,7 @@ export default function addParkingBylaw(
.run(requestBody.bylawNumber, requestBody.bylawDescription)

database.close()
clearCacheByTableName('ParkingBylaws')

return {
success: info.changes > 0
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/addParkingLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function addParkingLocation(requestBody) {
const database = sqlite(databasePath);
const locationRecord = database
Expand All @@ -20,6 +21,7 @@ export default function addParkingLocation(requestBody) {
values (?, ?, ?, 0, 1)`)
.run(requestBody.locationKey, requestBody.locationName, requestBody.locationClassKey);
database.close();
clearCacheByTableName('ParkingLocations');
return {
success: info.changes > 0
};
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/addParkingLocation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'
import type { ParkingLocation } from '../../types/recordTypes.js'

import type { AddUpdateParkingLocationReturn } from './getParkingLocations.js'
Expand Down Expand Up @@ -48,6 +49,7 @@ export default function addParkingLocation(
)

database.close()
clearCacheByTableName('ParkingLocations')

return {
success: info.changes > 0
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/deleteParkingBylaw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function deleteParkingBylaw(bylawNumber) {
const database = sqlite(databasePath);
const info = database
Expand All @@ -9,6 +10,7 @@ export default function deleteParkingBylaw(bylawNumber) {
and isActive = 1`)
.run(bylawNumber);
database.close();
clearCacheByTableName('ParkingBylaws');
return {
success: info.changes > 0
};
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/deleteParkingBylaw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'

import type { AddUpdateParkingBylawReturn } from './getParkingBylaws.js'

Expand All @@ -20,6 +21,7 @@ export default function deleteParkingBylaw(
.run(bylawNumber)

database.close()
clearCacheByTableName('ParkingBylaws')

return {
success: info.changes > 0
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/deleteParkingLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function deleteParkingLocation(locationKey) {
const database = sqlite(databasePath);
const info = database
Expand All @@ -9,6 +10,7 @@ export default function deleteParkingLocation(locationKey) {
and isActive = 1`)
.run(locationKey);
database.close();
clearCacheByTableName('ParkingLocations');
return {
success: info.changes > 0
};
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/deleteParkingLocation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'

import type { AddUpdateParkingLocationReturn } from './getParkingLocations.js'

Expand All @@ -20,6 +21,7 @@ export default function deleteParkingLocation(
.run(locationKey)

database.close()
clearCacheByTableName('ParkingLocations')

return {
success: info.changes > 0
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/updateParkingBylaw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function updateParkingBylaw(requestBody) {
const database = sqlite(databasePath);
const info = database
Expand All @@ -9,6 +10,7 @@ export default function updateParkingBylaw(requestBody) {
and isActive = 1`)
.run(requestBody.bylawDescription, requestBody.bylawNumber);
database.close();
clearCacheByTableName('ParkingBylaws');
return {
success: info.changes > 0
};
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/updateParkingBylaw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'
import type { ParkingBylaw } from '../../types/recordTypes.js'

import type { AddUpdateParkingBylawReturn } from './getParkingBylaws.js'
Expand All @@ -21,6 +22,7 @@ export default function updateParkingBylaw(
.run(requestBody.bylawDescription, requestBody.bylawNumber)

database.close()
clearCacheByTableName('ParkingBylaws')

return {
success: info.changes > 0
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/updateParkingLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function updateParkingLocation(requestBody) {
const database = sqlite(databasePath);
const info = database
Expand All @@ -10,6 +11,7 @@ export default function updateParkingLocation(requestBody) {
and isActive = 1`)
.run(requestBody.locationName, requestBody.locationClassKey, requestBody.locationKey);
database.close();
clearCacheByTableName('ParkingLocations');
return {
success: info.changes > 0
};
Expand Down
2 changes: 2 additions & 0 deletions database/parkingDB/updateParkingLocation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'
import { clearCacheByTableName } from '../../helpers/functions.cache.js'
import type { ParkingLocation } from '../../types/recordTypes.js'

import type { AddUpdateParkingLocationReturn } from './getParkingLocations.js'
Expand All @@ -26,6 +27,7 @@ export default function updateParkingLocation(
)

database.close()
clearCacheByTableName('ParkingLocations')

return {
success: info.changes > 0
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-get/locations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js';
import { getParkingLocations } from '../../helpers/functions.cache.js';
export default function handler(_request, response) {
const locations = getParkingLocations();
response.render('location-maint', {
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-get/locations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request, Response } from 'express'

import getParkingLocations from '../../database/parkingDB/getParkingLocations.js'
import { getParkingLocations } from '../../helpers/functions.cache.js'

export default function handler(_request: Request, response: Response): void {
const locations = getParkingLocations()
Expand Down
3 changes: 1 addition & 2 deletions handlers/admin-get/offences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import getParkingBylaws from '../../database/parkingDB/getParkingBylaws.js';
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js';
import getParkingOffences from '../../database/parkingDB/getParkingOffences.js';
import { getParkingBylaws, getParkingLocations } from '../../helpers/functions.cache.js';
export default function handler(_request, response) {
const locations = getParkingLocations();
const bylaws = getParkingBylaws();
Expand Down
6 changes: 4 additions & 2 deletions handlers/admin-get/offences.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Request, Response } from 'express'

import getParkingBylaws from '../../database/parkingDB/getParkingBylaws.js'
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js'
import getParkingOffences from '../../database/parkingDB/getParkingOffences.js'
import {
getParkingBylaws,
getParkingLocations
} from '../../helpers/functions.cache.js'

export default function handler(_request: Request, response: Response): void {
const locations = getParkingLocations()
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doAddLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import addParkingLocation from '../../database/parkingDB/addParkingLocation.js';
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js';
import { getParkingLocations } from '../../helpers/functions.cache.js';
export default function handler(request, response) {
const results = addParkingLocation(request.body);
if (results.success) {
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doAddLocation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request, Response } from 'express'

import addParkingLocation from '../../database/parkingDB/addParkingLocation.js'
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js'
import { getParkingLocations } from '../../helpers/functions.cache.js'
import type { ParkingLocation } from '../../types/recordTypes.js'

export default function handler(request: Request, response: Response): void {
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doDeleteLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deleteParkingLocation from '../../database/parkingDB/deleteParkingLocation.js';
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js';
import { getParkingLocations } from '../../helpers/functions.cache.js';
export default function handler(request, response) {
const results = deleteParkingLocation(request.body.locationKey);
if (results.success) {
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doDeleteLocation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request, Response } from 'express'

import deleteParkingLocation from '../../database/parkingDB/deleteParkingLocation.js'
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js'
import { getParkingLocations } from '../../helpers/functions.cache.js'

export default function handler(request: Request, response: Response): void {
const results = deleteParkingLocation(request.body.locationKey as string)
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doUpdateLocation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getParkingLocations from '../../database/parkingDB/getParkingLocations.js';
import updateParkingLocation from '../../database/parkingDB/updateParkingLocation.js';
import { getParkingLocations } from '../../helpers/functions.cache.js';
export default function handler(request, response) {
const results = updateParkingLocation(request.body);
if (results.success) {
Expand Down
2 changes: 1 addition & 1 deletion handlers/admin-post/doUpdateLocation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request, Response } from 'express'

import getParkingLocations from '../../database/parkingDB/getParkingLocations.js'
import updateParkingLocation from '../../database/parkingDB/updateParkingLocation.js'
import { getParkingLocations } from '../../helpers/functions.cache.js'
import type { ParkingLocation } from '../../types/recordTypes.js'

export default function handler(request: Request, response: Response): void {
Expand Down
5 changes: 5 additions & 0 deletions helpers/functions.cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { CacheTableName } from '../types/applicationTypes.js';
import type { ParkingBylaw, ParkingLocation } from '../types/recordTypes.js';
export declare function getParkingBylaws(): ParkingBylaw[];
export declare function getParkingLocations(): ParkingLocation[];
export declare function clearCacheByTableName(tableName: CacheTableName, relayMessage?: boolean): void;
57 changes: 57 additions & 0 deletions helpers/functions.cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import cluster from 'node:cluster';
import Debug from 'debug';
import getParkingBylawsFromDatabase from '../database/parkingDB/getParkingBylaws.js';
import getParkingLocationsFromDatabase from '../database/parkingDB/getParkingLocations.js';
const debug = Debug(`parking-ticket-system:functions.cache:${process.pid}`);
let parkingBylaws = [];
export function getParkingBylaws() {
if (parkingBylaws.length === 0) {
debug('Cache miss: ParkingBylaws');
parkingBylaws = getParkingBylawsFromDatabase();
}
return parkingBylaws;
}
let parkingLocations = [];
export function getParkingLocations() {
if (parkingLocations.length === 0) {
debug('Cache miss: ParkingLocations');
parkingLocations = getParkingLocationsFromDatabase();
}
return parkingLocations;
}
export function clearCacheByTableName(tableName, relayMessage = true) {
switch (tableName) {
case 'ParkingBylaws': {
parkingBylaws = [];
break;
}
case 'ParkingLocations': {
parkingLocations = [];
break;
}
default: {
debug(`Ignoring clear cache for unknown table: ${tableName}`);
}
}
try {
if (relayMessage && cluster.isWorker && process.send !== undefined) {
const workerMessage = {
messageType: 'clearCache',
tableName,
timeMillis: Date.now(),
pid: process.pid
};
debug(`Sending clear cache from worker: ${tableName}`);
process.send(workerMessage);
}
}
catch {
debug('Error sending clear cache message.');
}
}
process.on('message', (message) => {
if (message.messageType === 'clearCache' && message.pid !== process.pid) {
debug(`Clearing cache: ${message.tableName}`);
clearCacheByTableName(message.tableName, false);
}
});
Loading

0 comments on commit 3dfe531

Please sign in to comment.