Add helper in LRU cache to get cache key
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 3 Jul 2022 22:47:47 +0000 (00:47 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 3 Jul 2022 22:47:47 +0000 (00:47 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/SharedLRUCache.ts

index b834a69a946fdba25f2dd3b45731597b2fa48ab2..cda04196aa3c798f8bee698073c950940ac5e762 100644 (file)
@@ -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);
   }