UI server: permit to address all charging stations
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
index b834a69a946fdba25f2dd3b45731597b2fa48ab2..01e18734b673295004a3c033a5db37bd382d6c58 100644 (file)
@@ -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);
   }