X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FSharedLRUCache.ts;h=01e18734b673295004a3c033a5db37bd382d6c58;hb=18057587414006953ed112f315807d64ddb11bfd;hp=b834a69a946fdba25f2dd3b45731597b2fa48ab2;hpb=57adbebc01e0a47b922d8376dd5bf8673e7dbb01;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index b834a69a..01e18734 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,6 +1,7 @@ -import ChargingStationConfiguration from '../types/ChargingStationConfiguration'; -import ChargingStationTemplate from '../types/ChargingStationTemplate'; import LRUCache from 'mnemonist/lru-map-with-delete'; + +import type ChargingStationConfiguration from '../types/ChargingStationConfiguration'; +import type ChargingStationTemplate from '../types/ChargingStationTemplate'; import Utils from '../utils/Utils'; enum CacheType { @@ -19,14 +20,14 @@ export default class SharedLRUCache { } public static getInstance(): SharedLRUCache { - if (!SharedLRUCache.instance) { + if (SharedLRUCache.instance === null) { SharedLRUCache.instance = new SharedLRUCache(); } return SharedLRUCache.instance; } public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { - return this.has(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash); + return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); } public setChargingStationConfiguration( @@ -34,7 +35,7 @@ export default class SharedLRUCache { ): void { if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { this.set( - CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfiguration.configurationHash, + this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash), chargingStationConfiguration ); } @@ -44,39 +45,47 @@ export default class SharedLRUCache { chargingStationConfigurationHash: string ): ChargingStationConfiguration { return this.get( - CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash + this.getChargingStationConfigurationKey(chargingStationConfigurationHash) ) as ChargingStationConfiguration; } public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void { - this.delete(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash); + this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); } public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean { - return this.has(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash); + return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)); } public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void { this.set( - CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplate.templateHash, + this.getChargingStationTemplateKey(chargingStationTemplate.templateHash), chargingStationTemplate ); } public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate { return this.get( - CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash + this.getChargingStationTemplateKey(chargingStationTemplateHash) ) as ChargingStationTemplate; } public deleteChargingStationTemplate(chargingStationTemplateHash: string): void { - this.delete(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash); + this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)); } public clear(): void { this.lruCache.clear(); } + private getChargingStationConfigurationKey(hash: string): string { + return CacheType.CHARGING_STATION_CONFIGURATION + hash; + } + + private getChargingStationTemplateKey(hash: string): string { + return CacheType.CHARGING_STATION_TEMPLATE + hash; + } + private has(key: string): boolean { return this.lruCache.has(key); }