From d6a62ad9d0be1d1f29530dc9bae59272d40efee7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 21 Mar 2023 23:54:08 +0100 Subject: [PATCH] perf(simulator): optimize tags cache MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit merge maps into one Signed-off-by: Jérôme Benoit --- src/charging-station/AuthorizedTagsCache.ts | 81 ++++++++++----------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index 332b3a98..48c72fd4 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -3,14 +3,17 @@ import fs from 'node:fs'; import { FileType } from '../types'; import { FileUtils, Utils, logger } from '../utils'; +type TagsCacheValueType = { + tags: string[]; + tagsFileWatcher: fs.FSWatcher | undefined; +}; + export class AuthorizedTagsCache { private static instance: AuthorizedTagsCache | null = null; - private readonly tagsCaches: Map; - private readonly FSWatchers: Map; + private readonly tagsCaches: Map; private constructor() { - this.tagsCaches = new Map(); - this.FSWatchers = new Map(); + this.tagsCaches = new Map(); } public static getInstance(): AuthorizedTagsCache { @@ -23,38 +26,6 @@ export class AuthorizedTagsCache { public getAuthorizedTags(file: string): string[] | undefined { if (this.hasTags(file) === false) { this.setTags(file, this.getAuthorizedTagsFromFile(file)); - // Monitor authorization file - this.FSWatchers.has(file) === false && - this.FSWatchers.set( - file, - FileUtils.watchJsonFile( - file, - FileType.Authorization, - this.logPrefix(file), - undefined, - (event, filename) => { - if (Utils.isNotEmptyString(filename) && event === 'change') { - try { - logger.debug( - `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` - ); - this.deleteTags(file); - this.deleteFSWatcher(file); - } catch (error) { - FileUtils.handleFileException( - file, - FileType.Authorization, - error as NodeJS.ErrnoException, - this.logPrefix(file), - { - throwError: false, - } - ); - } - } - } - ) - ); } return this.getTags(file); } @@ -68,22 +39,46 @@ export class AuthorizedTagsCache { } private setTags(file: string, tags: string[]) { - return this.tagsCaches.set(file, tags); + return this.tagsCaches.set(file, { + tags, + tagsFileWatcher: FileUtils.watchJsonFile( + file, + FileType.Authorization, + this.logPrefix(file), + undefined, + (event, filename) => { + if (Utils.isNotEmptyString(filename) && event === 'change') { + try { + logger.debug( + `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` + ); + this.deleteTags(file); + } catch (error) { + FileUtils.handleFileException( + file, + FileType.Authorization, + error as NodeJS.ErrnoException, + this.logPrefix(file), + { + throwError: false, + } + ); + } + } + } + ), + }); } private getTags(file: string): string[] | undefined { - return this.tagsCaches.get(file); + return this.tagsCaches.get(file)?.tags; } private deleteTags(file: string): boolean { + this.tagsCaches.get(file)?.tagsFileWatcher?.close(); return this.tagsCaches.delete(file); } - private deleteFSWatcher(file: string): boolean { - this.FSWatchers.get(file)?.close(); - return this.FSWatchers.delete(file); - } - private getAuthorizedTagsFromFile(file: string): string[] { let authorizedTags: string[] = []; if (file) { -- 2.34.1