From 57adbebc01e0a47b922d8376dd5bf8673e7dbb01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 12 Jun 2022 10:29:41 +0200 Subject: [PATCH] Rename class ChargingStatinCache to SharedLRUCache MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 30 ++++++++++--------- ...rgingStationCache.ts => SharedLRUCache.ts} | 12 ++++---- 2 files changed, 22 insertions(+), 20 deletions(-) rename src/charging-station/{ChargingStationCache.ts => SharedLRUCache.ts} (92%) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 80735db6..772389de 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -49,7 +49,6 @@ import { AutomaticTransactionGeneratorConfiguration } from '../types/AutomaticTr import BaseError from '../exception/BaseError'; import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode'; import { ChargePointStatus } from '../types/ocpp/ChargePointStatus'; -import { ChargingStationCache } from './ChargingStationCache'; import ChargingStationConfiguration from '../types/ChargingStationConfiguration'; import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; import ChargingStationInfo from '../types/ChargingStationInfo'; @@ -73,6 +72,7 @@ import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService'; import OCPPRequestService from './ocpp/OCPPRequestService'; import { OCPPVersion } from '../types/ocpp/OCPPVersion'; import PerformanceStatistics from '../performance/PerformanceStatistics'; +import SharedLRUCache from './SharedLRUCache'; import { SupervisionUrlDistribution } from '../types/ConfigurationData'; import Utils from '../utils/Utils'; import crypto from 'crypto'; @@ -107,7 +107,7 @@ export default class ChargingStation { private autoReconnectRetryCount: number; private stopped: boolean; private templateFileWatcher!: fs.FSWatcher; - private readonly cache: ChargingStationCache; + private readonly sharedLRUCache: SharedLRUCache; private automaticTransactionGenerator!: AutomaticTransactionGenerator; private webSocketPingSetInterval!: NodeJS.Timeout; @@ -117,7 +117,7 @@ export default class ChargingStation { this.stopped = false; this.wsConnectionRestarted = false; this.autoReconnectRetryCount = 0; - this.cache = ChargingStationCache.getInstance(); + this.sharedLRUCache = SharedLRUCache.getInstance(); this.authorizedTagsCache = AuthorizedTagsCache.getInstance(); this.connectors = new Map(); this.requests = new Map(); @@ -514,7 +514,7 @@ export default class ChargingStation { this.templateFile } file have changed, reload` ); - this.cache.deleteChargingStationTemplate(this.stationInfo?.templateHash); + this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash); // Initialize this.initialize(); // Restart the ATG @@ -561,9 +561,9 @@ export default class ChargingStation { if (this.getEnableStatistics()) { this.performanceStatistics.stop(); } - this.cache.deleteChargingStationConfiguration(this.configurationFileHash); + this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.templateFileWatcher.close(); - this.cache.deleteChargingStationTemplate(this.stationInfo?.templateHash); + this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash); this.bootNotificationResponse = null; parentPort.postMessage({ id: ChargingStationWorkerMessageEvents.STOPPED, @@ -714,8 +714,8 @@ export default class ChargingStation { private getTemplateFromFile(): ChargingStationTemplate | null { let template: ChargingStationTemplate = null; try { - if (this.cache.hasChargingStationTemplate(this.stationInfo?.templateHash)) { - template = this.cache.getChargingStationTemplate(this.stationInfo.templateHash); + if (this.sharedLRUCache.hasChargingStationTemplate(this.stationInfo?.templateHash)) { + template = this.sharedLRUCache.getChargingStationTemplate(this.stationInfo.templateHash); } else { const measureId = `${FileType.ChargingStationTemplate} read`; const beginId = PerformanceStatistics.beginMeasure(measureId); @@ -727,7 +727,7 @@ export default class ChargingStation { .createHash(Constants.DEFAULT_HASH_ALGORITHM) .update(JSON.stringify(template)) .digest('hex'); - this.cache.setChargingStationTemplate(template); + this.sharedLRUCache.setChargingStationTemplate(template); } } catch (error) { FileUtils.handleFileException( @@ -1172,8 +1172,10 @@ export default class ChargingStation { let configuration: ChargingStationConfiguration = null; if (this.configurationFile && fs.existsSync(this.configurationFile)) { try { - if (this.cache.hasChargingStationConfiguration(this.configurationFileHash)) { - configuration = this.cache.getChargingStationConfiguration(this.configurationFileHash); + if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { + configuration = this.sharedLRUCache.getChargingStationConfiguration( + this.configurationFileHash + ); } else { const measureId = `${FileType.ChargingStationConfiguration} read`; const beginId = PerformanceStatistics.beginMeasure(measureId); @@ -1182,7 +1184,7 @@ export default class ChargingStation { ) as ChargingStationConfiguration; PerformanceStatistics.endMeasure(measureId, beginId); this.configurationFileHash = configuration.configurationHash; - this.cache.setChargingStationConfiguration(configuration); + this.sharedLRUCache.setChargingStationConfiguration(configuration); } } catch (error) { FileUtils.handleFileException( @@ -1220,9 +1222,9 @@ export default class ChargingStation { fs.writeFileSync(fileDescriptor, JSON.stringify(configurationData, null, 2), 'utf8'); fs.closeSync(fileDescriptor); PerformanceStatistics.endMeasure(measureId, beginId); - this.cache.deleteChargingStationConfiguration(this.configurationFileHash); + this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.configurationFileHash = configurationHash; - this.cache.setChargingStationConfiguration(configurationData); + this.sharedLRUCache.setChargingStationConfiguration(configurationData); } else { logger.debug( `${this.logPrefix()} Not saving unchanged charging station configuration file ${ diff --git a/src/charging-station/ChargingStationCache.ts b/src/charging-station/SharedLRUCache.ts similarity index 92% rename from src/charging-station/ChargingStationCache.ts rename to src/charging-station/SharedLRUCache.ts index b4e9b9d0..b834a69a 100644 --- a/src/charging-station/ChargingStationCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -10,19 +10,19 @@ enum CacheType { type CacheableType = ChargingStationTemplate | ChargingStationConfiguration; -export class ChargingStationCache { - private static instance: ChargingStationCache | null = null; +export default class SharedLRUCache { + private static instance: SharedLRUCache | null = null; private readonly lruCache: LRUCache; private constructor() { this.lruCache = new LRUCache(1000); } - public static getInstance(): ChargingStationCache { - if (!ChargingStationCache.instance) { - ChargingStationCache.instance = new ChargingStationCache(); + public static getInstance(): SharedLRUCache { + if (!SharedLRUCache.instance) { + SharedLRUCache.instance = new SharedLRUCache(); } - return ChargingStationCache.instance; + return SharedLRUCache.instance; } public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { -- 2.34.1