From ec69050249d8e32aa6ec786ff271179200bfb88e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 4 Jul 2022 00:47:47 +0200 Subject: [PATCH] Add helper in LRU cache to get cache key MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/SharedLRUCache.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index b834a69a..cda04196 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -26,7 +26,7 @@ export default class SharedLRUCache { } public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { - return this.has(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash); + return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); } public setChargingStationConfiguration( @@ -34,7 +34,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 +44,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); } -- 2.34.1