X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FSharedLRUCache.ts;h=d55f358e18809b19e65972287330047440c3ab8b;hb=6a4032b5d8f3cbaa18d3beddcdfe9d335c1cba90;hp=a40de80293ad9090904be33cd5213e349bfe06fe;hpb=1ca780f9d385bcf96a016ab5ba57ca0f19c94b74;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index a40de802..d55f358e 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,22 +1,25 @@ -import LRUCache from 'mnemonist/lru-map-with-delete'; +import LRUCache from 'mnemonist/lru-map-with-delete.js'; -import ChargingStationConfiguration from '../types/ChargingStationConfiguration'; -import ChargingStationTemplate from '../types/ChargingStationTemplate'; -import Utils from '../utils/Utils'; +import { Bootstrap } from './Bootstrap'; +import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types'; +import { isEmptyObject, isNotEmptyArray, isNotEmptyString, isNullOrUndefined } from '../utils'; enum CacheType { - CHARGING_STATION_TEMPLATE = 'chargingStationTemplate', - CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration', + chargingStationTemplate = 'chargingStationTemplate', + chargingStationConfiguration = 'chargingStationConfiguration', } -type CacheableType = ChargingStationTemplate | ChargingStationConfiguration; +type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration; -export default class SharedLRUCache { +export class SharedLRUCache { private static instance: SharedLRUCache | null = null; - private readonly lruCache: LRUCache; + private readonly lruCache: LRUCache; private constructor() { - this.lruCache = new LRUCache(1000); + this.lruCache = new LRUCache( + Bootstrap.getInstance().numberOfChargingStationTemplates + + Bootstrap.getInstance().numberOfChargingStations, + ); } public static getInstance(): SharedLRUCache { @@ -31,21 +34,21 @@ export default class SharedLRUCache { } public setChargingStationConfiguration( - chargingStationConfiguration: ChargingStationConfiguration + chargingStationConfiguration: ChargingStationConfiguration, ): void { if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { this.set( - this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash), - chargingStationConfiguration + this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!), + chargingStationConfiguration, ); } } public getChargingStationConfiguration( - chargingStationConfigurationHash: string + chargingStationConfigurationHash: string, ): ChargingStationConfiguration { return this.get( - this.getChargingStationConfigurationKey(chargingStationConfigurationHash) + this.getChargingStationConfigurationKey(chargingStationConfigurationHash), ) as ChargingStationConfiguration; } @@ -59,14 +62,14 @@ export default class SharedLRUCache { public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void { this.set( - this.getChargingStationTemplateKey(chargingStationTemplate.templateHash), - chargingStationTemplate + this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!), + chargingStationTemplate, ); } public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate { return this.get( - this.getChargingStationTemplateKey(chargingStationTemplateHash) + this.getChargingStationTemplateKey(chargingStationTemplateHash), ) as ChargingStationTemplate; } @@ -79,22 +82,22 @@ export default class SharedLRUCache { } private getChargingStationConfigurationKey(hash: string): string { - return CacheType.CHARGING_STATION_CONFIGURATION + hash; + return `${CacheType.chargingStationConfiguration}${hash}`; } private getChargingStationTemplateKey(hash: string): string { - return CacheType.CHARGING_STATION_TEMPLATE + hash; + return `${CacheType.chargingStationTemplate}${hash}`; } private has(key: string): boolean { return this.lruCache.has(key); } - private get(key: string): CacheableType { + private get(key: string): CacheValueType | undefined { return this.lruCache.get(key); } - private set(key: string, value: CacheableType): void { + private set(key: string, value: CacheValueType): void { this.lruCache.set(key, value); } @@ -103,15 +106,17 @@ export default class SharedLRUCache { } private isChargingStationConfigurationCacheable( - chargingStationConfiguration: ChargingStationConfiguration + chargingStationConfiguration: ChargingStationConfiguration, ): boolean { return ( - !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) && - !Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) && - !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) && - !Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) && - !Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) && - !Utils.isEmptyString(chargingStationConfiguration?.configurationHash) + isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false && + isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false && + isNullOrUndefined(chargingStationConfiguration?.automaticTransactionGenerator) === false && + isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false && + isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true && + isEmptyObject(chargingStationConfiguration.stationInfo!) === false && + isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator!) === false && + isNotEmptyString(chargingStationConfiguration?.configurationHash) === true ); } }